#!/usr/bin/perl -w
#
# $Id: frob_sysconfig 3715 2005-01-19 09:06:05Z sarnold $
# ----------------------------------------------------------------------
#    PROPRIETARY DATA of IMMUNIX INC.
#    Copyright (c) 2004, IMMUNIX (All rights reserved)
#
#    This document contains trade secret data which is the property
#    of IMMUNIX Inc.  This document is submitted to recipient in
#    confidence. Information contained herein may not be used, copied
#    or disclosed in whole or in part except as permitted by written
#    agreement signed by an officer of IMMUNIX, Inc.
# ----------------------------------------------------------------------
# read /etc/sysconfig/apache2 and add "change_hat" to the apache
# modules found there

use Getopt::Long;
use File::Temp qw/ :mktemp  /;

my ($conffile,$help,$remove);
GetOptions("file=s" => \$conffile,
           "remove" => \$remove,
           "help!" => \$help);

if ($help) {
	print "$0\t--file=<file>       modify <file>\n";
	print "\t\t\t--remove            remove option from config file\n";
	print "\t\t\t--help              this help\n";
	exit(0);
}

if (defined $conffile) {
	$old = $conffile;
	chomp($old);
} else {
	$old="/etc/sysconfig/apache2";
}
open(MENU,"<$old") or die "Fatal: can't open $old: $!";
($fh, $file) = mkstemp($old . "XXXXXX" );

while (<MENU>) {
# ok, we rely on the '="' to site the changes ;
	if (! defined $remove) {
		if ( /^\s*APACHE_MODULES="/ ) {
			if ( ! /change_hat/ ) {
				s/="/="change_hat /;
			}
		}
	} else { # remove the option
		if ( /^\s*APACHE_MODULES=".*change_hat/ ) {
			s/change_hat\s*//;
		}
	}

	print $fh $_;
}

rename $old, "$old.orig" || system("/bin/mv", $old, "$old.orig") &&
	die "$old could not be renamed to $old.orig ($!); see $file for modifications";
rename $file, "$old" || system("/bin/mv", $file, "$old") &&
	die "$file could not be renamed to $old ($!); see $file for modifications";
