#!/bin/sh
######################################################################
#
#     mountfloppy
#
#     Hewlett-Packard HP Netstation SW, version B.08
#
#     Copyright (c) 1993-1997 Hewlett-Packard Company
#
#     $Revision: 1.0 $
#
######################################################################

#
# This script attempts to mount a floppy from an HP Netstation
# $1 is DISPLAY,  $2 is USER, $3 is name of link to mountpoint
#
# Real mountpoint is /tmp_mnt.hpxt/Xstation_name/hpxtfd
# A symbolic link to mountpoint is created as $3 - normally $HOME/floppy
#
# Script can be run standalone by root but is normally called
# from Xstartup by vuelogin/xdm after the user is verified.
# 

set -a

PATH="/etc/:/sbin:/usr/sbin:/bin/:/usr/etc:/usr/bin"
IFS=' 	
'			# space tab newline


  #
  # If no arguments specified, fall back on "built-ins"
  # Since we are being called from Xstartup there is error checking
  #
if [ "$1" != "" ]
then
  DISPLAY=$1
fi

if [ "$2" != "" ]
then
  USER=$2
fi

if [ "$3" != "" ]
then
  LINKNAME=$3
else
  LINKNAME=$HOME/floppy
fi


  #
  # make sure we have a SHELL
  #
if [ -x /bin/sh ]
then
    SHE="/bin/sh"
else
    SHE="/sbin/sh"
fi



  #
  # make sure it's an HP Netstation
  #

XRDB="/usr/bin/X11/xrdb"
if [ ! -f "$XRDB" ]
then
  XRDB="/usr/openwin/bin/xrdb"
fi

if [ -f "$XRDB" ]
then
  $XRDB -symbols -display $DISPLAY \
        | egrep -i 'Hewlett.Packard.Company' > /dev/null
  if [ $? -ne 0 ]
  then
    #
    # If connection was just refused .. do not bother with this check
    #
    $XRDB -query -display $DISPLAY 2>&1 | egrep -i 'connection.*refused' > /dev/null
    if [ $? -ne 0 ]
    then
      # no just return
      exit 0
    fi
  fi
fi

  #
  # Isolate host part of DISPLAY variable	
  #

THISTERM=`echo ${DISPLAY} | sed 's/[:.]/ /g' | awk '{print $1}'`
echo $THISTERM | grep '^[0-9]' > /dev/null
if [ $? -eq 0 ]
then
  THISTERM=`echo ${DISPLAY} | sed 's/[:]/ /g' | awk '{print $1}'`
fi

FFS="hpxtfd"
MNTTAB=/etc/mnttab


PARENTDIR=/tmp_mnt.hpxt/$THISTERM
MOUNTPOINT=$PARENTDIR/$FFS

  #
  #  any floppy on this terminal?
  #

showmount -e $THISTERM | egrep "/$FFS" > /dev/null 2>&1 
if [ $? -ne 0 ]
then
  # no just return
  exit 0
fi


#
# As a security measure:
# if uid is 0, eg we are the super user, just return 
#

UID=\
`su $USER -c $SHE <<EOF
id
EOF`

UID=`echo $UID | awk -F'(' '{print $1}' | awk -F'=' '{print $2}'`

if [ "$UID" = "0" ]
then
  exit 0
fi


#
#  What is the Architecture?
#
THISPATH=`ls -la $0 | awk '{print $NF}'`
THISDIR=`dirname $THISPATH`

OS=`$THISDIR/xtutil_get-os`

if [ $OS = "sun4" ]
then
  MNTTAB=/etc/mtab
fi


#
# unmount floppy in case host thinks it's mounted already
#
cat $MNTTAB | egrep "$MOUNTPOINT" >/dev/null 2>&1
if [ $? -eq 0 ]
then
  umount $MOUNTPOINT
  sleep 2
  umount "$THISTERM:/$FFS"

  #
  # If still in MNTTAB something's wrong 
  # so don't try to mount it again - just exit
  #
  sleep 1
  cat $MNTTAB | egrep "$MOUNTPOINT" >/dev/null 2>&1
  if [ $? -eq 0 ]
  then
    exit 0
  fi
fi

#
#
# Get floppy mount preference from file in $HOME/.hpxt_floppy
# If preference is MOUNT return 0
# If preference is NOMOUNT return 1
# If preference is ASK prompt user what is preferred this time (login) 
# and return 0 if user wants to mount now, 1 otherwise 
# 
# If file does not exist:
# -  initiate dialogue with user
# -  get user preference
# -  store them in file for next time around
#

MNTRET=\
`su $USER -c $SHE <<EOF
$THISDIR/get_floppy_prefs $HOME/.hpxt_floppy
echo \\$?
EOF`

