:
# NAME:
#	profile - global initialization for sh,ksh
#
# DESCRIPTION:
#	This file is processed during login by /bin/sh
#	and /bin/ksh.  It is used to setup the default user
#	environment.
#
#	It may look hideously complex, but most of it is shell
#	internals so its not too slow...
#
# SEE ALSO:
#	$HOME/.profile
#	/etc/ksh.kshrc
#
# AUTHOR:
#	Simon J. Gerraty <sjg@crufty.net>

# RCSid:
#	$Id: profile,v 1.35 2002/01/06 08:50:25 sjg Exp $
#
#	@(#)Copyright (c) 1991 Simon J. Gerraty
#
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to copy, redistribute or otherwise
#	use this file is hereby granted provided that 
#	the above copyright notice and this notice are
#	left intact. 

set +u

sigs="2 3"
trap "" $sigs	# don't interrupt us

# simple versions. See add_path.sh for the clever ones,
# we could source add_path.sh here, but these are ok and guaranteed to
# be here 
add_path () { [ -d $1 ] && eval ${2:-PATH}="\$${2:-PATH}:$1"; }
pre_path () { [ -d $1 ] && eval ${2:-PATH}="$1:\$${2:-PATH}"; }
del_path () { eval ${2:-PATH}=`eval echo :'$'${2:-PATH}: | 
	sed -e "s;:$1:;:;g" -e "s;^:;;" -e "s;:\$;;"`; }

ask () { echo $N "${2:-$1?} "$C; read $1; }
	
_SOURCE_SH=:

