Sophie

Sophie

distrib > Mandriva > 10.0-com > i586 > by-pkgid > 28344608c49d1e65847c4aa1edb9977d > files > 12

apache-conf-2.0.48-4mdk.src.rpm

#!/usr/bin/perl
# adxvaddmod
# Script to modify the Apache configuration file to include a module
# (C) 2001 Jean-Michel Dault <jmdault@mandrakesoft.com> and Mandrakesoft
# You can use it under the Apache Licence

# This script will try to add the module at the correct place in the config
# file. It will try to find the last LoadModule and AddModule directives
# that come before a special section (<Directory>, <VirtualHost>...) and 
# try to place its directives there. If you specify a "before=mod_foo"
# option, it will try to position itself before that module. If you specify
# "define=WHATEVER", it will place the module between <IfDefine WHATEVER>
# directives.

$bakpath="/etc/httpd/conf/bak";

if ((!$ARGV[0]) || (!$ARGV[1]) || (!$ARGV[2]) || (!$ARGV[3])) {
    print "Usage: $0 <conffile> <path/mod_filename.so> <mod_name.c> \n";
    print "<name_module> [OPTIONS]\n";
    print "Options:\n";
    print " before=mod_filename will add before specified module\n";
    print " define=WHATEVER will install module between <IfDefine WHATEVER>\n";
    print " addconf=CONFFILE adds 'Include CONFFILE' in httpd.conf\n";
    die "\n";
}

sub debug{
    if ($DEBUG) { foreach (@_) { print $_; } }
}

$modso=$ARGV[1];
$modc=$ARGV[2];
$name_mod=$ARGV[3];

@confparts=split(/\//,$ARGV[0]);

if ($#confparts==0) { 
#    debug "confparts=0\n";
    $confpath="/etc/httpd/conf"; 
    $conf="$ARGV[0]";
} else {
#    debug "confparts=$#confparts\n";
    $conf="$confparts[$#confparts]";
    $confpath=join('/',@confparts[0 .. $#confparts-1]);
}

system("mkdir -p $bakpath") ==0  
	or die "Can't create backup directory $bakpath\n";

#debug "Confpath: $confpath\n";
#debug "Conffile: $conf\n";
#debug "Backups: $bakpath\n";


foreach (@ARGV) {
    if (/before=(\w+)/i) {
	$_=$1;
	s/^mod_//g;
	s/^lib//g;
	s/\.so$//g;
	$before1="mod_$_";
	$before2="lib$_";
	debug "##before1=$before1##before2=$before2\n";
    }
    if (/define=(\w+)/i) {
	$opendef="<IfDefine $1>\n";
	$closedef="</IfDefine>\n";
	debug "##define=$1##\n";
    }
    if (/addconf=(\S+)/i) {
	$addconf="Include  $1\n";
    }
}


$_=`date +%Y%m%d-%T`;chop;s/:/./g;$dat=$_;
$bak="$conf-$dat";
debug "$bak\n";


open(BAK,">$bakpath/$bak") or die "Can't create $bakpath/$bak\n";
open(CONFF,"$confpath/$conf") or die "Can't open $confpath/$conf\n";

while (<CONFF>) {
    print BAK $_;
    if ($insidesection==0) { $lastbeforesection=$.};
    if (/^\<\w+/i) {
	$insidesection=1;
	if (/^\<IfDefine/i) { $lastbeforedefine=$.};
	$lastbeforesection-=1;
#	debug "Entering section $_";
    }
    if (/^\<\/\w+/i) {
	$insidesection=0;
	if (/^\<\/IfDefine/i) { $lastbeforedefine=0};
#	debug "Leaving section $_";
    }
    if (/^\<(Directory)/i) {
	if ($inmain==0) { debug "Met my first inmain directive\n"; }
	$inmain=1;
    }
    if (/^\<(Location)/i) {
	if ($inmain==0) { debug "Met my first inmain directive\n"; }
	$inmain=1;
    }
    if (/^\<(Files)/i) {
	if ($inmain==0) { debug "Met my first inmain directive\n"; }
	$inmain=1;
    }
    if (/^\<(Limit)/i) {
	if ($inmain==0) { debug "Met my first inmain directive\n"; }
	$inmain=1;
    }
    if (/^\<(VirtualHost)/i) {
	if ($inmain==0) { debug "Met my first inmain directive\n"; }
	$inmain=1;
    }
    if (/^#*LoadModule\s+\w+_module\s+.*[\/]{1}(\w+)\./i) {
	if ($inmain==0) {$lastload=$lastbeforesection}
	if ($lastbeforedefine > 0) {$lastload=$lastbeforedefine}
	if (($before1 eq $1) || ($before2 eq $1)) { 
	    $beforeload=$lastload;
	    debug "Found Loadmodule $1 at line $beforeload\n";
	}
#	debug "Load module #$1# at line line $lastload\n";
    }
    if (/^#*AddModule\s+(\w+)\./i) {
	if ($inmain==0) {$lastadd=$lastbeforesection}
	if ($lastbeforedefine > 0) {$lastadd=$lastbeforedefine}
	if (($before1 eq $1) || ($before2 eq $1)) { 
	    $beforeadd=$lastadd;
	    debug "Found Addmodule $1 at line $beforeadd\n";
	}
#	debug "Add module #$1# at line $lastadd\n";
    }
}
$lastline=$.;

if ($lastload > 1) {
    debug "Last valid LoadModule at line $lastload\n";
} else {
    debug "Did not find any valid LoadModule directive. Load at the end.\n";
    $lastload=$lastline+1;
}
if ($lastadd > 1) {
    debug "Last valid AddModule at line $lastadd\n";
} else {
    debug "Did not find any valid AddModule directive. Add at the end.\n";
    $lastadd=$lastline+1;
}


if ($beforeload > $lastload) {
    debug "$before1 or $before2 loaded too low in the config file!\n";
    debug "Will load right before the last valid module\n";
}

if ($beforeadd > $lastadd) {
    debug "$before1 or $before2 added too low in the config file!\n";
    debug "Will add right before the last valid module\n";
}

if (($beforeload > 0) && ($beforeload <= $lastload)) {
    $lastload=$beforeload;
    debug "Beforeload exists and is before (or is) the last valid loaded module\n";
    debug "Loading before $before1 or $before2 directive\n";
}
if (($beforeadd > 0) && ($beforeadd <= $lastadd)) {
    $lastadd=$beforeadd;
    debug "Beforeadd exists and is before (or is) the last valid loaded module\n";
    debug "Adding before $before1 or $before2 directive\n";
}

#remove 1 from the line count
$lastload-=1;$lastadd-=1;

debug "Beforeload $beforeload\n";
debug "Beforeadd $beforeadd\n";
debug "Lastload: $lastload\n";
debug "Lastadd: $lastadd\n";


close(CONFF);
close(BAK);

debug "\nFinal results:\n";
debug "Put Loadmodule after line: $lastload\n";
debug "Put Addmodule after line: $lastadd\n";

open(BAK,"$bakpath/$bak") or die "Can't open $bakpath/$bak\n";
open(CONFF,">$confpath/$conf") or die "Can't create $confpath/$conf\n";

while (<BAK>) {
    print CONFF $_;
    if ($.==$lastload) {
	print CONFF "$opendef";
	print CONFF "LoadModule $name_mod    $modso\n";
	print CONFF "$closedef";
    }
    if ($.==$lastadd) {
	print CONFF "$opendef";
	print CONFF "AddModule $modc\n";
	print CONFF "$closedef";
    }
}
print CONFF $addconf;

close(CONFF);
close(BAK);