#! /bin/sh

###### Return suffix of argument

mysuffix_f() {

   echo $1 | sed -e "s|^.*\.||"
}

###### Return base name of argument

mybasename_f() {

   echo $1 | sed -e "s|^.*/||"
}

###### Return directory name of argument

mydirname_f() {

   DIR=`echo $1 | sed -e "s|/[^/]*$||"`

   if [ "$DIR" = "$1" ]
   then
      DIR="."
   elif [ -z "$DIR" ]
   then
      DIR="/"
   fi

   if [ "$DIR" = "." ]
   then
      DIR=`/bin/pwd`
   fi

   echo $DIR
}

###### Used to see if a file is a link

islink() {

   case "$OS_TYPE" in
      aix) test -L $1
	    ;;
      osf1) file $1 | grep 'symbolic link' > /dev/null 2>&1
	    ;;
      *) test -h $1
	    ;;
   esac

   exit $?
}

###### Return real location of argument

realpath_f() {

###### Initial variables

   CMD=$1

###### See if the command is a link

   if ( islink $CMD )
   then

      DIR=`mydirname_f $CMD`		######  Go to the directory in question
      cd $DIR
      PROG=`mybasename_f $CMD`		###### Get the program name

###### Loop while the program is a link

      while ( islink $PROG )
      do
	 CMD=`ls -l $PROG | sed -e "s|.* ||"`	###### Get the link source

	 DIR=`mydirname_f $CMD`		###### Get the new directory name
	 cd $DIR
	 DIR=`/bin/pwd | sed -e "s|/tmp_mnt||"`

	 PROG=`mybasename_f $CMD`	###### Get the new program name
	 CMD=$DIR/$PROG			###### Build a new command name
      done
   fi

   echo "$CMD"

} ##### End realpath_f

#<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
#<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
#<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>

##### Determine OS_TYPE

   STR=`uname -a`
   if [ `echo $STR | grep -i -c "5\.4.*sun4H.*sparc"` -gt 0 ]; then
      OS_TYPE=hal
   elif [ `echo $STR | grep -c "5\.4.*sparc"` -gt 0 ]; then
      OS_TYPE=sol24
   elif [ `echo $STR | grep -c "5\.3.*sparc"` -gt 0 ]; then
      OS_TYPE=sol23
   elif [ `echo $STR | grep -c "5\..*sparc"` -gt 0 ]; then
      OS_TYPE=sol
   elif [ `echo $STR | grep -c "AIX"` -gt 0 ]; then
      OS_TYPE=aix
   elif [ `echo $STR | grep -c "386"` -gt 0 ]; then
      OS_TYPE=386
   elif [ `echo $STR | grep -c "HP-UX"` -gt 0 ]; then
      OS_TYPE=hp
   elif [ `echo $STR | grep -c "Linux"` -gt 0 ]; then
      OS_TYPE=linux
   elif [ `echo $STR | grep -c "OSF1"` -gt 0 ]; then
      OS_TYPE=osf1
   elif [ `echo $STR | grep -c "R4000"` -gt 0 ]; then
      OS_TYPE=mips
   elif [ `echo $STR | grep -c "DS/90"` -gt 0 ]; then
      OS_TYPE=ds90
   else
      OS_TYPE=sun
   fi
   export OS_TYPE

   ISHMAIL=$0
   PATH=$PATH:/bin
   if [ -d /usr/ucb ]; then
	PATH=$PATH:/usr/ucb
   fi
   export PATH

##### Find the real location of this script

   ISHMAIL=`realpath_f $ISHMAIL`
#echo Really: $ISHMAIL

   ISHBIN=`mydirname_f $ISHMAIL`; export ISHBIN
   ISHDIR=`mydirname_f $ISHBIN`; export ISHDIR
   ISHVER=`mybasename_f $ISHDIR`; export ISHVER
   ISHLIB=$ISHDIR/lib; export ISHLIB
   ISHMAN=$ISHDIR/man; export ISHMAN
#echo Dir: $ISHDIR
#echo Ver: $ISHVER
#echo Bin: $ISHBIN
#echo Lib: $ISHLIB

##### Find user name

   USER=${USER:-$LOGNAME}
   if [ -z "$USER" ]
   then

      if [ "`type whoami | sed -n '/.* is .*/p'`" ]; then
         USER=`whoami`
      else
	 echo ""
	 echo "Please set the environment varable \"USER\" to your login id."
	 echo ""
	 exit 1
      fi

      export USER
   fi

##### Find mail directory

#
# This code removed because it doesn't allow $MAIL to exist on an IMAP or POP
#    server
#
#   if [ ! -z "$MAIL" ]
#   then
#
#      if [ -d $MAIL ]
#      then
#	 MAIL=$MAIL/$USER
#      else
#	 MAILDIR=`mydirname_f $MAIL`
#	 if [ ! -d $MAILDIR ]
#	 then
#	    unset MAIL
#	 fi
#      fi
#
#   fi

   if [ -z "$MAIL" ]
   then

      if [ -d /var/spool/mail ]
      then
	 MAIL=/var/spool/mail/$USER

      elif [ -d /usr/spool/mail ]
      then
	 MAIL=/usr/spool/mail/$USER

      elif [ -d /var/mail ]
      then
	 MAIL=/var/mail/$USER

      else
	 echo ""
	 echo "Please set the environment varable \"MAIL\" to the location of"
	 echo "your in-box."
	 echo ""
	 exit 1
      fi