# these are from rc.sh, but others can use them too.
Which() {
	case "$1" in
	-*) t=$1; shift;;
	*) t=-x;;
	esac
	case "$1" in
	/*)	test $t $1 && echo $1;;
	*)
		for d in `IFS=:; echo ${2:-$PATH}`
		do
			test $t $d/$1 && { echo $d/$1; break; }
		done
		;;
	esac
}

# handy for finding include files
source() {
	f=$1; shift
	case $f in
	:|break) x=$f; f=$1; shift;;
	*) x=break;;
	esac
	for d in `IFS=:; echo ${*:-$PATH}`
	do
		test -s $d/$f && { . $d/$f; $x; }
	done
}

# Doh! telnet on solaris 2.6 passes _INIT_ along,
# so we need to detect if it came from this host
# or not...
case "$_INIT_" in
*:${HOSTNAME}:*) ;;
*) _INIT_=;;
esac

# first check if we are in single user...
case "$_INIT_" in
"")
	if [ -d /usr/bin ]; then
		run_level=`(who -r) 2>/dev/null`
		case "$run_level" in
		*run-level*)
			run_level=`set -- $RUN_LEVEL; echo $3`
			case "$run_level" in
			S|5|0)	_INIT_=single;;
			esac
			;;
		esac
		unset run_level
	else
		# if /usr/bin is missing we had better be in single user
		_INIT_=single
	fi
	;;
esac

case "$_INIT_" in
single)
	# keep it simple.
	PATH=/bin:/usr/bin
	add_path /sbin
	add_path /usr/sbin
	add_path /usr/etc
	PROMPT='#'
	ask t "Enter terminal type [$TERM]:"
	TERM=${t:-$TERM}
	export TERM PATH
	;;
*env*)
	;;
*)	# do these once
	_INIT_="$_INIT_"env
	case `echo -n .` in -n*) N=; C="\c";; *) N="-n"; C=;; esac

	# make sure this at least is in path.
	add_path /usr/bin

	# much of this stuff is repeated in /etc/rc_d/os.sh
	OS=`uname`
	MACHINE=`uname -m`
	OSREL=`uname -r`
	OSMAJOR=`IFS=.; set -- $OSREL; echo $1`

	export OS OSREL OSMAJOR MACHINE MACHINE_ARCH

	# pick one of the following for the default umask
	umask 002	# relaxed	-rwxrwxr-x
	# umask 022	# cautious	-rwxr-xr-x
	# umask 027	# uptight	-rwxr-x---
	# umask 077	# paranoid	-rwx------
	# you can override the default umask
	# for specific groups later...

#	LOCAL_DIRS=`/bin/ls -ld /local /usr/local 2>/dev/null | awk '{ print $NF }' | sort -u`

	# defaults (might be reset below)
	PATH=/bin:/usr/bin
	MANPATH=/usr/man
	SPOOL=/usr/spool
	defterm=vt220

	# set system specific things,
	# eg. set PATH,MANPATH 
	# override default ulimit if desired.
	# defult ulmit is unlimited on SunOS
	# and 4Mb for most System V
	case $OS in
	SunOS)
		# On sun's /bin -> /usr/bin so leave it out!
		PATH=/usr/bin
		add_path /sbin
		add_path /usr/sbin
		add_path /usr/ucb
		add_path /usr/5bin
		add_path /usr/etc
		SPOOL=/var/spool
		LD_LIBRARY_PATH=/usr/lib
		add_path /usr/snm/lib LD_LIBRARY_PATH
		add_path /usr/X11R6/lib LD_LIBRARY_PATH
		add_path /usr/X11R5/lib LD_LIBRARY_PATH
		add_path /usr/openwin/lib LD_LIBRARY_PATH
		export LD_LIBRARY_PATH
		MACHINE_ARCH=`arch`
		case $OSREL in
		4.0)
			# uname -m just says sun which could be anything
			# so use arch(1).
			MACHINE=$MACHINE_ARCH
			;;
		5*)
			MAILDIR=/var/mail
			add_path /opt/gnu/bin
			for d in /opt/SUN* /usr/opt/SUN*
			do
				add_path $d/bin
				add_path $d/sbin
				add_path $d/man MANPATH
			done
			add_path /usr/dt/lib LD_LIBRARY_PATH
			add_path /opt/SUNWconn/snm/lib LD_LIBRARY_PATH
			;;
		esac
		;;
	SCO-UNIX)
		defterm=ansi
		;;
	B.O.S.)
		MANPATH=/usr/catman
		SRC_COMPAT=_SYSV
		export SRC_COMPAT
		;;
	*BSD|386bsd)
		MANPATH=/usr/share/man
		MAILDIR=/var/mail
		SPOOL=/var/spool
		;;
	HP-UX)
		test -s /etc/src.sh && . /etc/src.sh
		HOSTNAME=$SYSTEM_NAME
		test -w / && add_path /etc
		MAILDIR=/usr/mail
		;;
	esac
	MACHINE_ARCH=${MACHINE_ARCH:-$MACHINE}
	
	# add_path only adds them if they exist
	add_path /sbin
	add_path /usr/sbin
	add_path /usr/distbin
	add_path /usr/ucb
	add_path /usr/lbin
	add_path /usr/dbin
	add_path /usr/ldbin
	add_path /usr/ccs/bin
	add_path /opt/bin
	add_path /opt/sbin
	add_path /usr/pkg/bin
	add_path /usr/pkg/sbin
	add_path /usr/pkg/man MANPATH
	
	# move this here because we need PATH set first.
	# the sub-shell is needed to avoid hostname: not found
	HOSTNAME=${HOSTNAME:-`( hostname ) 2>/dev/null`}

	# allow for additional OS specific and site overrides
	for x in $OS$OSMAJOR $OS site
	do
		test -f /etc/profile.$x && . /etc/profile.$x
	done
	# this bit is lifted from config_setup.sh
	# here it allows us to mount a single tree
	# from a fileserver that supports multiple architectures.
	OS_DIRS="$OS_DIRS \
	$OS/$OSMAJOR.X/$MACHINE \
	$OS/$OSMAJOR.X \
	$OS/$MACHINE \
	$OS$OSMAJOR.$MACHINE \
	$OS$OSMAJOR.$MACHINE_ARCH \
	$OS$OSMAJOR \
	$OS.$MACHINE \
	$OS.$MACHINE_ARCH \
	$OS"

	# $SINGLE is always locally mounted and should contain
	# stuff needed in single user mode.
	# $LOCAL may be via NFS
	# $SHARE may be via NFS
	SINGLE=${SINGLE:-/local}
	LOCAL=${LOCAL:-/usr/local}
	SHARE=${SHARE:-/share}
	NFS=${NFS:-/nfs}
	
	for top in $SINGLE $LOCAL $SHARE $NFS
	do
		test -d $top || continue
		for d in share $OS_DIRS local
		do
			test -d $top/$d || continue
			add_path $top/$d/bin
			add_path $top/$d/sbin
			add_path $top/$d/man MANPATH
		done
		add_path $top/bin
		add_path $top/sbin
		add_path $top/man MANPATH
	done

	# if HOSTNAME not set by now, use uname
	HOSTNAME=${HOSTNAME:-`uname -n`}

	export HOSTNAME

	case "$HOME" in
	/)	;;
	""|/tmp)
		echo "Using / for HOME"
		HOME=/; export HOME
		;;
	*)	# put ~/bin at head of path
		pre_path $HOME/bin
		;;
	esac

	for X in X11R6 X11R5 X386 openwin
	do
		for d in /usr $LOCAL
		do
			add_path $d/$X/bin
			add_path $d/$X/man MANPATH
		done
	done
	# historical
	add_path /usr/bin/X11
	add_path /usr/openwin/bin

	# make sure these are set at least once
	LOGNAME=${LOGNAME:-`logname`}
	USER=${USER:-$LOGNAME}

	# Two common ways of dealing with group specific startup.
	# We look for profile.$GROUPNAME in .. and /etc
	# Then provided $HOME does not contain /etc/ we also look for
	# profile in ..
	# We use .. instead of `dirname $HOME` since that does not do
	# the right thing when using automounted /homes.
	# GROUPNAME is the name of the user's primary group.
	GROUPNAME=`id -gn 2>/dev/null`
	source : profile.$GROUPNAME .. /etc
	case $HOME in
	*/etc/*) ;;
	*)	[ -s ../profile ] && . ../profile;;
	esac

	export LOCAL TTY PATH LOGNAME USER
	# the PS1P, PS1S and PS2 values ensure that we can cut/paste
	# commands easily from one window to another
	# add this to your .profile or profile.site if you want it
        # note that the space after : is necessary.
	# PS1P=": "
	if [ -w / ]; then
		PS1S="#${PS1P:+;}"
	else
		# ensure . is at end, but not for root
		PATH=$PATH:.
		PS1S="${PS1P:+;}"
		PS1S="${PS1S:-\$}"
	fi
	
	if [ -t 0 ]; then
		# we are interactive
		# for Solaris and some others, basename won't do
		TTY=`tty | sed 's,/dev/,,'`
                case "$HOSTNAME" in
		*.*)	HOST=`IFS=.; set $HOSTNAME; echo $1`;;
		esac

		# set up some env variables
		MAIL=${MAILDIR:-$SPOOL/mail}/$USER
		MAILPATH=$MAIL:/etc/motd
		PAGER=${PAGER:-more}
		export MAIL MANPATH MAILPATH PAGER

		EDITOR=vi
		VISUAL=vi
		export EDITOR VISUAL
		PROMPT="$USER@$HOST"
		[ -f /etc/profile.TeX ] && . /etc/profile.TeX
	else
		TTY=none
	fi

	# test (and setup if we are Korn shell)
	if [ "$RANDOM" != "$RANDOM" ]; then
		# we are Korn shell
		SHELL=/bin/ksh
		ENV=${HOME%/}/.kshrc
		if [ ! -f $ENV ]; then
			ENV=/etc/ksh.kshrc
		fi
                case "$KSH_VERSION" in
		*PD*v5.*|"")
                        # this _gross_ bit of magic ensures that $ENV
                        # is only evaled for interactive shells
                        # apparently based on a usenet posting about
                        # an error in the ksh book
                        # thanks to woods@most.weird.com (Greg A. Woods)
	                # note that pd-ksh >= 5.x is needed for arrays
                        # to work!
                        ENVFILE=$ENV
                        ENV='${ENVFILE[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}'
                        export ENVFILE
                        ;;
		esac
		HISTFILE=${HOME%/}/.sh_hist
		CDHISTFILE=${HOME%/}/.cd_hist
                PS1S=":!$PS1S"
		export HISTSIZE HISTFILE ENV CDHISTFILE
		CDPATH=.:$HOME
		if [ "$TMOUT" ]; then
			typeset -r TMOUT
		fi
		export FCEDIT=$EDITOR
		set -o emacs	# biased -)
	else
		SHELL=/bin/sh
	fi
	export SHELL EDITOR PATH PROMPT HOSTNAME CDPATH
;;
esac
PS1=${PROMPT:+"$PS1P$PROMPT$PS1S "}
case "$PS1" in
:*) PS2="	";;
esac
export PS1 PS2

# login time initialization
case "$_INIT_" in
*single*)
	;;
*log*)
	;;
*)	_INIT_="$_INIT_"log
	if [ "$TTY" != none -a "x$0" != "x-su" -a "$LOGNAME" = "`logname`" ]
	then
		case ",${MODEMS}," in
		*,$TTY,*)
		  if [ -x /usr/bin/logger -o -x /usr/ucb/logger ]; then
		    logger -p auth.notice -t $TTY "$LOGNAME logged in"
		  else
		    echo "`date '+%b %d %H:%M:%S'` $LOGNAME logged in on $TTY" > /dev/console
		  fi
		  ;;
		esac
		# ensure these are known, 
		# we could use stty sane, but that fiddles parenb etc
		stty isig icanon intr '^c' erase '^h' kill '^u' eof '^d' echoe echok
		if [ ! -f ~/.hushlogin ]; then
			# ensure known state
			case $OS in
			SunOS|*BSD)	;;
			*)	mesg y;;
			esac

			case $TERM in
			ask|network|unknown|dialup|"") 
				ask tmpterm "Enter terminal type [$defterm]:"
				TERM=${tmpterm:-$defterm}
				;;
			esac
			case "$TERM" in
			pc3|xterm*)
#				stty erase '^?'
				case "$OS" in
				SunOS|HP-UX)
					stty rows 24
					;;
				esac
				;;
			esac
			# not all of the following are appropriate at all sites
			# Sun's don't need to cat /etc/motd for instance
			case "$OS" in
			SunOS)
				case $OSREL in
				5*)
					# this is from original Sun profile
					test -x /usr/sbin/quota && /usr/sbin/quota
					trap "trap '' 2"  2
					/bin/cat -s /etc/motd
					trap "" 2

					/bin/mail -E
					case $? in
					0)	echo "You have new mail.";;
					2)	echo "You have mail.";;
					esac
					;;
				esac
				;;
			SCO-UNIX)	
				[ -s /etc/motd ] && cat /etc/motd
				[ -x /usr/bin/mail -a -s "$MAIL" ] && 
					echo "You have mail."
				[ -x /usr/bin/news ] && /usr/bin/news -n
				;;
			*BSD|386bsd)
				# hardware flow control works so use it
				case ",${MODEMS}," in
				*,$TTY,*)
					stty -ixon -ixany
					stty crtscts
					;;
				esac
				;;
			*)
				[ -s /etc/motd ] && cat /etc/motd
				if [ -x /usr/bin/mailx -a -s "$MAIL" ]; then
				  if mailx -e; then
				    echo "You have mail."
				    # show the the headers, this might
				    # be better done in .profile so they
				    # can override it.
				    # mailx -H
				  fi
				fi
				[ -x /usr/bin/news ] && /usr/bin/news -n
				;;
			esac
			if [ -f $LOCAL/etc/1stlogin.ann ]; then
				[ -f $HOME/... ] || sh $LOCAL/etc/1stlogin.ann
			fi
			[ -x /usr/games/fortune ] && /usr/games/fortune -a
			# remind folk who turned on reply.pl to turn it off.
			if [ -f $HOME/.forward ]; then
				fwd=`cat $HOME/.forward`
				case "$fwd" in
				*reply*)
					echo "Your mail is being forwarded to:"
					echo $fwd
					echo "Perhaps you should run \"reply.pl off\""
					;;
				esac	
				unset fwd
			fi
			# we install msgs where we can
			msgs=`Which msgs`
			[ "$msgs" ] && $msgs -fq
		  fi
	fi
	unset tmpterm defterm C N

	case "$TERM" in
	network|unknown|"")	TERM=$defterm;;
	esac
	export TERM TTY
;;
esac
# Handle X-terminals if necessary
case "$_INIT_" in
single)	;;
*)	[ -f /etc/profile.X11 ] && . /etc/profile.X11;;
esac
case "$_INIT_" in
*:${HOSTNAME}:*) ;;
*) _INIT_="${_INIT_}:${HOSTNAME}:";;
esac
# make sure you have this bit last
trap $sigs	# restore signals
unset sigs
export _INIT_
