#!/bin/sh
: '110914 shell profile -*- ksh -*- vim:set ft=sh noet sw=2 pt=8:'

trap "echo '.profile: interrupted'" 1 2 3 4 5 12 14 15

: 'This PATH is temporary'

PATHOLD="$PATH"
PATH='/bin:/usr/bin:/sbin:/usr/sbin'

: 'For the list of "updatedb" hierarchies'

DBDIRS='/etc/updatedb.dirs'

# Because of the way the path setting functions work, the 
# first hierarchies, twigs and subhierarchies will end up
# last in the constructed path.

export HULL SITE NODE

for P in '/etc/' "$LOCAL/etc/" "$HOME/."
do
  for F in env-SITE env-HULL env-NODE env-boot
  do test -r "$P$F" && . "$P$F"
  done
done

: 'Hierachies are tested in reverse order '

export LOCAL
case "$LOCAL" in
'/usr/local')	;;
'/loc')		;;
?*)		HPRE="$LOCAL $HPRE";;
'')		;;
esac

for S in '' $HSUF; do for P in /usr/local $HPRE; do HIERS="$HIERS $P$S/"; done; done

TWIGS='/X11 /netpbm /ncurses /pthread'
SUBHIERS='games/ mh/ openwin/ dt/ interviews/ andrew/ grass/ khoros/ TeX/ emacs/ gnome/ gnome2/ kde/ kde2/ kde3/'
SUBXHIERS='X11R6/ X11/ openwin/ lpp/X11/ X11R5/ X386/ X/'

export SHELL PS1 PS2 HOME
export USER LOGNAME HOST DOMAIN
export EDITOR VISUAL PAGER
export TERMCAP TERMINFO
export RSH CVS_RSH RSYNC_RSH

export cso_proxy finger_proxy ftp_proxy gopher_proxy http_proxy https_proxy
export news_proxy nntp_proxy no_proxy snews_proxy wais_proxy

unset MAIL

isrdir() { test \! -L "$1" -a -d "$1"; }
case "$OSTYPE" in NetBSD) typeset() { :; };; esac

case "$USER;$USERNAME" in
  ';'?*)				USER="$USERNAME";;
esac

case "$USER;$LOGNAME" in ';')
  if test -f '/usr/ucb/whoami';		then USER=`/usr/ucb/whoami`
  elif test -f '/usr/bin/id';		then LOGNAME="`expr \`id\` : '(\([^)]*\))'`"
  fi
esac

case "$USER;$LOGNAME" in
  ';'?*)				USER="$LOGNAME";;
  ?*';')				LOGNAME="$USER";;
esac

case "$HOST" in '')
  if test -f '/bin/hostname';		then HOST="`/bin/hostname`"
  elif test -f '/usr/bin/hostname';	then HOST="`/usr/bin/hostname`"
  elif test -f '/bin/uname';		then HOST="`/bin/uname -n`"
  elif test -f '/usr/bin/uname';	then HOST="`/usr/bin/uname -n`"
  fi;;
esac

case "$DOMAIN" in '')
  if test -f '/bin/dnsdomainname' \
    -o -f '/usr/bin/dnsdomainname';	then DOMAIN="`dnsdomainname`"
  elif test -f '/bin/domainname' \
    -o -f '/usr/bin/domainname';	then DOMAIN="`domainname`"
  fi;;
esac

case "$HOST" in *'.'*)
  eval `echo "$HOST" | sed 's/\([^.]*\)\.\(.*\)/HOST="\1"; DOMAIN="\2"/'`;;
esac

case "$EDITOR" in '')			EDITOR='/bin/ed';; esac

case "$no_proxy" in '')			no_proxy="$DOMAIN";; esac

searchfor()
{
  : typeset NAME TEST VALUE ITEM

  NAME="$1"; TEST="$2"; shift; shift
  eval VALUE="\"\$$NAME\""
  case "$VALUE" in '')
    for ITEM in ${1+"$@"}
    do
      if test \! -L "$ITEM" -a "$TEST" "$ITEM"
      then
	eval "$NAME"="'$ITEM'"
	return 0
      fi
    done
    ;;
  esac
  return 1
}

