#!/bin/sh -- need to mention perl here to avoid recursion
'true' || eval 'exec perl -S $0 $argv:q';
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
& eval 'exec /usr/local/bin/perl -S $0 $argv:q'
	if 0;

#  Usage: [perl] reconfig [file]
#
#   This replaces the program paths (e.g. /bin/awk) in SAINT with an
# alternate path that is found in the file "file.paths".  It also finds
# perl5 (or at least tries!) and changes the path in all the stand-alone
# perl programs.
#

# all the HTML browsers we know about, __IN ORDER OF PREFERENCE__!!!
@all_www= ("netscape", "mosaic", "xmosaic", "lynx", "chimera");

#
#  Potential directories to find commands; first, find the user's path...
$PATH = $ENV{"PATH"};

# additional dirs; *COLON* separated!
$other_dirs="/usr/ccs/bin:/bin:/usr/bin:/usr/ucb:/usr/bsd:/usr/ucb/bin:/usr/sbin:/usr/etc:/usr/local/bin:/usr/bin/X11:/usr/X11/bin:/usr/openwin/bin:/usr/local/samba/bin";

#
# split into a more reasonable format. Personal aliases come last.
@all_dirs = split(/:/, $other_dirs . ":" . $PATH);

#
#  Target shell scripts in question:
@shell_scripts=("config/paths.pl", "config/paths.sh");
@perl5_src = <bin/get_targets bin/faux_fping saint bin/*.saint perl/html.pl bin/fwping scripts/saint.cgi>;

#
#  Target shell commands in question
@all_commands=("cc", "cat", "chmod", "cmp", "comm", "cp", "date", "diff",
	"egrep", "expr", "find", "grep", "ls", "mail", "mkdir", "mv", "rm",
	"sed", "sh", "sort", "tftp", "touch", "uniq", "uudecode", "ypcat",
	"strings", "finger", "ftp", "rpcinfo", "rusers", "showmount", "ping",
	"ypwhich", "nslookup", "dig", "xhost", "su", "awk", "sed", "test", "whoami", 
	"basename", "echo", "file", "rlogin", "rsh", "stty", "mkfifo", "mknod",
	"nmap", "nmblookup", "smbclient", "rup");

print "Reconfiguring...\n";
print "Checking to make sure all the targets are here...\n";

for (@shell_scripts) {
	die "ERROR -- $_ not found!\n" unless -f $_;
	}

# find perl5!
print "Trying to find Perl...";
for $dir (@all_dirs) {
	# first, find where it might be; oftentimes you'll see perl,
	# perl4, perl5, etc. in the same dir
	next if (! -d $dir);
	while (<$dir/perl5* $dir/perl*>) {
		if (-x $_) {
			$perl_version=`($_ -v 2> /dev/null) |
				awk '
				    /This is perl, version 5/ { print $NF }
				    /This is perl, v5/ { print $4 }
				'`;
			if ($perl_version) {
				$PERL=$_;
				$pflag="1";
				last;
				}
			}
			last if $pflag;
		}
	last if $pflag;
	}

die "\nCan't find Perl!  Bailing out...\n" unless $PERL;
print " $PERL\n";

for (@perl5_src) { $perl5_src .= "$_ "; }
print "Changing the source in PERL scripts...\n";
system "$PERL -pi -e \"s@^#!.*/perl.*@#!$PERL@;\" $perl5_src";

# make sure things are executable...
system("chmod u+x $perl5_src");
 
# find the most preferred www viewer first.
print "Trying to find HTML/WWW browser...";
for $www (@all_www) {
	for $dir (@all_dirs) {
		if (!$MOSAIC) {
			if (-x "$dir/$www") {
				$MOSAIC="$dir/$www";
				next;
				}
			}
		}
	}
if ($MOSAIC) {
	print " $MOSAIC\n";
	$upper{"MOSAIC"} = $MOSAIC;
	}
else { print "\nCannot find a web browser!  SAINT cannot be run from GUI\n"; }

print "Looking for UNIX commands...\n";

for $command (@all_commands) {
	$found="";
	$no_mkfifo=0;
	for $dir (@all_dirs) {
		# special case rsh/remsh; if we can find remsh, ignore rsh
		if ($command eq "rsh") {
			# print "Looking for rsh/remsh ($dir/$command)\n";
			if (-f "$dir/remsh") {
				# this converts to upper case
				($upper = $command) =~ y/[a-z]/[A-Z]/;
				$found="true";
				$upper{$upper} = "$dir/remsh";
				print "Found $dir/remsh; using this instead of rsh\n";
				last;
				}
			}

		# if find the command in one of the directories, print string
		if (-f "$dir/$command") {
			# this converts to upper case
			($upper = $command) =~ y/[a-z]/[A-Z]/;
			$found="true";
			$upper{$upper} = "$dir/$command";
			# print "found ($upper) $dir/$command\n";

			# if it's rsh we're examining, keep looking; else quit
			last unless $command eq "rsh";
			}
		}
	   if (!$found) {
	     print "Can't find $command";
	     print " (okay on Linux, will compile it)" if ($command eq "showmount");
	     print " (okay, will use nslookup)" if $command eq "dig";
	     if ($command eq "mkfifo") {
	       print " (okay, will use mknod)";
	       $no_mkfifo = 1;
	     }
	     if ($command eq "mknod") {
	       $no_mkfifo && print " (-w option will not work)"
	       || print " (okay, will use mkfifo)";
	     }
	     print "\n";
	   }
	}

print "Doing substitutions on the shell scripts...\n";
for $shell (@shell_scripts) {
 	print "Changing paths in $shell...\n";
	die "Can't open $shell\n" unless open(SCRIPT, $shell);
	rename($shell, $shell . '.old');
	die "Can't open $shell\n" unless open(OUT, ">$shell");

	#
	#  Open up the script, search for lines beginning with
	# stuff like "TEST", "AWK", etc.  If the file ends in "pl",
	# assume it's a perl script and change it accordingly
	while (<SCRIPT>) {
		$found = 0;
		for $command (keys %upper) {
			if(/^\$?$command=/) {
				# shell script
				if ($shell !~ /.pl$/) {
					print OUT "$command=$upper{$command}\n";
					}
				# perl script
				else {
					print OUT "\$" . "$command=\"$upper{$command}\";\n";
					}
				$found = 1;
				}
			}
		print OUT $_ if !$found;
		}
	close(SCRIPT);
	close(OUT);
	}

# Substitute our path into saint.cgi
chomp($pwd = `pwd`);
rename('scripts/saint.cgi', 'scripts/saint.cgi.old');
open(SCRIPT, "< scripts/saint.cgi.old") || die "cannot open saint.cgi.old";
open(OUT, "> scripts/saint.cgi") || die "cannot open saint.cgi";
while(<SCRIPT>) {
    s/%saint_dir%/$pwd/;
    print OUT;
    }
close SCRIPT;
close OUT;
unlink "scripts/saint.cgi.old";
chmod 0755, "scripts/saint.cgi";

# done...