##### Here, we're ok

      export MAIL
   fi

#
# work around occasional glitch with "unix:0". assume ":0" always works
# for the local display
#
   DEFDISP=":0"
   DISPLAY=${DISPLAY:-$DEFDISP}
   if [ "$DISPLAY" = "unix:0" -o "$DISPLAY" = "unix:0.0" ]
   then
      DISPLAY=":0"
   fi
   export DISPLAY

#
# make sure we can find "xterm" (used, for example, when launching
# the alternate editor)
#
   if [ "`type xterm | sed -n '/.* is .*/p'`" = "" ]; then
      if [ -x /usr/bin/X11/xterm ]; then
	PATH=$PATH:/usr/bin/X11
      else
	if [ -x /usr/openwin/bin/xterm ]; then
	   PATH=$PATH:/usr/openwin/bin
	else
	   echo
	   echo "Please update the \"PATH\" environment variable to"
	   echo "indicate the location of the \"xterm\" command."
	   echo
	   exit 1
	fi
      fi
      export PATH
   fi
#
# unconditionally use our X11R5 NLS stuff. some earlier versions
# of X11R5 have incompatibilities.
#
   if [ "$OS_TYPE" != "sol24" -a "$OS_TYPE" != "hal" ]
   then
      XKEYSYMDB=$ISHLIB/XKeysymDB; export XKEYSYMDB
      XNLSPATH=$ISHLIB/nls; export XNLSPATH
      LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ISHLIB; export LD_LIBRARY_PATH
   fi

   if [ -z "$MAILCAPS" ]
   then
      MAILCAPS=$HOME/.mailcap:$ISHLIB/mailcap:/etc/mailcap
      MAILCAPS=$MAILCAPS:/usr/etc/mailcap:/usr/local/etc/mailcap
   else
      MAILCAPS=$MAILCAPS:$ISHLIB/mailcap
   fi
   export MAILCAPS

   MIMETYPES=${MIMETYPES:-$ISHLIB/mime-types}; export MIMETYPES
   PATH=$PATH:$ISHBIN; export PATH
   MANPATH=$MANPATH:$ISHMAN; export MANPATH
   LANG=${LANG:-C}; export LANG

   XFILESEARCHPATH=$XFILESEARCHPATH:$ISHLIB/%N%C:$ISHLIB/%N
   XBMLANGPATH=$XBMLANGPATH:$ISHLIB/%B:/usr/include/X11/bitmaps/%B
   export XFILESEARCHPATH XBMLANGPATH

#
# Uncomment this line if you don't want the table of contents created in the
# mail spool directory
#
#   ISHTOC=${ISHTOC:-$HOME/Mail/.ishtoc}; export ISHTOC

##### Set up the license file

   DEFLIC=$ISHBIN/license
   ISHMAILLIC=${ISHMAILLIC:-$DEFLIC}; export ISHMAILLIC

##### Point to the welcome file

   if [ -f $ISHLIB/welcome ]
   then
      ISHWELCOME=$ISHLIB/welcome
      export ISHWELCOME
   fi

   PROG=`mybasename_f $0`
   TYPE=`mysuffix_f $PROG`

   if [ "$PROG" = "ishmail.shr" -o "$PROG" = "ishmail.shr.debug" ]
   then
      CMD="$ISHBIN/ishmail.shr.real $*"
   else
      CMD="$ISHBIN/ishmail.real $*"
   fi
	     
   if [ "$TYPE" = "debug" ]
   then

      if [ ! -z "$ISHDEBUG" ]
      then
	 CMD="$ISHDEBUG $CMD"
      else
	 case $OS_TYPE in
	    386)
	       CMD="debug $CMD"
	       ;;
	    sol | sol23 | sol24 | hal)
	       if [ -x /opt/SUNWspro/bin/dbx ]
	       then
		  CMD="/opt/SUNWspro/bin/dbx $CMD"
	       elif [ -x /tools/fpd/bin/gdb ]
	       then
		  CMD="/tools/fpd/bin/gdb $CMD"
	       fi
	       ;;
	    aix)
	       if [ -x /bin/dbx ]
	       then
		  CMD="/bin/dbx $CMD"
	       fi
	       ;;
	    *)
	       if [ -x /usr/tools/lang/SC1.0/dbx ]
	       then
		  CMD="/usr/tools/lang/SC1.0/dbx $CMD"
	       elif [ -x /usr/tools/bin/gdb ]
	       then
		  CMD="/usr/tools/bin/gdb $CMD"
	       elif [ -x /usr/bin/gdb ]
	       then
		  CMD="/usr/bin/gdb $CMD"
	       fi
	       ;;
	 esac
      fi
   fi

   exec $CMD