searchfor VISUAL	-f /usr/ucb/vi /usr/bin/vi /bin/vi
searchfor PAGER		-f /bin/less /usr/bin/less /usr/bin/lv /usr/ucb/more /usr/bin/more /usr/bin/pg
searchfor TERMCAP	-f "$HOME/.termcap" "$LOCAL/etc/termcap" /usr/share/misc/termcap
searchfor TERMINFO	-f "$HOME/.terminfo" "$LOCAL/etc/terminfo"
if test -r /usr/share/X11/rgb.txt
then XWINHOME=/usr
else
  for SXDIR in $SUBXHIERS
  do searchfor XWINHOME	-d /usr/"$SXDIR"; done
fi
searchfor OPENWINHOME	-d /usr/openwin/ "$LOCAL/openwin/"

searchfor RSH		-x /bin/ssh /usr/bin/ssh "$LOCAL/bin/ssh"
CVS_RSH="$RSH"
RSYNC_RSH="$RSH"

setpath() { test -d "$3" && eval "$1=\"$2$3$4\""; }
sxfspath() { test -d "$3/app-defaults" && eval "$1=\"$2$3/%T/%N%S$4\""; }

: 'BSD vs. System V vs. AIX...'

unset ECHON
unset ECHOC

false && unset PATH
unset FPATH
unset CDPATH
unset MANPATH
unset INFOPATH
unset LD_LIBRARY_PATH
unset EMACSLOADPATH
unset ELPATH
unset XELPATH
unset XFILESEARCHPATH
unset CCPATH
unset LDPATH

export ECHON ECHOC

export PATH FPATH CDPATH MANPATH INFOPATH
export LD_LIBRARY_PATH ELPATH XELPATH XFILESEARCHPATH
export CCPATH LDPATH
export XWINHOME OPENWINHOME

: 'Korn/Bourne Again shell vs. Bourne shell'

: 'This is duplicated in "env"'

export SHELL_VERSION
SHELL_VERSION='sh'

case "$KSH_VERSION" in
			*PD*)	SHELL_VERSION="pdksh $KSH_VERSION";;
			?*)	SHELL_VERSION="ksh $KSH_VERSION";;
esac
case "$BASH_VERSION" in ?*)	SHELL_VERSION="bash $BASH_VERSION";;	esac
case "$ZSH_VERSION" in ?*)	SHELL_VERSION="zsh $ZSH_VERSION";;	esac
case "$SHELL_VERSION;$OSTYPE" in
  		'sh;'*BSD)	SHELL_VERSION="bsdsh";;
esac

case "`echo -n`" in '-n') ECHOC='\c';; '') ECHON='-n';; esac

case "$MAILCHECK" in '') PS1="${PS1-\$ }"; PS2="${PS2-> }";; esac

export HISTSIZE HISTSAVE SAVEHIST HISTFILESIZE HISTCONTROL

HISTSIZE=500
HISTSAVE=300; SAVEHIST="$HISTSAVE"; HISTFILESIZE="$HISTSAVE"
HISTCONTROL=ignoreboth

: 'for "zsh" we need to set some options, so check "env"'

case "$RANDOM" in ?*)
  export PS3 PS4 COLS FCEDIT
  if test -r "$HOME/.env";		then export ENV="$HOME/.env"
  elif test -r '/etc/env';		then export ENV='/etc/env'
  fi;;
esac

: 'Various types of system'

CDPATH=":..:$HOME"

if test -f /bin/cygwin1.dll
then
  : 'Cygwin, so not quite like UNIX'
  PATH="$PATHOLD"
  STTYARGS='crt'
elif test -d /usr/ucb -o -d /usr/libdata
then
  if test \! -d /etc/saf
  then
    : 'More or less pure BSD, or AIX'
    PATH=''
  else
    : 'SVR4'
    PATH=''
    setpath PATH ''		'/usr/ccs/bin'		':$PATH'
    for HIER in usr "$LOCAL"
    do
      setpath PATH '$PATH'	"$HIER/dbin"		':'
      setpath PATH '$PATH'	"$HIER/msdos/bin"	':'
    done
    setpath MANPATH ''		'/usr/share/manx' 	':$MANPATH'
    STTYARGS='echoe echok echoctl echoke'
  fi
elif test -d /proc/net -o -d /usr/src/linux
then
  : 'Linux, a mixed breed'
  PATH=''
  STTYARGS='crt'
  test -r /etc/TIMEZONE	&& . /etc/TIMEZONE
  export KR
  if test -r /proc/version
  then read < /proc/version DUMMY$$ DUMMY$$ KR DUMMY$$; unset DUMMY$$
  elif test -e /bin/uname
  then KR="`/bin/uname -r`"
  fi
