
:	TNIX/UNIX Version
:	Keyshell Version Number 2	*/

:
: build    23 sep 1983
:
: This shell procedure allows a user to set up a ColorKey+ environment.
: It will either set up the default configuration or build a custom
: script, depending on the external variable ACTION.
: Invocation - called from setksh.

: External variables set in setksh
:	$TEKPATH -	pathname to Tektronix directory .../tek
:	$ACTION -	either CUSTOM or DEFLT
:       $idir		include directory of subroutines

: Set some locally used variables
if test "$ACTION" = "CUSTOM"		
then					
	custom=1
else
	custom=
fi
shelltype=;			: set to "_sh" for Bourne, "_csh" for cshell
tekpath=$TEKPATH;			: passed in from setksh
userhome=$HOME

if test "$TNIX" = "yes" ; then
	shelltype=_sh
else
: Get the user\'s login shell and set $shelltype.
	cat << !!

ColorKey+ runs under the Bourne shell (regular shell 'sh') or Cshell ('csh').
Please specify which shell is running when you log on:

	1 - 	Bourne shell
	2 -	Cshell

Enter your choice: 
!!
	looptest=0
	while test $looptest
	do
		read INPUT
		case $INPUT in
			1)	looptest=
				shelltype=_sh;;
			2)	looptest=
				shelltype=_csh;;
			*)	echo "Invalid input: Please enter '1' or '2'."
				echo
				echo -n "Enter your choice: ";;
		esac
	done
fi


	
:
: Check for the string to call $HOME/.setTEK in either .profile
: or .login, depending upon which shell the user has chosen.  If the
: string is found, we will assume that the user\'s .profile of .login
: has the necessary initialization for ColorKey+.
:
move=;				: if set, .profile or .login must be copied
protect=;			: if set, protect existing .profile or .login
if test  -f $userhome/.profile -a "$shelltype" = "_sh"
then
	fgrep -s '. $HOME/.setTEK' $userhome/.profile
	if test "$?" != "0"
	then 
		move=0
		protect=0
	fi

elif test -f $userhome/.login -a "$shelltype" = "_csh"
then
	fgrep -s 'source $HOME/.setTEK' $userhome/.login
	if test "$?" != "0"
	then 
		move=0
		protect=0
	fi
else
	move=0
fi
if test $protect
then
	cat <<!!

Your existing .profile(.login) does not contain the necessary commands
to initialize ColorKey+ when you log in.  Shall a .profile(.login) 
with the necessary commands placed in your home directory? (y or n)

If you answer 'y':	Your current .profile(.login and .cshrc) will be 
			saved under the name .oldprofile(.oldlogin and 
			.oldcshrc).  The new .profile (.login) will be 
			placed in your home directory.

If you answer 'n':	Your current .profile(.login and .cshrc) will not 
			be changed, this procedure will continue to configure 
			your account for ColorKey+ and you will have to 
			properly initialize ColorKey+ in some other manner.
			
Please enter your answer ('y' or 'n'):
!!
	invalid=0
	while test $invalid
	do
	read INPUT
	case $INPUT in
	Y | y)
		case $shelltype in
			_sh)
		  		rm -f $userhome/.oldprofile
				echo "...moving '.profile' --> '.oldprofile'..."
		  		mv $userhome/.profile $userhome/.oldprofile
			;;
			_csh)
		  		rm -f $userhome/.oldlogin
				echo "...moving '.login' --> '.oldlogin'..."
		  		mv $userhome/.login $userhome/.oldlogin
			;;
		esac
		invalid=
		;;
	N | n)	move=
		invalid=
		;;
	*)	
		echo
		echo -n "Invalid input.  Please enter (y) for yes or (n) for no: "
		;;
	esac
	done
fi

if test $move			
then
	case $shelltype in
	_sh)
	  	echo "...writing new '.profile' to login directory..."
	  	cp $idir/default.profile $userhome/.profile
		chmod 0644 $userhome/.profile
	;;	
	_csh)
		if test -f $userhome/.cshrc ; then
			echo "...moving '.cshrc' --> '.oldcshrc'..."
		  	mv $userhome/.cshrc $userhome/.oldcshrc
		fi
	  	echo "...writing new '.login' to login directory..."
	  	cp $idir/default.login $userhome/.login
		chmod 0644 $userhome/.login
	;;
	esac
