#!/bin/sh
# 
# This little shell program searches the path for Perl, and once
# it finds it, it substitutes the path into all the Perl executables
# in the tree. It is intended to be run as part of the install process.
# 
# The install guide (on the syssumm web pages) should be updated
# to reference this shell script which will, in turn, invoke the
# install.pl Perl script.
# 
# ==============================================================
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 
# 2 of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be 
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
# 
# Original source:
#	Copyright (c) 1999, 2000 Frank Crawford
# 
# ==============================================================
# 
# $Header: /export/home/mohlerb/syssumm/web.server/RCS/install,v 1.2 2000/05/04 21:10:50 mohlerb Exp mohlerb $
# 
# $Revision: 1.2 $
#
# $Log: install,v $
# Revision 1.2  2000/05/04 21:10:50  mohlerb
# changes from Bruce Mohler including:
# in the findPerl function, if we have to search down through
# the $PATH variable for perl, make sure we pick up the binary,
# not a symbolic link to the binary; also, leave behind a small
# file that the install.pl Perl script can detect to make sure
# we're executing the install script rather than install.pl
#
# Revision 1.1  2000/04/04 03:27:57  mohlerb
# Initial revision
#
# 

defperl="/usr/bin/perl"

isPerl5 () {
	$1 -e 'require 5;' > /dev/null 2>&1
}
 
findPerl () {
	for i in `echo $PATH | tr ':' ' '`
	do
		for p in perl perl5
		do
			# -f test added to make sure we're not picking up sym links
			if test -x $i/$p && ! test -L $i/$p && isPerl5 $i/$p ; then
				echo $i/$p
				return 0
			fi
		done
	done
}

process () {
	dir=$1
 
	for i in $dir/*
	do
		if [ -d $i ]; then
			# recurse into directories (note: there's no guarantee
			# that $dir will be set right after this...)
			process $i
		elif [ -x $i ]; then
			echo "    $i"
			$perl -i.bak -pe "s%^#! ?${defperl}%#!$perl%" $i
			chmod +x $i
			rm -f $i.bak
		fi
	done
}

# leave a small file behind so that we know that we've run 
# the install shell script
> ./.ran.install

perl=`findPerl`
if [ ! -z "$perl" ]; then
	echo "Congratulations, you have Perl 5 installed as $perl"
	echo
else
	echo "You don't seem to have Perl 5 in your path."
	echo "Either fix your path to include your perl, or"
	echo "visit http://www.perl.com, install Perl 5, and"
	echo "try again."
	exit 1
fi

if [ "$perl" != "$defperl" ]; then
	echo "Fixing executables to use your Perl binary."
	echo
	chmod +x *.pl
	process .
else
	echo "No need to process files, since your Perl is in a standard place."
fi

echo
 
if [ ! -d RCS -a -f MANIFEST.remote ]; then
	echo "Updating MANIFEST"
	rm MANIFEST
	mv MANIFEST.remote MANIFEST
fi

echo "Invoking the real install."
exec ./install.pl "$@"
 
exit 1
