#!/usr/bin/perl
# $Id: apachevhost.pl,v 1.5 2006/09/29 12:32:38 weby Exp $
#
# use emacs;
use strict;
use warnings;
use Getopt::Long;


{
    my $opt = {};
    GetOptions ($opt,
		"add","del","h|help",
		"domain=s","ip=s","user=s","group=s","home=s","file=s");
    
    verify_options($opt);
        
    if ($opt->{add}) {
	add_vhost($opt);
    }
    elsif ($opt->{del}) {
	del_vhost($opt);
    }
}

sub verify_options {
    my $opt = shift;
    
    if (exists $opt->{add}) {
	if (not exists $opt->{domain} or 
	    not exists $opt->{ip}     or 
	    not exists $opt->{user}   or 
	    not exists $opt->{group}  or 
	    not exists $opt->{home})
	{
	    usage();
	}
    }
    elsif (exists $opt->{del}) {
	if (!exists $opt->{domain}) {
	    usage();
	}
    }
    else {
	usage();
    }
}

sub add_vhost {
    my $opt = shift;

    my $httpd = read_config($opt->{file});
    my $vhost = find_vhost($opt, $httpd);
    
    if ($vhost) {
	print "\n";
	print "    ERROR: Vhost *** $opt->{domain} *** already exists!\n\n";
	exit(1); # exit eith status 1 - error
    }

    my $config = 
	"<VirtualHost $opt->{ip}>\n" .
	"\t# ADDED BY SCRIPT $0\n" .
	"\tUser         $opt->{user}\n" .
	"\tGroup        $opt->{group}\n" . 
	"\tServerName   $opt->{domain}\n" . 
	"\tDocumentRoot $opt->{home}\n" .
	"</VirtualHost>\n";
    
    $httpd .= "\n$config";
    write_config($httpd, $opt->{file});
    print "\n    Vhost $opt->{domain} added!\n\n";
}

sub del_vhost {
    my $opt = shift;

    my $httpd = read_config($opt->{file});
    my $vhost = find_vhost($opt, $httpd);

    if ($vhost) {
	my $xpr = quotemeta $vhost;
	if ($httpd =~ s|$xpr||) {
	    write_config($httpd, $opt->{file});
	    print "\n    Vhost $opt->{domain} removed!\n\n";
	}
    }
    else {
	print "\n";
	print "   ERROR: Vhost *** $opt->{domain} *** not found!\n\n";
    }
}

sub find_vhost {
    my $opt = shift;
    my $httpd = shift;

    my $domain = quotemeta $opt->{domain};
    my $vhost = undef;

    while ($httpd =~ m|(\n?\#?<VirtualHost .*?>.*?</VirtualHost>\n)|igsm) {
        $vhost = $1;
        if ($vhost =~ m|\s*ServerName\s+[w\.]*$domain\s*$|igsm) {
            if ($vhost !~ /^\s*\#/) {
                last;
            }
            else {
                print "** Domain found, but commented! Continue search...\n";
            }
        }
        undef $vhost;
    }

    return $vhost;
}

sub usage {
    print "\n";
    print "  USAGE:  $0 [OPTIONS]\n\n";
    print "  OPTIONS:\n";
    print "    $0 -add -domain=domain.com -ip=127.0.0.1 -user=userId -group=www-data -home=/var/www -file httpd.conf\n";
    print "    $0 -del -domain=domain.com -file httpd.conf\n";
    print "    $0 -help\n";
    print "\n";
    exit;
}

sub read_config {
    my $file = shift || 'httpd.conf';
    open FH, "$file" or die $!;
    my @data = <FH>;
    close FH;
    return join '', @data;
}

sub write_config {
    my $fdata = shift;
    my $file = shift || 'httpd.conf';
    #print "  Wrinting file $file\n";
    open FH, ">$file" or die $!;
    print FH $fdata;
    close FH;
}