if [ "$MNTRET" != "0" ]
then
  exit 0
fi

#
# End of Get floppy mount preference from file 


#
# unmount the other "filesystems" from netstation in case
# Mpower or autofs or ? mounted them
#

if [ $OS = "hp" ]
then
  (
  sleep 10
  TMPPS=/tmp/ps.tmp.$THISTERM
  ps -ef | grep automount | grep -v grep > $TMPPS
  if [ -s "$TMPPS" ]
  then
    fgrep "$THISTERM:/usr/lib/X11/700X" $MNTTAB > /dev/null 
    if [ $? -eq 0 ]
    then
      #ADMPID=`cat $TMPPS | awk '$NF == "-hosts" {print $2}' | head -1`
      #ADIR=`cat $TMPPS | awk '$NF == "-hosts" {print $(NF-1)}' | head -1`
      #if [ ! -z "$ADMPID" -a -d "$ADIR" ]
      #then
      umount $THISTERM:/usr/lib/X11/700X
      umount $THISTERM:/$FFS
      umount $THISTERM:/rom

	  #
	  # it should be possible to inform automount about changes
	  # to mnttab. However, if you do, experience shows that 
	  # it will hang shortly thereafter.

        #sleep 5
        #kill -1 $ADMPID
      #fi
    fi
  fi
  exit 0
  ) &
fi


#
# If an automount seems to run our map just create symbolic link and exit
#

cat $MNTTAB | egrep '[(]pid.*/hpxtfd' > /dev/null 
if [ $? -eq 0 ]
then
  su $USER -c $SHE <<EOF

if [ -h "$LINKNAME" ]
then
  rm $LINKNAME
fi

if [ -d "$LINKNAME" -o -f "$LINKNAME" ]
then
  mv $LINKNAME $LINKNAME.orig.$$
else

  if [ ! -d "`dirname $LINKNAME`" ]
  then
    mkdir "`dirname $LINKNAME`"
  fi
fi

ln -s /hpxtfd/$THISTERM $LINKNAME

#
# make a: link for DOS "compatibility"
#
if [ ! -h "$HOME/a:" -a ! -d "$HOME/a:" -a ! -f "$HOME/a:" ]
then
  ln -s $LINKNAME $HOME/a:
fi


EOF
  exit 0
fi


  #
  # Create mountpoint if necessary
  #

if [ ! -d $MOUNTPOINT ]
then
  mkdir -p $MOUNTPOINT
fi


  #
  # set mount options as soft as possible 
  #

OPTIONS="-o soft,retry=2,retrans=6,timeo=150,rsize=4096,wsize=4096"

case $OS in
	sun4|aix) mount $OPTIONS $THISTERM:/$FFS $MOUNTPOINT ;;
	sun5)     mount -F nfs $OPTIONS $THISTERM:/$FFS $MOUNTPOINT ;;
	sco) 
                  OPTIONS="soft,retry=2,timeo=150,rsize=1024,wsize=1024"
	          mount -f NFS,$OPTIONS $THISTERM:/$FFS $MOUNTPOINT ;;
	hp10)     mount $OPTIONS $THISTERM:/$FFS $MOUNTPOINT ;;
	hp*)      mount $THISTERM:/$FFS $MOUNTPOINT $OPTIONS ;;
esac


   #
   # If mount failed clean up and 
   # exit 0 regardsless	
   #

if [ $? -ne 0 ]
then
  su $USER -c $SHE <<EOF

  if [ -h "$LINKNAME" ]
  then
    rm -f "$LINKNAME"
  fi
  if [ -h "$HOME:a" ]
  then
    rm -f "$HOME:a"
  fi
EOF
  echo "`date`: mount of floppy failed for user $USER on display $DISPLAY"
  exit 0
fi


  # 
  # Make floppy accessible to $USER only
  # 

chown $USER 	$PARENTDIR
chmod 700 	$PARENTDIR



  # 
  # Create $HOME/floppy pointing to $MOUNTPOINT
  # 

su $USER -c $SHE <<EOF

if [ -h "$LINKNAME" ]
then
  rm $LINKNAME
fi

if [ -d "$LINKNAME" -o -f "$LINKNAME" ]
then
  mv $LINKNAME $LINKNAME.orig.$$
else

  if [ ! -d "`dirname $LINKNAME`" ]
  then
    mkdir "`dirname $LINKNAME`"
  fi
fi

ln -s $MOUNTPOINT $LINKNAME

#
# make a: link for DOS "compatibility"
#
if [ ! -h "$HOME/a:" -a ! -d "$HOME/a:" -a ! -f "$HOME/a:" ]
then
  ln -s $LINKNAME $HOME/a:
fi

EOF


   #
   # exit 0 regardsless	
   #

exit 0