elif test -f /usr/bin/cut
then
  : 'Probably some SVR3 variant'
  PATH=''
  MANPATH='.'
  STTYARGS="echoe echok swtch ^T"
  test -d "$HOME" -a -f "$HOME/.hushlogin" || {
    test -r /etc/motd	&& /bin/cat /etc/motd
    test -f /bin/mail	&& /bin/mail -e && echo 'You have mail.'
  }
elif test -d /usr/spool -o -d /usr/mail -o -f /lib/libc.a
then
  : 'Version 7 or vanilla UNIX'
  PATH=''
  ECHON=''; ECHOC=''
else
  : 'Unknown'
  PATHOLD="$PATH"
  ECHON=''; ECHOC=''
fi

setrdir() { 
  isrdir "$3" && \
    setpath "$1"	"$2"	"$3"			"$4"
}

setpaths () {
  : typeset HIER BRANCH

  for HIER in "${1}share/" "${1}"
  do
    isrdir "${HIER}" || continue

    for TWIG in $TWIGS ''
    do
      setpath PATH	''	"${HIER}bin${TWIG}"	':$PATH'
      setpath CCPATH	'-I'	"${HIER}include${TWIG}"	' $CCPATH'
      setpath INFOPATH	''	"${HIER}einfo${TWIG}"	':$INFOPATH'
      setpath INFOPATH	''	"${HIER}info${TWIG}"	':$INFOPATH'
      setpath MANPATH	''	"${HIER}man${TWIG}"	':$MANPATH'
    done
    setpath PATH	''	"${HIER}sbin"		':$PATH'

    isrdir "${HIER}lib" || continue

    setpath FPATH	''	"${HIER}lib/sh"		':$FPATH'
    setpath LIBGL_DRIVERS_PATH '' "${HIER}lib/dri"	':$LIBGL_DRIVERS_PATH'

    sxfspath XFILESEARCHPATH ''	"${HIER}share/X11"	':$XFILESEARCHPATH'
    sxfspath XFILESEARCHPATH ''	"${HIER}lib"		':$XFILESEARCHPATH'

    setpath ELPATH	''	"${HIER}lib/emacs/site-lisp"    ':$ELPATH'
    setpath ELPATH	''	"${HIER}lib/elisp"	        ':$ELPATH'

    setpath XELPATH	''	"${HIER}lib/xemacs/site-lisp"   ':$XELPATH'
    setpath XELPATH	''	"${HIER}lib/xelisp"	        ':$XELPATH'
  done

  test -f /etc/ld.so.cache || for HIER in "${1}"
  do
    if isrdir "${HIER}lib/elf"
    then
      setpath LD_LIBRARY_PATH '' "${HIER}lib/elf"	':$LD_LIBRARY_PATH'
      setpath LDPATH	'-L'	"${HIER}lib/elf"	' $LDPATH'
    elif isrdir "${HIER}lib/aout"
    then
      setpath LD_LIBRARY_PATH '' "${HIER}lib/aout"	':$LD_LIBRARY_PATH'
      setpath LDPATH	'-L'	"${HIER}lib/aout"	' $LDPATH'
    else
      setpath LD_LIBRARY_PATH '' "${HIER}lib"		':$LD_LIBRARY_PATH'
      setpath LDPATH	'-L'	"${HIER}lib"		' $LDPATH'
    fi
  done
}

setXsubpaths () {
  : typeset HIER

  for HIER in "${1}share/" "${1}"
  do
    setrdir PATH	''	"${HIER}bin/X11"	':$PATH'
    setrdir CCPATH	'-I'	"${HIER}include/X11"	' $CCPATH'
    setrdir MANPATH	''	"${HIER}man/X11"	':$MANPATH'
    sxfspath XFILESEARCHPATH ''	"${HIER}lib/X11"	':$XFILESEARCHPATH'
  done

  test -f /etc/ld.so.cache || for HIER in "${1}"
  do
    if isrdir "${HIER}lib/X11/elf"
    then
      setpath LD_LIBRARY_PATH '' "${HIER}lib/X11/elf"	':$LD_LIBRARY_PATH'
      setpath LDPATH	'-L'	"${HIER}lib/X11/elf"	' $LDPATH'
    elif isrdir "${HIER}lib/X11/aout"
    then
      setpath LD_LIBRARY_PATH '' "${HIER}lib/X11/aout"	':$LD_LIBRARY_PATH'
      setpath LDPATH	'-L'	"${HIER}lib/X11/aout"	' $LDPATH'
    else
      setpath LD_LIBRARY_PATH '' "${HIER}lib/X11"	':$LD_LIBRARY_PATH'
      setpath LDPATH	'-L'	"${HIER}lib/X11"	' $LDPATH'
    fi
  done
}

