#	.login_sh - for sh (Bourne shell)

#  Henry Grebler    12 Jan 07  More try to fix MANPATH.
#  Henry Grebler    23 Aug 06  Try to fix MANPATH.
#  Henry Grebler    17 Aug 06  Try to accommodate FC5 and FC2.
#  Henry Grebler    20 Feb 05  Only set DISPLAY under certain circumstances.
#  Henry Grebler     1 Jan 04  OpenVPN.
#  Henry Grebler    31 Oct 99  Sort out LD_LIBRARY_PATH.
#  Henry Grebler    26 Oct 99  Add /usr/local/bin_caldera to PATH.
#  Henry Grebler    26 Oct 99  Linux needs SHELL=/bin/bash.
#  Henry Grebler    23 Oct 99  Optimise PATH.
#  Henry Grebler    14 Apr 99  Turn INPUTRC off (specifically for Linux, but
#					I can't ever see it as useful).
#  Henry Grebler    12 Apr 99  Add /usr/proc/bin to PATH. Fix setting of 
#				DISPLAY.
#  Henry Grebler    18 Mar 99  Set default for LESS to '-j 10'.
#  Henry Grebler     7 Jan 99  Set MANPATH for SCO.
#  Henry Grebler    14 Sep 98  If possible, run bash from local machine.
#  Henry Grebler    26 Jun 98  Allow less to open binary files without prompt.
#  Henry Grebler    27 May 98  Only add sunos paths on suns.
#  Henry Grebler    24 Nov 97  Add /opt/java/bin and termsim stuff to path.
#  Henry Grebler     5 Aug 97  Move pass8 staff and make sure it doesn't get
#				invoked on inappropriate platforms. Perhaps
#				it should be removed.
#  Henry Grebler    21 Jan 97  Fix last change.
#  Henry Grebler    21 Jan 97  Define LESSOPEN and LESSCLOSE.
#  Henry Grebler    30 Dec 96  Define LESSCHARSET.
#  Henry Grebler    19 Nov 96  Use a modified path for bash search.
#  Henry Grebler    11 Sep 96  Add /usr/dt/man /opt/hpnp/man to manpaths.
#  Henry Grebler    10 Sep 96  Fix setting of DISPLAY if no Xserver file:
#				echo of a null variable still prints 1 line,
#				so test for null variable.
#  Henry Grebler    30 Aug 96  Don't set TAPE to a useless value.
#  Henry Grebler    15 Aug 96  Remember the received PATH.
#  Henry Grebler    30 Jul 96  Recover from dmo's changes.
#  Henry Grebler    18 Jul 96  Fix path: avoid :: and replace ~ with expansion
#				of $HOME.
#  Henry Grebler     1 Jul 96  Implement debugging of shell. Handle less
#				a bit differently (some shells send 'not found'
#				to stdout, some to stderr; all set status).
#  Henry Grebler    19 Mar 96  Make -i default for less.
#  Henry Grebler    27 Feb 96  Put /sbin in path for bash.
#  Henry Grebler     5 Oct 95  Fix pass8 error.
#  Henry Grebler    17 Jul 95  Fix bug in setting DISPLAY.
#  Henry Grebler     5 Jul 95  Try -istrip, pass8 for bawbaw.
#  Henry Grebler    20 Mar 95  Enable HMGPATH and HMGMANPATH for .bashrc.
#  Henry Grebler    16 Feb 95  Setup all the paths for OSE.
#  Henry Grebler    29 Nov 94  Set ENV for ksh correctly.
#  Henry Grebler    28 Nov 94  Set DISPLAY variable from host running
#				openwin, not host being logged into.
#  Henry Grebler    23 Nov 94  Fix test for optimation and give it new 
#				meaning: is this an OSE machine?.
#  Henry Grebler    23 Nov 94  Fix test for shell.
#  Henry Grebler    23 Nov 94  Fix test for shell; add ksh initialisation.
#  Henry Grebler    17 Oct 94  Converted from .login (for csh)

echo Starting .login_sh
##########
	if ${DEBUG-false} ;then set -xv; fi

	stty erase "^H" -parenb cs8 -istrip
	umask 007

setenv () {
	eval $1=\"$2\"; export $1
}
# alias x11 "xinit; clear"

	unset INPUTRC

#---------------------------------------------------------------------------
#	Set PATH
#---------------------------------------------------------------------------

setenv OPENWINHOME /usr/openwin     # openlook home directory
setenv X11BINARY   /usr/bin/X11     # X11r4 executables

	setenv SAVED_PATH $PATH		# Save the received PATH

if [ -d /usr/bsd ]; then setenv PATH /usr/bsd:$PATH;fi	#IRIX
if [ -d /usr/ucb ]; then setenv PATH /usr/ucb:$PATH;fi
setenv PATH '.:$HOME/scripts:$HOME/bin:'$PATH

# The next paths should come before system default:

