#!/usr/bin/perl

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

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

my $arch = `uname -p`; chomp $arch;

$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";
}

$texbindirparent = "${parent}/bin";
$texmandir = "${parent}/man";
$darwinversion = `uname -r`;
$systemstring = "${arch}-apple-darwin";

opendir( TEXBINDIRPARENT, $texbindirparent) or die "Cannot open $texbindirparent\n";
@bins = sort( grep { /^$systemstring\d\.\d/ and -d "$texbindirparent/$_"} readdir( TEXBINDIRPARENT));
closedir( TEXBINDIRPARENT);

$highestbin = $bins[$#bins];
$generaltexbin = "$systemstring-current";
$highestbin = "$generaltexbin" if (-l "$texbindirparent/$generaltexbin");

$exitval = 0;

# Remove from pre-10.2 location
if (open LOGIN, "</usr/share/init/tcsh/login") {
    @loginfile = <LOGIN>;
    close LOGIN;

    for ($i=0; $i<$#loginfile; $i++) {
	if (!$startremove and $loginfile[$i] =~ /^##/ and $loginfile[$i+1] =~ /^# teTeX modifications have been added/ and $loginfile[$i+2] =~ /^##/) {
	    $startremove = $i+1;
	    $i += 2;
	}
	if (!$endremove and $loginfile[$i] =~ /^##/ and $loginfile[$i+1] =~ /^# teTeX modifications have been added/ and $loginfile[$i+2] =~ /^##/) {
	    $endremove = $i+3;
	    $i += 2;
	}
    }

    if ($startremove and $endremove) {
	print "Removing TeX entries from old-style tcsh init files\n";
	splice( @loginfile, $startremove-1, $endremove-$startremove+2, ("## TeX settings have been removed\n"));

	if (open LOGIN, ">/usr/share/init/tcsh/login") {
	    print LOGIN @loginfile;
	    close LOGIN;
	}
	else {
	    warn "Cannot write /usr/share/init/tcsh/login file to reverse patch. Run with administrator access.\n";
	    $exitval++;
	}
    }
}

&adapt_login( "/etc/csh.login", "csh");
&adapt_login( "/etc/profile", "sh");
&adapt_manpath( "/etc/manpath.config");		# 10.3
&adapt_manpath( "/usr/share/misc/man.conf");	# 10.4 and up

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

exit $exitval;

sub adapt_manpath {
    my $manpath_cfg_file = shift;
    if (open MANPATH, "<$manpath_cfg_file") {
	@manpathfile = <MANPATH>;
	close MANPATH;

	$texsetfound = 0;
	$oldtexsetfound = 0;
	if (open MANPATH, ">$manpath_cfg_file") {
	    foreach $line (@manpathfile) {
		if ($line =~ /^## TeX modifications end at/) {
		    $texsetfound = 0;
		    next;
		}
		if ($line =~ /^# teTeX modifications have been added/) {
		    $oldtexsetfound = not $oldtexsetfound;
		    next;
		}
		next if ($texsetfound or $oldtexsetfound);
		if ($line =~ /^## TeX modifications start/) {
		    $texsetfound = 1;
		    next;
		}
		print MANPATH $line;
	    }
	    close MANPATH;

	    if (not $removeonly) {
		print "$0: Applying TeX man path addition of $texmandirto $manpath_cfg_file (Works on Mac OS X 10.2 and higher)\n";
		open MANPATHCONFIG, ">>$manpath_cfg_file" or die "Cannot append manpath.config file";
		print MANPATHCONFIG "## TeX modifications start at $date## Do not remove previous line\n";
		print MANPATHCONFIG "MANPATH_MAP\t$texbindirparent/$highestbin\t$texmandir\n";
		print MANPATHCONFIG "## Do not remove next line\n## teTeX modifications end at $date##\n";
		close MANPATHCONFIG;
		print "Man path in $manpath_cfg_file set.\n";
	    }
	}
	else {
	    warn "You do not have write permissions on $manpath_cfg_file. Man path not set. Run with administrator access.\n";
	    $exitval++;
	}
    }
    else {
	print "Cannot read $manpath_cfg_file file. You are probably not running Mac OS X 10.3. Man path not set. Don't worry. I Will try different layouts/versions.\n";
    }
}

sub adapt_login {
    my $loginfilename = shift;
    my $logintype = 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;

	$texsetfound = 0;
	$oldtexsetfound = 0;
	if (open( LOGIN, ">$loginfilename")) {
	    foreach $line (@loginfile) {
		if ($line =~ /^## TeX modifications end at/) {
		    $texsetfound = 0;
		    next;
		}
		if ($line =~ /^# teTeX modifications have been added/) {
		    $oldtexsetfound = not $oldtexsetfound;
		    next;
		}
		next if ($texsetfound or $oldtexsetfound);
		if ($line =~ /^## TeX modifications start/) {
		    $texsetfound = 1;
		    next;
		}
		print LOGIN $line;
	    }
	    close LOGIN;

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

		print "$0: Applying TeX path addition of $texbindirparent/$highestbin to $loginfilename\n";
		open LOGIN, ">>$loginfilename" or die "Cannot append to login file $loginfilename. Weird!";
		print LOGIN "## TeX modifications 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} \"$texbindirparent/$highestbin\" )\n";
		    print LOGIN "endif\n";
		}
		else {
		    print LOGIN "if [ `whoami` != \"root\" ]\nthen\n";
		    print LOGIN "  PATH=\"\$PATH:$texbindirparent/$highestbin\"\n";
		    print LOGIN "  export PATH\n";
		    print LOGIN "fi\n";
		}
		print LOGIN "## Do not remove the next line\n## TeX modifications 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 $logintype login file $loginfilename. PATH has not been set.\n";
	$exitval++;
    }
}

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