
:	Unix/Tnix Version
:	Keyshell Version Number 2	*/

: This shell subroutine copies a custom ColorKey+ configuration from
: a specified user.  
: Invocation - Called from setksh which is located in $TEKPATH/tek/bin.

shelltype=;			: set to "_sh" for Bourne, "_csh" for cshell
tekpath=$TEKPATH;			: confirmed and passed in from setksh
userhome=$HOME

invalid=0
continue=
while test $invalid
do
	echo
	echo  "Enter the full pathname (start with a '/') "
	echo -n  "of the user whose custom script you want to copy: "
	read copyname
	if test "$copyname"
	then
		if test -d $copyname
		then
			invalid=
			continue=0
		else 	
			echo "Invalid user name \`$copyname'."
			echo "<CR> will return to main menu."
		fi
	else
		echo "...Copy not made..."
		exit 1
	fi
done

if test $continue
then
	if test -d $copyname/.ksh -a -f $copyname/.setKSH -a \
			-f $copyname/.setTEK
	then
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
		rm -f -r $userhome/.ksh $userhome/.setTEK $userhome/.setKSH
		trap "rm -f -r $userhome/.ksh $userhome/.setTEK $userhome/.setKSH; echo Set up has been aborted.; exit 1" 2 3
		echo "...copying ColorKey+ configuration from `basename $copyname`..."
		mkdir $userhome/.ksh
		chmod 0755 $userhome/.ksh
		cp $copyname/.ksh/* $userhome/.ksh
		cp $copyname/.setTEK $userhome/.setTEK
		cp $copyname/.setKSH $userhome/.setKSH
		chmod 0644 $userhome/.setTEK $userhome/.setKSH $userhome/.ksh/*
		trap 2 3
	else
		echo "Can't copy: invalid ColorKey+ configuration in $copyname."
		exit 1
	fi
fi
