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

#
# This script unmounts floppy from the HP Netstation
#
# The umount is done in the background after a sleep
# as active nfs mounts may still be present at execution time.
# (eg somebody "cd'ed" into the nfs mounted file system)
#
# Contrary to the documentation that is conveyed by the man page
# empiri shows that active mounts are gone **after** Xreset exits
# but may still be present while Xreset executes.
# The "umount" thus has better chances of succeeding after a delay. 
#
# Script can be run standalone by root but is normally called
# from Xreset by vuelogin/xdm when xdm/vue session has ended. 
# 

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 Xreset there is error checking
   #

if [ $# -eq 1 ]
then
  LINKNAME=$1
else
  if [ $# -eq 3 ]
  then
    DISPLAY=$1
    USER=$2
    LINKNAME=$3
  else
    echo wrong number of command line arguments
    echo 1=$1 2=$2 3=$3 4=$4
    exit 1
  fi
fi

#
# make sure it's an HP Netstation
#
XRDB="/usr/bin/X11/xrdb"
if [ ! -f "$XRDB" ]
then
  XRDB="/usr/openwin/bin/xrdb"
fi

#
# make sure we have a SHELL
#
if [ -x /bin/sh ]
then
    SHE="/bin/sh"
else
    SHE="/sbin/sh"
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 env var
#
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

MNTTAB=/etc/mnttab
FFS="hpxtfd"


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



 # mountpoint is /tmp_mnt.hpxt/Xstation_name/hpxtfd.
 # The symbolic link to mountpoint in $HOME/floppy is removed
 #

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

#
#  What is the Architecture?
#

if [ -x /bin/sun ]
then
  if /bin/sun
  then
    if [ `uname -r | awk -F'.' '{print $1}'` = "5" ]
    then
      OS=sun5
    else
      OS=sun4
      MNTTAB=/etc/mtab
    fi
  fi
else
  OS=`uname -m`
  if [ $OS = "i386" -o $OS = "i486" ]
  then
    OS=sco
  else
    OS=`uname`
  fi
fi

#
# remove symbolic link if present
#
if [ -h $LINKNAME -o -h "$HOME/a:" ]
then
su $USER -c $SHE <<EOF
  if [ -h $LINKNAME ]
  then
    rm $LINKNAME
  fi

  if [ -h "$HOME/a:" ]
  then
    rm "$HOME/a:"
  fi
EOF
fi

#
# If an automount seems to run our map just exit
#

cat $MNTTAB | egrep '[(]pid.*/hpxtfd' > /dev/null
if [ $? -eq 0 ]
then
  exit 0
fi


#
#  Anything mounted from this terminal?
#

if [ $OS != "AIX" ]
then
  cat $MNTTAB | egrep "$THISTERM:/$FFS" >/dev/null 2>&1
  if [ $? -ne 0 ]
  then
    # no just return
    exit 0
  fi
else
  mount | egrep "$THISTERM./$FFS" >/dev/null 2>&1
  if [ $? -ne 0 ]
  then
    # no just return
    exit 0
  fi
fi


  #	
  #	For HP-UX get rid of processes that did not go away,
  #     eg typically hpterm and descendants


if [ ${OS} = "hp" ]
then
(
  sleep 5
  #	

  for DP in `who -a | fgrep $THISTERM |awk '$1 == '\"$USER\"' {print $8"!"$7}'`
  do
    echo DP=$DP
	#
	# isolate DISPLAY part 
	#
    D=`echo ${DP} | cut -f1 -d'!' | sed 's/[:.]/ /g' | awk '{print $1}'`

      #
      # is it a process associated with this terminal?
      #
    if [ "$D" != "" -a "$THISTERM" != "" ]
    then
      if [ ${D} = ${THISTERM} ]
      then
        #
        # isolate process id part 
        #
        THISPID=`echo ${DP} | cut -f2 -d'!' | sed 's/[:.]/ /g' | awk '{print $1}'`
        #
	# does pid seem reasonable?
	#
	echo "$THISPID" | egrep '([a-z])|([A-Z])' > /dev/null
	if [ $? -eq 0 -o "$THISPID" = "" -o "$THISPID" -le "1" ]
	then
	  break
        fi

        #
	# Is process still there?
	#
        ps -p $THISPID > /dev/null 2>&1
        if [ $? -eq 0 ]
        then
          echo killing $THISPID
          kill "$THISPID" > /dev/null 2>&1
          kill "$THISPID" 2>&1 | grep "no such" > /dev/null
          if [ $? -ne 0 ]
          then
            sleep 1
            kill -9 "$THISPID"
          fi
        fi
      fi
    fi
  done

  for SEC in 5 10 20 60 
  do
    # Floppy still mounted?
    #
    cat $MNTTAB | egrep "$MOUNTPOINT" >/dev/null 2>&1
    if [ $? -ne 0 ]
    then
      break
    fi

    # yes, try to unmount it
    #
    umount $MOUNTPOINT 
    if [ $? -eq 0 ]
    then
      break
    else
      umount "$THISTERM:/$FFS"
      if [ $? -eq 0 ]
      then
        break
      fi
    fi
    sleep $SEC
  done

  exit 0

) &
else
  umount $MOUNTPOINT 
  if [ $? -ne 0 ]
  then
    umount "$THISTERM:/$FFS"
  fi
fi


   #
   # exit 0 regardsless	
   #

exit 0
