#!/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 slocate cron daemon job, to ignore mfs.

# Default arguments currently compatible with all RedHat distribtuions.

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

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

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

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

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