fi

: open temporary file $setTEK to receive TEK initialization commands
setTEK=/tmp/set$$
echo "" >$setTEK

case $shelltype in
_sh)
	if test "$TNIX" = "yes" ; then
		echo "TNIX=yes; export TNIX" >>$setTEK
		echo "USER=`basename $userhome`; export USER" >>$setTEK
	else
		echo "TEKPATH=$tekpath; export TEKPATH" >>$setTEK
	fi
	;;

_csh)
	echo "setenv TEKPATH $tekpath" >>$setTEK
	;;
esac

klib=$tekpath/tek/ksh


: If it\'s a custom job, get the options and build
if test $custom		
then
	kdir=\\\$HOME/.ksh
	kppdefs=/tmp/kpp$$
	echo -n "" >$kppdefs
	echo "#define _kdir $kdir" >>$kppdefs
	if test "$TNIX" = "yes" ; then
		echo "#define _TNIX 1" >>$kppdefs
	fi
	echo "#define _UNIX 1" >>$kppdefs
	kshconfig=/tmp/ksh$$
	echo "" >$kshconfig
	echo "ColorKey+ is currently configured as follows:" >>$kshconfig
	echo "" >>$kshconfig
	. $idir/kshoptions
	echo -n "...Now creating the custom ColorKey+ script..."

	kshdir=/tmp/kd$$
	mkdir $kshdir
	trap "rm -f -r $kshdir $kshconfig $kppdefs $setTEK; \
		echo Set up has been aborted.; exit 1" 2 3
	(
	cat $kppdefs $klib/scripts/main | \
	$tekpath/tek/bin/kpp \
 	-I$klib/scripts/include\
 	-o $kshdir/main.s\
 	-h $kshdir/main.n\
 	>/dev/null 2>/dev/null

	if test "$TNIX" = "yes" -a -n "$iunit" ; then
		cat $kppdefs $klib/scripts/dbug | \
		$tekpath/tek/bin/kpp \
 		-I$klib/scripts/include\
 		-o $kshdir/dbug.s\
 		-h $kshdir/dbug.n\
 		>/dev/null 2>/dev/null

		if test $tta ; then
			cat $kppdefs $klib/scripts/tta | \
			$tekpath/tek/bin/kpp \
 			-I$klib/scripts/include\
 			-o $kshdir/tta.s\
 			-h $kshdir/tta.n\
 			>/dev/null 2>/dev/null
		fi

		if test $ddl ; then
			cat $kppdefs $klib/scripts/ddl | \
			$tekpath/tek/bin/kpp \
 			-I$klib/scripts/include\
 			-o $kshdir/ddl.s\
 			-h $kshdir/ddl.n\
 			>/dev/null 2>/dev/null
		fi
	fi
	rm -f $kppdefs)&

	: print a few dots
	while test -f $kppdefs			
	do
		echo -n ' .'
		sleep 7
	done
	echo
	echo "...The ColorKey+ script has been created..."
trap 2 3
fi

echo "...Writing ColorKey+ initialization files to login directory..."
rm -f -r $userhome/.setKSH
rm -f -r $userhome/.ksh
rm -f -r $userhome/.setTEK 

case $shelltype in
	_sh)
		if test "$TNIX" = "yes" ; then
			:
		else
			echo "PATH=:\$TEKPATH/tek/bin:\$PATH; export PATH" >>$setTEK
		fi
		cp $idir/setKSH $userhome/.setKSH
		chmod 0644 $userhome/.setKSH
	;;
	_csh)
		echo "setenv PATH :\$TEKPATH/tek/bin:\$PATH" >>$setTEK
		cp $idir/csetKSH $userhome/.setKSH
		chmod 0644 $userhome/.setKSH
	;;
esac
	
mv $setTEK $userhome/.setTEK
chmod 0644 $userhome/.setTEK $userhome/.setKSH

if test $custom
then
	mkdir $userhome/.ksh
	chmod 0755 $userhome/.ksh
	cp $kshdir/* $userhome/.ksh
	rm -f -r $kshdir
	mv $kshconfig $userhome/.ksh/kshconfig
	cp $klib/deflt/help*  $userhome/.ksh
	chmod 0644 $userhome/.ksh/*
fi


echo

