#!/usr/bin/perl

$ENV{PATH} = "/usr/bin:/bin:/usr/sbin:/sbin";

my $scriptdirname = $0;
my $pathtoadd = $ARGV[0];
my $removearg = $ARGV[1];

$removeonly = $removearg =~ /^remove$/;

($scriptdirname) = $scriptdirname =~ /([A-Za-z\._\-\/ ]+)/;
$scriptdirname=`/usr/bin/dirname $scriptdirname`;
chomp( $scriptdirname);
if ($scriptdirname ne "") {
    chdir( "$scriptdirname") or die "Cannot chdir to $scriptdirname\n";
}
else {
    warn "Empty scriptdirname \"$scriptdirname\"\n";
}

# $installdir = `/bin/pwd`;
# chomp( $installdir);
# ($installdir) = $installdir =~ /([A-Za-z\._\-\/ ]+)/;
# die "Not enough depth or problem characters\n" unless $installdir =~ m'(/[^/]+){2}';
# ($installdir,undef) = $installdir =~ m'(.+)(/[^/]+){2}';

$exitval = 0;

&adapt_login( "/etc/csh.login", "csh", $pathtoadd);
&adapt_login( "/etc/profile", "sh", $pathtoadd);

if ($exitval) {
    warn "There were $exitval errors. Please read the warning messages,\n";
}

exit $exitval;

sub adapt_login {
    my $loginfilename = shift;
    my $logintype = shift;
    my $directory = shift;

    if ($logintype ne "csh" and $logintype ne "sh") {
	warn "Cannot determine proper login type of $loginfilename ($logintype??).\n";
	$exitval++;
	return;
    }

    if (open LOGIN, "<$loginfilename") {
	@loginfile = <LOGIN>;
	close LOGIN;

	$setloginpathfound = 0;
	if (open( LOGIN, ">$loginfilename")) {
	    foreach $line (@loginfile) {
		if ($line =~ /^## setloginpath added $directory end at/) {
		    $setloginpathfound = 0;
		    next;
		}
		next if ($setloginpathfound);
		if ($line =~ /^## setloginpath added $directory start/) {
		    $setloginpathfound = 1;
		    next;
		}
		print LOGIN $line;
	    }
	    close LOGIN;

	    if (not $removeonly) {
		$date = `date`;

		print "$0: Applying path addition of $directory to $loginfilename\n";
		open LOGIN, ">>$loginfilename" or die "Cannot append to login file $loginfilename. Weird!";
		print LOGIN "## setloginpath added $directory start at $date## Do not remove the previous line\n";
		if ($logintype eq "csh") {
		    print LOGIN "if (\"\${uid}\" != \"0\") then\n";
		    print LOGIN "  set path = ( \${path} $directory )\n";
		    print LOGIN "endif\n";
		}
		else {
		    print LOGIN "if [ `whoami` != \"root\" ]\nthen\n";
		    print LOGIN "  PATH=\"\$PATH:$directory\"\n";
		    print LOGIN "  export PATH\n";
		    print LOGIN "fi\n";
		}
		print LOGIN "## Do not remove the next line\n## setloginpath added $directory end at $date";
		close LOGIN;
	    }
	}
	else {
	    warn "You do not have write permissions on $loginfilename. PATH has not been set. Run with administrator access.\n";
	    $exitval++;
	}
    }
    else {
	warn "Cannot read system-wide csh login file $loginfilename. PATH has not been set.\n";
	$exitval++;
    }
}

# $Id: setloginpath,v 1.1.1.1 2004/02/03 21:36:11 gctwnl Exp $