setXpaths() {
  : typeset HIER

  for HIER in $SUBXHIERS
  do isrdir "${1}$HIER" || continue
    case "$XWINHOME" in '') XWINHOME="${1}$HIER";; esac
	false && case "${1}$HIER" in "$XWINHOME"|"$OPENWINHOME") break;; esac
    setpaths "${1}$HIER"
    break
  done
}

setallpaths() {
  : typeset HIER SDIR

  for HIER
  do isrdir "$HIER/." || continue
    setpaths "$HIER"
    setXsubpaths "$HIER"
    setXpaths "$HIER"

    for SDIR in $SUBHIERS
    do isrdir "${HIER}$SDIR" || continue
      setpaths "${HIER}$SDIR"
      setXpaths "${HIER}$SDIR"
    done
  done
}

# This allows setting up a "..." directory in the home
# directory that contains links to directories one wants
# to be able to 'cd' directly to.

setpath		CDPATH		'$CDPATH:'	"$HOME/..."	''

false && case "$XWINHOME" in ?*)	setpaths "$XWINHOME";;		esac
false && case "$OPENWINHOME" in ?*)	setpaths "$OPENWINHOME";;	esac

setallpaths	/usr/ /
setpath		PATH		''		"/usr/ucb"	':$PATH'
setpath		PATH		'$PATH:'	"/usr/games"	''

setallpaths	$HIERS

setpath		ELPATH		''	"/etc/elisp"	':$ELPATH'
setpath		XELPATH		''	"/etc/xelisp"	':$XELPATH'
sxfspath	XFILESEARCHPATH '' 	"/etc/X11"	':$XFILESEARCHPATH'
sxfspath	XFILESEARCHPATH '' 	"/etc"		':$XFILESEARCHPATH'

false && login ()	{ exec login; }
false && logout ()	{ kill -1 $$; }

ask () {
  : typeset NAME VALUE ALIAS DEFAULT ACTION

  NAME="$1"; VALUE="$2"; ALIAS="$3"; DEFAULT="$4"; ACTION="$5"
  {
    : typeset QUERY DOQUERY

    case "$VALUE" in '') : ;; *) DEFAULT="$VALUE";; esac

    QUERY="$NAME[$DEFAULT]=";
    DOQUERY="echo $ECHON '$QUERY$ECHOC' && read 'VALUE'"

    until
      eval "$DOQUERY"
      case "$VALUE" in '') VALUE="$DEFAULT";; esac;

      eval "$ALIAS"="'$VALUE'"
      case "$VALUE" in '.') : ;; ?*) "$ACTION";; esac
    do :
    done
  }
}

