#!/usr/bin/perl -w
#
# $Id: frob_sysconfig 5910 2005-12-09 03:41:29Z steve $
# ----------------------------------------------------------------------
#    Copyright (c) 2004, 2005 NOVELL (All rights reserved)
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of version 2 of the GNU General Public
#    License published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, contact Novell, Inc.
# ----------------------------------------------------------------------
# read /etc/sysconfig/apache2 and add "change_hat" to the apache
# modules found there

use Getopt::Long;
use File::Temp qw/ :mktemp  /;
my $module="apparmor";

sub usage() {
	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);
}

my ($conffile,$help,$remove);

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

usage() if $help;

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 ( ! /$module/ ) {
				s/="/="$module /;
			}
		}
	} else { # remove the option
		if ( /^\s*APACHE_MODULES=".*$module/ ) {
			s/$module\s*//;
		}
		# remove old versions of the module
		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";
