#!/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 installs the mosix.init file in the init scripts directory, 
# and creates the symlinks to all the run-levels. 

# Default arguments currently compatible with RedHat and SuSE distributions.

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

$up_runlevels		= "2345";
$down_runlevels		= "016"; 

GetOptions(	"rcd=s" ,		\$rcd ,
		"initd=s" ,		\$initd ,
		"up_runlevels=s" ,	\$up_runlevels ,
		"down_runlevels=s" ,	\$down_runlevels ,
		"help" ,		\$usage ,
		"uninstall" ,		\$uninstall );

verify_initd_rcd();

$rcd =~ s/\/$// ;
$base_rc = $rcd;
$initd =~ s/\/$// ;
$base_init = $initd;

usage() if defined $usage ;
foreach $i(0..6) {
	-w "$base_rc/rc$i.d" or dead "\nYou do not have write permission in '$base_rc/rc$i.d'";
}
-e $base_init or
-e "$base_init/../killall" and $base_init .= '/..' or
	dead "Unable to find init directory ($base_init)";
uninstall() and exit 1 if defined $uninstall ;
-e "$source_dir/mosix.init" or dead "MOSIX Distribtuion is not intact: Unable to find '$source_dir/mosix.init'";
install();
exit 1;
    
sub install
{
	system( "cp -f $source_dir/mosix.init $base_init/mosix" ) and
		dead "Failed copying '$source_dir/mosix.init' to '$base_init/mosix'";
	chmod 0755 , "$base_init/mosix" or
		dead "Failed to change the mode of '$base_init/mosix' to rwxr-xr-x";
	foreach $runlevel( split //, $up_runlevels ) {
		system( "ln -sf $base_init/mosix $base_rc/rc$runlevel.d/S95mosix" ) and
			dead "Unable to link '$base_init/mosix' to '$base_rc/rc$runlevel.d/S95mosix'";
	}
	foreach $runlevel(0..6) {
		if(`ls $base_rc/rc$runlevel.d/K??network 2>/dev/null` =~ /K\d\dnetwork/) {
			$down_runlevels="$down_runlevels$runlevel";
		}
	}
	foreach $runlevel( split //, $down_runlevels ) {
		system( "ln -sf $base_init/mosix $base_rc/rc$runlevel.d/K05mosix" ) and
			dead "Unable to link $base_init/mosix to $base_rc/rc$runlevel.d/K05mosix" ;
	}

	-f "/sbin/chkconfig" and system( "/sbin/chkconfig --add mosix" ) and
		print "Unable to 'chkconfig --add mosix'\n";
	exit ;
}

sub usage {
	print "Usage : install_rc [OPTION]\n";
	print "--rcd=              the base rc directory (default: /etc/rc.d)\n";
	print "--initd=            the base init directory (default: /etc/rc.d/init.d)\n";
	print "--up_runlevels=     the run levels in which MOSIX is to be up (default: 2345)\n";
	print "--down_run_levels=  the run levels in which MOSIX is to be brough down (default: 016)\n";
	print "--uninstall         uninstall\n";
	print "--help              this message\n";		
	exit ;
}

sub uninstall {
	-e "$base_init/mosix" or
		print "\nYou do not appear to have installed the MOSIX init file!\n" and
		exit ;
	-f "/sbin/chkconfig" and system( "/sbin/chkconfig --del mosix" ) and
		print "Unable to 'chkconfig --del mosix'\n";
	system( "rm -f $base_init/mosix" ) and
		dead "Unable to delete '$base_init/mosix'";
	foreach $runlevel(0..6) {
		system( "rm -f $base_rc/rc$runlevel.d/".'*mosix' ) and
			print "Unable to delete mosix links from run level $runlevel";
	}
	exit ;
}