setenv PATH ':/opt/firefox:/opt/thunderbird:'$PATH
#	paths="$paths /opt/firefox /opt/thunderbird"	


	paths="$OPENWINHOME/bin $OPENWINHOME/demo $OPENWINHOME/demo/xview
		$X11BINARY"
	paths="$paths /usr/contrib/bin"		#HP-UX


	paths="$paths /usr/local/bin /usr/sbin /etc /usr/etc
		/usr/local/sbin /usr/5bin
		/optim1/local/bin_scripts /opt/local/bin_scripts
		/usr/local/bin_scripts /opt/local/bin_optim1
		/opt/optimation/bin /opt/optimation/termsim/tools
		/usr/local/bin_optimation
		/usr/ccs/bin /usr/etc/install /sbin
		/opt/java/bin
		/usr/proc/bin
		"
	if [ `uname -n` = tsf ]
	then
		paths="$paths /usr/local/bin_*"
	fi

	if [ "`uname -sr | cut -c1-7`" = "SunOS 5" ]
	then
		paths="$paths /optim1/local/bin_sunos5 /opt/local/bin_sunos5
			/optim1/local/bin_sunos4 /opt/local/bin_sunos4
			/usr/local/bin_sunos5 /usr/local/bin_sunos4
			"
	fi

	paths="$paths /usr/X11R6/bin"
	paths="$paths /opt/*/*bin"

	for dir in $paths
	do
		if [ -d $dir ]; then setenv PATH $PATH':'$dir; fi
	done

# Optimise the path

	NEWPATH=
	for dir in `echo $PATH | tr ':' ' '`
	do
		if [ -d $dir ]
		then
			if echo $NEWPATH | grep ":$dir:" > /dev/null 2>&1
			then
				cat <<XXX
Dropping '$dir' from PATH 'coz it's already in the PATH.
XXX
			else
				NEWPATH=$NEWPATH:$dir:
			fi
		else
			cat <<XXX
Dropping '$dir' from PATH 'coz it don't exist.
XXX
		fi
#		echo $NEWPATH
	done

	PATH=$NEWPATH

# strip out any adjacent colons (:) and the first

	setenv PATH `echo $PATH | sed 's/::/:/g;s/^://'`

	setenv HMGPATH $PATH

#---------------------------------------------------------------------------
#	Set other paths
#---------------------------------------------------------------------------

# note: 7 Jan 99 - if it turns out that /usr/share/man does NOT work
# for some platform then will need to examine /usr/man and
# /usr/share/man and only include both if they are different
	manpaths="/usr/share/man $OPENWINHOME/share/man /usr/local/man \
		/work/sysadmin/sops/man"
# Mandrake Linux
	manpaths="$manpaths /usr/man /usr/X11R6/man /usr/lib/perl5/man"
	manpaths="$manpaths $OPENWINHOME/man"
	manpaths="$manpaths /opt/SUNWmfdoc/man /usr/demo/SOUND/man \
		/opt/SUNWconn/llc2/man /opt/SUNWconn/snm/man 
		/opt/SUNWconn/x25/man /opt/SUNWpcnfs/man /opt/hpnp/man
		/opt/SUNWste/license_tools/man /usr/sunlink/x25/man"
	manpaths="$manpaths /usr/share/catman"	#IRIX
	manpaths="$manpaths /usr/dt/man"	#Sun DeskTop (CDE)
	manpaths="$manpaths /opt/hpnp/man"	#optim1 hp laser4m

# (Consider /etc/man.config)
# Fedora
	manpaths="$manpaths /home/henryg/bin/PowerTools/share/man"
	manpaths="$manpaths /usr/kerberos/man /usr/share/doc/PyXML-0.8.4/man"
	manpaths="$manpaths /FC2/usr/X11R6/man /usr/local/nmh/man"
	manpaths="$manpaths /usr/local/share/man"

#set -xv
	for dir in $manpaths
	do
		if [ -d $dir ]; then setenv MANPATH $MANPATH':'$dir; fi
	done

# Optimise MANPATH

	TEMP=
	for dir in `echo $MANPATH | tr ':' ' '`
	do
		if [ -d $dir ]
		then
			if echo $TEMP: | grep ":$dir:" > /dev/null 2>&1
			then
				cat <<XXX
Dropping '$dir' from MANPATH'coz it's already in MANPATH.
XXX
			else
				TEMP=$TEMP:$dir
			fi
		else
			cat <<XXX
Dropping '$dir' from MANPATH 'coz it don't exist.
XXX
		fi
	done
	MANPATH=$HOME/help/man$TEMP
	unset TEMP
	setenv HMGMANPATH $MANPATH

#---------------------------------------------------------------------------

##########

# I'm not sure this is the right test. It seems to have started with FC5.

	if [ `uname` = Linux ]
	then
		libs="/lib /usr/lib /usr/lib/qt-3.3/lib"
	fi

libs="$libs /usr/openwin/lib /usr/dt/lib /opt/SUNWmotif/lib /usr/local/lib"
libs="$libs /usr/local/lib/lib_*"

	libs="$libs /opt/*/*lib"


	for lib in $libs
	do
		if [ -d $lib ]
		then
			LLP=$LLP:$lib
		fi
	done

	HMG_LD_LIBRARY_PATH=$LLP
	export HMG_LD_LIBRARY_PATH
	LD_LIBRARY_PATH=$HMG_LD_LIBRARY_PATH
	export LD_LIBRARY_PATH
	

