#!/bin/sh
#@(#)HP X Station Admin Scripts
#
# wherelives
#  Get mount point of file system to which file/dir from $1 belongs
#  (to be used later to update /etc/exports in order for this X station
#  to be able to access file server via NFS.)
#

#
# check if silent specified on command line, ie. don't echo errors
#
if [ $1 != "-s" ]
then
	FILE=$1
	TALK=true

else
	FILE=$2
	TALK=false
fi


#
# do basic file/dir existence check
#
if [ ! -r $FILE ]
then
	if $TALK 
	then
	  echo "file/directory $FILE doesn't exist." 1>&2
	fi
	exit 1
fi

#
# Find mount point of filesystem for $FILE
#
if [ $OS = "sco" ]
then
  DEV=`df -v $FILE | tail -1 | awk '{print $2}'`
  MOUNTPOINT=`/etc/mount | grep $DEV | awk '{print $1}' | grep '.'`
else
  MOUNTPOINT=`$DFCMD $FILE | tail -1 | awk '{print $NF}' | grep '.'`
fi

if [ $? -ne 0 ]
then
  if $TALK
  then
	echo "Can't find the mount point of the filesystem for $FILE." 1>&2
  fi
	exit 5
fi


#
# Now make sure the mount point is a local file system
#
grep '^/' $MOUNTTABLE | grep $MOUNTPOINT > /dev/null

if [ $? -ne 0 ]
then
  if $TALK
  then
	echo "File/directory $FILE is not located on a locally mounted" 1>&2
	echo "disk drive on machine `hostname`." 1>&2
  fi
	exit 6
fi

echo $MOUNTPOINT

exit 0