if test -t 0
then
  case "$DISPLAY" in '')
    : 'Initialize X windows.'

    # $DISPLAY logic:

    # If $DISPLAY is set, use that as default.
    # If $DISPLAY is not set, use "." as default.

    # If $DISPLAY is not ".":
    #	If "xdpyinfo" is present check it.
    #	If the check fails, prompt again and repeat.

    searchfor XDPYI -f \
      /usr/X11/bin/xdpyinfo /usr/bin/X11/xdpyinfo \
      /usr/bin/xdpyinfo /usr/local/X11/bin/xdpyinfo

    inidisp() { 
      case "$DISP;$XDPYI" in
      '.;'*)	: 'unset, xpydinfo does not matter';;
      *':'*';')	: 'good, no xpydinfo, accept it' ;;
      *':'*';'?*)	"$XDPYI" -display "$DISP" >/dev/null 2>&1;;
      *';'?*)	DISP=":0.0"; "$XDPYI" -display "$DISP" >/dev/null 2>&1;;
      esac
    }

    if test -t 0 \
      -a '(' -n "$XWINHOME" -o -n "$OPENWINHOME" -o -d /usr/lib/X11 ')'
    then
      case "$DISPLAY" in
      ?*) INITIAL="$DISPLAY";;
      '') INITIAL='.';;
      esac

      ask DISPLAY "$DISPLAY" DISP "$INITIAL" 'inidisp'
    fi

    case "$DISP" in
    '')	: ;;
    '.')	unset DISPLAY;;
    ?*)	export DISPLAY; DISPLAY="$DISP";;
    esac

    unset inidisp
    unset INITIAL
    unset DISP
    unset XDPY;;
  esac

  case "$TERM" in '')
    : 'Initialize the terminal'

    if test -x /usr/ucb/tset \
      -o -x /usr/bin/tset \
      -o -x "$LOCAL/bin/tset" \
      && tset -Q dumb >/dev/null;	then INIT='tset -Q -m ":$TTYPE"'
    elif test -x /bin/tput \
	-o -x /usr/bin/tput \
	-o -x /usr/ccs/bin/tput \
       && tput -Tdumb init >/dev/null;	then INIT='tput -T "$TTYPE" init'
    fi

    unset TINFO

    case "$INIT" in '') INIT=':';; esac

    if false
    then
      ask TERM "$TERM" TERM 'vt100' "$INIT"
    else
      searchfor STTY -f /bin/stty /usr/bin/stty /usr/ucb/stty
      STTYA="$STTYARGS"

      initerm() \
      {
	: 'some versions of bash cannot do "echo a b c | read A B C"'

	echo "$TTYPE" | while read TTYPE TLINES TCOLUMNS
	do
	  case "$TTYPE" in [,=]) TTYPE="$DEFAULT";; esac

	  eval "$INIT" && {
	    case "$TLINES" in ?*)     export LINES; LINES="$TLINES"
				      STTYA="$STTYA rows $TLINES";;
	    esac
	    case "$TCOLUMNS" in ?*)   export COLUMNS; COLUMNS="$COLUMNS"
				      STTYA="$STTYA cols $TCOLUMNS";;
	    esac
	    case "$STTY;$STTYA" in ?*';'?*)
				      test -t 0 -a -r /dev/tty \
					&& eval "$STTY" < /dev/tty "$STTYA";;
	    esac
	  }
	  unset TLINES
	  unset TCOLUMNS

	  : 'this "break" is needed by some versions os BASH'
	  break
	done

	: 'putting an "unset" here breaks the return code'
      }

      ask TERM "$TERM" TTYPE 'vt100' 'initerm'

      case "$TTYPE" in
      '')   : ;;
      '.')  unset TERM;;
      ?*)   export TERM; TERM="$TTYPE";;
      esac

      unset initerm
      unset TTYPE
      unset STTYA
      unset STTY
    fi;;
  esac

  case "$COMSPEC" in
  '') stty erase '^?' kill '^U' intr '^C';;
  ?*) stty erase '^H' kill '^U' intr '^C';;
  esac

  unset INIT
fi

unset STTYARGS
unset ask

: 'For the list of "updatedb" hierarchies'

DBDIRS='/etc/updatedb.dirs'

if test -e "$DBDIRS"
then
  export LOCATE_PATH

  dbdirspath() {
    DBPATH="$1"; DBDIRS="$2"

    { grep -v '^[ \t]*#\|^[ \t]*$' "$DBDIRS"; echo ''; } \
    | while read DIR DB
    do
      case "$DIR" in
      '') echo "$DBPATH";;
      ?*)
	: ${DB:="$DIR/mlocate.db"}
	case "$DBPATH" in
	'') DBPATH="$DB";;
	?*) DBPATH="$DBPATH:$DB";;
	esac;;
      esac
    done

    unset DBPATH
    unset DIR
    unset DB
  }

  LOCATE_PATH="`dbdirspath \"$LOCATE_PATH\" \"$DBDIRS\"`"
fi

: 'Reset the not yet done message and define a logout script'

trap 1 2 3 4 5 12 13 14 15
trap 'test "$HOME" -a -r "$HOME"/.bye		&& . "$HOME"/.bye;
  echo "Goodbye $USER."; trap 0' 0

# We need to unset all exported env vars which have an empty value; they
# might confuse the NetBSD /bin/sh and programs. Pray there are no variables
# whose name has blanks in them -- too hard to fix.

test -x /bin/printenv -o -x /usr/bin/printenv \
  && for VAR in `printenv | grep '[-A-Za-z0-9_]=$' | sed 's/=$//'`
     do unset $VAR; done

: ' This is done for some silly but important reason. Just to make sure...'

cd .
true
