# generate virtual hosts on the fly with Perl
<Perl>
#!/usr/bin/perl -w
#line <n>
# The above along with the __END__ at the bottom allows us to check the
# syntax of the section with 'perl -cx httpd.conf'. Change the '<n>' in
# '#line <n>' to whatever line in httpd.conf the Perl section really starts

# Define some local variables. These are made local so Apache doesn't 
# see them and try to interpret them as configuration directives
local ($ip,$host,$admin,$vroot,$aliases);
local ($directive,$args);

# Open the virtual hosts file
open (FILE,"/usr/local/apache139/conf/vhosts.conf");

# Pull lines from the file one by one
while (<FILE>) {
        # Skip comments and blank lines
        next if /^\s*(#|$)/;

        # If the line starts with a number it's the IP of a new host
        if (/^\d+/) {

                # Extract core vhost values and assign them
                ($ip,$host,$admin,$vroot,$aliases)=split /\s*,\s*/,$_;
                $VirtualHost{$ip}={
                        ServerName => $host,
                        ServerAdmin => $admin, 
                        DocumentRoot => "/home/www/".$vroot,
                        ErrorLog => "logs/".$host."_error",
                        TransferLog => "logs/".$host."_log"
                };

                # If we have any aliases, assign them to a ServerAlias directive
                $VirtualHost{$ip}{ServerAlias}=$aliases if $aliases;

                # If the IP has a port number attached, infer and add a Port directive
                $VirtualHost{$ip}{Port}=$1 if ($ip=~/:(\d+)$/); 

        # Otherwise it's an arbitrary additional directive for the current host
        } elsif ($ip) { 
                # Note this only handles simple directives, not containers
                ($directive,$args)=split / /,$_,2; 
                $VirtualHost{$ip}{$directive}=$args;
        }
}

# All done
close (FILE); 

# Tell 'perl -cx' to stop checking
__END__
# back to httpd.conf
</Perl>
