#!/bin/sh
function yesno
{
        read a?"$*"
        case $a in
                [nN]*)  false   ;;
                *)      true    ;;
        esac
}

if test -z "$1"; then
	echo $0 master_file_list
	exit 1
fi

if test "$PWD" = "/"; then
        ROOT=
else
        ROOT=$PWD
fi

OLDPATH=$PATH; PATH=;
for f in $ROOT/bin32 $ROOT/bin16 $ROOT/bin $ROOT/sbin $ROOT/usr/bin $ROOT/usr/sbin $ROOT/etc $ROOT/etc32 $ROOT/usr/etc $ROOT/usr/etc32 $ROOT/usr/libexec $ROOT/usr/local/ucb $ROOT/usr/ucb
do
	if test -d $f; then PATH=$PATH:$f; fi
done
OPATH=`echo $PATH $OLDPATH | sed -e "s,:, ," | sort | uniq -d`
if test -n "$OPATH"; then PATH=$PATH:`echo $OPATH | sed -e "s, ,:,"`; fi
unset OPATH OLDPATH

# 4.22 shipped /etc/services was flagged executable for some unknown reason
chmod a-x $ROOT/etc/services
chmod a-x $ROOT/etc/slip.login

echo Using a PATH of $PATH
for f in $(<$1); do
	case $f in
		*/etc/config*) continue ;;
		*/etc/services) continue ;;
	esac
	if test ! -f $f; then
		continue
	elif test ! -x $f; then
		continue
	fi
	for path in $(which -a `basename $f` 2>/dev/null)
	do
		if test "$(fullpath -t $path)" = "$(fullpath -t ${ROOT}${f})" ; then
			continue
		elif test "$path" = "$ROOT/usr/bin/sendmail"; then
			continue
		else
				echo "\nYou have an old version of the file $path"
				rm -i $path
				if test $? -eq 1; then
					echo "\nYou chose not to remove the file from the non-standard location,"
					echo "this may cause you problems later on."
					continue
				fi
				echo
		fi
	done
done

if test -r $ROOT/usr/tcpip; then
	# this has been replace by /usr/tcprt/current, which now points to the current hierarchy
	rm -fv $ROOT/usr/tcpip
fi

chmod a+x $ROOT/etc/slip.login

for f in $ROOT/usr/bin/mailq $ROOT/usr/bin/newaliases
do
	if test -L $f; then
		rm -fv $f
	fi
done

SAVEPATH=$ROOT/usr/tcprt/old
if test -f $ROOT/etc/readme/R-ftp-telnet; then
	mkdir -p $SAVEPATH/etc/readme
	mv -v $ROOT/etc/readme/R-ftp-telnet $SAVEPATH/etc/readme/
fi

for f in $ROOT/usr/ucb/nfsiod $ROOT/usr/ucb/rpc.pcnfsd
do
	if test -f $f; then
		mkdir -p $SAVEPATH/usr/ucb
		mv -v $f $SAVEPATH/usr/ucb
	fi
done

# For some reason tn3270 4.22 is newer than later versions
ln -fvs /usr/tcprt/current/usr/ucb/tn3270 $ROOT/usr/ucb/tn3270 >> $2 2>&1

#
# check executables in /etc/inetd.conf
#
fileList=/tmp/fileList
confFile=$ROOT/etc/inetd.conf
tmpFile=/tmp/tmpFile
customized="no"
oldConf=$ROOT/usr/tcprt/old/etc

echo "\nChecking $confFile..."
mkdir -p $oldConf
cp -f $confFile $oldConf
awk '!/#/{print $6}' $confFile | sort -u -o $fileList
for f in $(<$fileList); do
	if test "$(basename $f)" = "$f"; then
		continue;
	fi

	if test ! -x $ROOT$f; then
		nf=/usr/ucb/$(basename $f)
		if test -x $ROOT$nf; then
			echo WARNING! $confFile points to $f, which was replaced by $nf
			if yesno "Update $confFile [yn]? "; then
				echo
				f=$(echo $f | sed -e 's/\//\\\//'g)
				nf=$(echo $nf | sed -e 's/\//\\\//'g)
				sed -e "s/$f/$nf/" $confFile > $tmpFile
				mv -f $tmpFile $confFile
			fi
		else
			echo WARNING! $confFile points to $f, which no longer exists
		fi
		customized="yes"
	fi
done

if test $customized = "yes"; then
	echo "The default inetd.conf file can be found in $ROOT/etc/config/socket."
	echo "Your original inetd.conf file can be found in $oldConf."
fi

rm -f $fileList

echo "Completed checking $confFile"
