#!/usr/bin/perl -w
##
## Copyright (c) 2001, 2002, Amnon BARAK (amnon@MOSIX.org).
## All rights reserved.
## This software is subject to the MOSIX SOFTWARE LICENSE AGREEMENT.
##
## THIS SOFTWARE IS PROVIDED IN ITS "AS IS" CONDITION, WITH NO WARRANTY
## WHATSOEVER. NO LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING
## FROM THE USE OF THIS SOFTWARE WILL BE ACCEPTED.
##
##
## Author(s): Shlomo Matichin 
##

# This script modifies the inittab configuration file to prevent
# migration of daemons and system-maintenance tasks.

# Default arguments currently compatible with all distributions.

use FindBin;
use Getopt::Long;
use lib "$FindBin::Bin";
use common;

GetOptions(	"help" ,		\$usage ,
		"uninstall" ,		\$uninstall );

usage() if defined $usage ;
-w "/etc/inittab" or
	dead "You do not have write permissions on /etc/inittab";
-w "/etc" or 
	dead "You do not have write permissions on /etc";
uninstall() and exit 1 if defined $uninstall ;
-e "$FindBin::Bin/inittab_awk" or
	dead "MOSIX Distribution is not intact: Unable to find $FindBin::Bin/inittab_awk";
install();
exit 1;
    
sub install
{
	-e "/etc/inittab.pre_mosix" and 
		dead "Please Uninstall MOSIX before reinstalling";
	system( "mv -f /etc/inittab /etc/inittab.pre_mosix" ) and
		dead "Unable to backup /etc/inittab";
	system( "$FindBin::Bin/inittab_awk < /etc/inittab.pre_mosix > /etc/inittab" ) and
		dead "Awk reported problems patching /etc/inittab" ;
	exit ;
}

sub usage {
	print "Usage : inittab [OPTION]\n";
	print "--uninstall         uninstall\n";
	print "--help              this message\n";		
	exit ;
}

sub uninstall {
	`grep mosrun /etc/inittab 2>&1` =~ /mosrun/ or
		print "\nYou do not appear to have patched /etc/inittab!\n" and
		system("/bin/rm -f /etc/inittab.pre_mosix") ,
		exit; 
	-e "/etc/inittab.pre_mosix" or
		dead "Unable to find the pre-MOSIX backup for /etc/inittab";
	system( "mv -f /etc/inittab.pre_mosix /etc/inittab" ) and
		dead "Unable to restore the pre-MOSIX back-copy of /etc/inittab";
	exit;
}
