#! /usr/bin/perl
######################################################
#Scriptname: remove_virtual.pl
#Description: This script searches for a given website
#	     and deletes it from apache configuration
#Created/Date: Mike Rhyner / 11/16/2002
#Changed:
######################################################

use strict;

my $apacheconf = $ARGV[1];
my $webname = $ARGV[0];
my @oldconf = ();
my @newconf = ();

if ($webname eq "") {
  print "usage: remove_virtual.pl <webname> <apache conf file>\n";
  exit 1;
}
readconf();
writeconf();

sub readconf {
  open (CONF, $apacheconf);
  push @oldconf,<CONF>;
} 

sub writeconf {
  my $inputline;
  my $block = 0;
  my $remove = 0;
  my $removed = 0;
  my $i = 0;
  my @virtualhost = ();
  open (CONF, ">$apacheconf");
  while ($inputline = @oldconf[$i]) {
    if ($inputline =~ /<VirtualHost\s.*>/) {
      $block = 1;
    }
    if ($inputline =~ /ServerName\s*$webname/) {
      $remove = 1;
    }
    if ($block == 1) {
      push @virtualhost, $inputline;
    } else {
        if (($inputline =~ /\s*\n/) && ($removed==1)) {
         } else {
           print CONF $inputline;
         }
    }
    $removed=0;
    if ($inputline =~ /<\/VirtualHost>/) {
      $block = 0;
      if ($remove == 0) {
        print CONF @virtualhost; 
      } else {
        $remove = 0;
        $removed = 1;
      }
      @virtualhost = ();
    }
    $i++;
  }
  close (CONF);
} 
