#!/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 inet daemon configuration file, to unlock and
# allow migration of shells.

# Default arguments currently compatible with all distributions using inet.
# Note: RedHat 7.x does not use the inet daemon.

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

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

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

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

sub uninstall {
	`grep mosrun /etc/inetd.conf 2>&1` =~ /mosrun/ or
		print "\nYour inet-daemon configuration file (/etc/inetd.conf)\n" and
		print "was not previously patched for MOSIX!\n" and
		exit ;
	-e "/etc/inetd.conf.pre_mosix" or
		dead "Unable to find the pre-MOSIX backup for /etc/inetd.conf";
	system( "mv -f /etc/inetd.conf.pre_mosix /etc/inetd.conf" ) and
		dead "Unable to restore the pre-MOSIX back-copy of /etc/inetd.conf";
	exit;
}
