#! /bin/sh

###### 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 -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 "OSF1"` -gt 0 ]; then
      OS_TYPE=osf1
   else
      OS_TYPE=sun
   fi
   export OS_TYPE

   ISHMAIL=$0
      PATH=$PATH:/bin; 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

   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 system mail folder."
	 echo ""
	 exit 1
      fi

##### Here, we're ok

      export MAIL
   fi

#
# work around UnixWare weirdness with unix:0, assume DISPLAY=:0 is
# good everywhere, and in fact preferrable.
#
   DEFDISP=":0"
   DISPLAY=${DISPLAY:-$DEFDISP}
   if [ "$DISPLAY" = "unix:0" -o "$DISPLAY" = "unix:0.0" ]
   then
      DISPLAY=":0"
   fi
   export DISPLAY

#
# unconditionally use our X11R5 NLS stuff. some earlier versions
# of X11R5 have incompatibilities.
#
   XKEYSYMDB=$ISHLIB/XKeysymDB; export XKEYSYMDB
   XNLSPATH=$ISHLIB/nls; export XNLSPATH
   SYSMAILCAP=$ISHLIB/mailcap; export SYSMAILCAP
   PATH=$PATH:$ISHBIN; export PATH
   MANPATH=$MANPATH:$ISHMAN; export MANPATH
   LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ISHLIB; export LD_LIBRARY_PATH
   LANG=${LANG:-C}; export LANG

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

##### 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

##### make sure default folder directory is present

   if [ ! -d $HOME/Mail ]
   then
      mkdir $HOME/Mail
      chmod 0755 $HOME/Mail
   fi

   CMD="$ISHBIN/ishmail.real $*"
   PROG=`mybasename_f $0`

   if [ "$PROG" = "ishmail.shr" ]
   then
      CMD="$ISHBIN/ishmail.shr.real $*"
	     
   elif [ "$PROG" = "ishmail.debug" ]
   then

      if [ ! -z "$ISHDEBUG" ]
      then
	 CMD="$ISHDEBUG $CMD"
      else
	 case $OS_TYPE in
	    386)
	       CMD="debug $CMD"
	       ;;
	    sol)
	       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"
	       fi
	       ;;
	 esac
      fi
   fi

   exec $CMD
