#! /bin/sh
#
#  Usage: reconfig
#
#   This replaces the program paths (e.g. /bin/awk) in COPS with an
# alternate path that is found in the file "file.paths".
#   All programs are renamed "name.old", and the new version replaces
# the original name.  It then uses sed to replace all occurances of
# the target strings.
#   Basically, the program looks through all directories listed in
# $all_dirs for the list of programs in $all_commands and when it finds
# them, puts them in a sed source file.  It then goes to all of the
# shell files COPS uses ($shell_scripts) and replaces all occurances of
# the variables found with the new value.  It goes through some
# contortions trying to look for test (it has to find test without
# using test), and does some other not so smart things, but it seems
# to get the job done.

# shell is always here, isn't it?
SH=/bin/sh

# need these later
TEST=
AWK=
SED=
STRINGS=
YPCAT=

#  Potential directories to find commands:
all_dirs='/bin /usr/bin /usr/ucb /usr/local/bin'

#   First things first; are test and echo built-in shell commands?
# Theory.  If test is executed correctly and not found in the path
# I set, then they should be built into the shell, right?
PATH=/bin:/usr/bin
for dir in $all_dirs
	do
	if test -f $dir/test
		then
		TEST=$dir/test
		break
	fi
	done
# if not set, then set to default
if test -z "$TEST"
	then
	TEST=test
	fi

for dir in $all_dirs
	do
	if $TEST -f $dir/echo
		then
		ECHO=$dir/echo
		break
	fi
	done

# if not set, then set to default
if $TEST -z "$ECHO"
	then
	ECHO=echo
	fi

# The sed filter file
location=./file.paths

#  Target shell scripts in question:
shell_scripts="makefile chk_strings cops dev.chk dir.chk file.chk cron.chk group.chk passwd.chk rc.chk root.chk suid.chk kuang init_kuang"

#  Target commands in question, sans those checked above:
all_commands='cc nroff mkdir awk sed sh cat chmod comm mv date egrep cp expr find grep ls mail rm sort uniq'
rest_of_them=' echo test'

#  make sure everything is here:
for i in $shell_scripts
	do
	if $TEST ! -s $i
		then
		$ECHO  ERROR -- Shell script $i not found!
		exit
	fi
done

#   This finds the paths to any program used in COPS, then prints out
# a sed filter to the file "file.paths" that is used by this shell
# script to change all occurances of that command in the COPS system.
#
#   For example, if sed is in /usr/bin, it will create a line that looks
# like this:
#
# s.AWK=*$.AWK=/usr/bin/sed.
#
#  This corresponds to the sed command substitute ("." is used as a
# delineator instead of "/" because the strings will be containing
# "/"'s) /usr/bin/sed in place of whatever was to the right of the
# equal sign.  This works because all commands are accessed by the
# variable "$XYZ", where "XYZ" corresponds to the lowercase command
# "xyz".  And, of course, all command variables are set at the top
# of each command file.
#

# First we need awk and sed if this shell script will work....
for dir in $all_dirs
	do
	if $TEST -f $dir/awk ; then
		AWK=$dir/awk
		fi
	if $TEST -f $dir/sed ; then
		SED=$dir/sed
		fi
	done
if $TEST -z "$AWK" ; then
	$ECHO "Cannot find awk; awk is needed to run this shell script"
	exit 1
	fi

if $TEST -z "$SED" ; then
	$ECHO "Cannot find sed; sed is needed to run this shell script"
	exit 1
	fi

# zero out the file, then put in the real locations...
$ECHO > $location

for command in $all_commands
	do
	found=false
	for dir in $all_dirs
		do
		# if find the command in one of the directories, print string
		if $TEST -f $dir/$command
			then
			# this converts to upper case
			upper=`$ECHO $command | $AWK 'BEGIN{upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ";lower="abcdefghijklmnopqrstuvwxyz";} \
			{out = ""; for(i=1;i <= length($1);i++) {
				if((cpos = index(lower,c = substr($1,i,1))) > 0)
					c = substr(upper,cpos,1); \
				out = out c; \
				} printf("%s",out)}'`

		$ECHO "s-^$upper=.*\$-$upper=$dir/$command-" >> $location
			found=true
			break
			fi
		done
	if $TEST "$found" = "false"
		then
		$ECHO ERROR!  $command not found!  Change or delete command!
		exit
		fi
	done

$ECHO "s-^ECHO=.*\$-ECHO=$ECHO-" >> $location
$ECHO "s-^TEST=.*\$-TEST=$TEST-" >> $location

# almost forgot -- we need chmod, mv too:
for dir in $all_dirs
	do
	if $TEST -f $dir/mv ; then
		MV=$dir/mv
		fi
	if $TEST -f $dir/chmod ; then
		CHMOD=$dir/chmod
		fi
	done

for i in $shell_scripts
	do
	$ECHO "Changing paths in $i..."
	$SED -f $location $i > $i.new
	$MV $i $i.old
	$MV $i.new $i
	# finally, make sure everything is back to executable status
	$CHMOD 700 $i

done