#---------------------------------------------------------------------------


#	temp=`type less | fgrep -v 'not found'`
	type less >/dev/null 2>&1
	if [ $? = 0 ]
	then
		setenv PAGER less
		setenv LESS '-if -j 10'
		setenv LESSCHARSET latin1
		setenv LESSOPEN "lessopen.sh %s"
		setenv LESSCLOSE "lessclose.sh %s %s"
	fi





#---------------------------------------------------------------------------

setenv EDITOR 'emacs -nw'
#set -xv
h=`ls ./.ENV./Xserver.*`
count=0
##########
while true
do
	if [ `hostname` = tsf2 ]
	then
		if who am i | fgrep tsf500 > /dev/null
		then
			echo This is a session from tsf500
			setenv DISPLAY 'hmg:0.0'
			break
		fi
		echo Not setting DISPLAY because it upsets GNOME.
		break
	fi
	w=`echo "$h" | wc -l`
	if [ "`tty`" = /dev/console ]
	then
		echo On console. Setting display from hostname.
		setenv DISPLAY "`hostname`:0.0"
		break
	elif [ "$h" = "" ]
	then
		echo No Xserver file. Setting DISPLAY from hostname.
		setenv DISPLAY "`hostname`:0.0"
		break
	elif [ $w -eq 1 ]
	then
		echo Setting DISPLAY from Xserver file.
		setenv DISPLAY `basename $h | cut -f2 -d.`:0.0
		break
	elif [ $count -eq 0 ]
	then
		from=`who am i | cut -s -f2 -d'(' | cut -f1 -d')'`
# who am i
# henryg     pts/0        Apr 12 14:35    (bogong.mel.optimation.com.au)
		from=`echo $from | cut -f1 -d.`

		if [ "$from" != "" ]
		then
			h=`echo "$h" | fgrep $from`
			echo "Trying again with $h as 'from' server line."
		fi
		count=1
	else
		echo Giving up. Setting DISPLAY from hostname.
		setenv DISPLAY "`hostname`:0.0"
		break
	fi
done
echo DISPLAY = $DISPLAY

#---------------------------------------------------------------------------
#	A section only to be performed for terminals

	temp=`$HOME/.ENV./.login.set_terminal`
	if [ "$temp" != 0 ]
	then
		setenv TERM $temp
	fi

	if [ $TERM = xterm ]
	then
		setenv KBTERM xterm_sun4
		setenv SCTERM xterm-connect
	fi

#---------------------------------------------------------------------------
#	A section to be performed depending on whether this is an Optimation
#	machine or not

##########
	xxx=.not
	h=`hostname`

	for host in `cat $HOME/.ENV./Optimation_hosts | grep -v '^#'`
	do
		if [ $host = $h ]
		then
			xxx=""
			break
		fi
	done

	if [ "$xxx" = ".not" ]
	then
		domainname | fgrep optimation > /dev/null
		if [ $? -eq 0 ]
		then
			xxx=""
		fi
	fi

	. $HOME/.ENV./.login$xxx.optimation_sh
	unset h xxx

#---------------------------------------------------------------------------
#	A section only to be performed if on a particular platform

	temp=$HOME/.ENV./.login.`uname`
	if [ -r $temp ]; then  . $temp; fi

# I can't remember why it is here but it is causing problems on HP.

	if [ `uname -n` = bawbaw -a -x /usr/ucb/stty ]
	then
		/usr/ucb/stty pass8
	fi

#---------------------------------------------------------------------------
#	Calculate shell to use

	SHELLPATH=/sbin:/usr/local/bin:/bin:/work/home/henryg/bin
	export SHELLPATH

	cd .ENV.	# avoid finding directory ~/bash as shell
	flag=0
	if [ -f /tmp/HMGUSESHELL -a "$HMGUSESHELL" = "" ]
	then
		. /tmp/HMGUSESHELL
	fi
	shells=${HMGUSESHELL-'bash ksh csh'}
#set -xv
	for shell in $shells
	do
		temp=`PATH=$SHELLPATH type $shell | fgrep -v 'not found'`
		if [ $? = 0 ]
		then
			flag=1
			break
		fi
	done
	cd

	if [ "$shell" = ksh ]
	then
		HISTFILE=$HOME/.ENV./.sh_history_$$;export HISTFILE
		ENV=$HOME/.kshrc; export ENV
		LEVEL=0; export LEVEL
		VISUAL=emacs; export VISUAL
	fi
 
	if [ $flag -eq 1 ]
	then

# Under Linux, if SHELL=bash, su fails!

		if [ "`uname`" != Linux ]
		then
			SHELL=`echo $temp | cut -f3 -d' '`
			export SHELL
			echo Invoking $shell
			exec $shell
		else
			cat <<XXX
If this is Linux, the shell must be bash so we're done.
XXX
		fi
	fi

	echo Unable to find a shell in $shells

#---------------------------------------------------------------------------

	unset temp flag shell shells paths

echo MANPATH=$MANPATH
echo Leaving .login
