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

if [ ! -f $EXPORTS ]
then
  touch $EXPORTS
fi

if [ $# -ne 1 ]
then
  echo "Missing argument in call to $0, not doing NFS, - $EXPORTS unchanged."
  exit 2
fi

# Find out local file system of $1

FS2EXP=`wherelives $1`

if [ $? -ne 0 ]
then
  exit 3 
fi

#
# File system exported?
#

if [ $OS = "sun5" ]
then
  awk '$1 == "share" && $NF == "'$FS2EXP'" {print $NF}' $EXPORTS \
		| grep $FS2EXP  > /dev/null 2>&1
else
  egrep "^${FS2EXP}([ 	]|$)" $EXPORTS > /dev/null 2>&1 
fi


if [ $? -ne 0 ]
then
  #
  # no, do it now
  #

  #
  # make a back-up before modifying $EXPORTS
  #
  cp $EXPORTS /tmp/`basename $EXPORTS`.$$
  echo "a copy of $EXPORTS file has been saved in /tmp/`basename $EXPORTS`.$$"
  echo ""

  if [ "$OS" = "hp" -a $RELEASE -lt 9 ]
  then
      echo "$FS2EXP	$SHORTNAME" > $TMP/line.$$
  else
    if [ $OS = "sun5" ]
    then
      echo "share -F nfs $FS2EXP"          > $TMP/line.$$ 
    else
      echo "$FS2EXP	-access=$SHORTNAME" > $TMP/line.$$
    fi
  fi

  cat $TMP/line.$$
  $ECHO "Append the above line to $EXPORTS? [y|n] (y): $NL"
  REPLY=`line`
  case $REPLY in
  y|Y|yes|YES|Yes|"")
      cat $TMP/line.$$ >> $EXPORTS
      if [ $? -ne 0 ]
      then
  	  echo "Couldn't append to $EXPORTS, - $EXPORTS unchanged."
  	  exit 5
      fi
      echo "line appended to $EXPORTS."
      
      ;;
  *)
      echo "OK .. no NFS, $EXPORTS unchanged. "
      rm $TMP/line.$$
      exit 4
      ;;
  esac

  rm $TMP/line.$$

else	# File system IS already exported

  if [ "$OS" != "hp" -o \( "$OS" = "hp" -a $RELEASE -lt 9 \) ]
  then
    egrep 'access=' $EXPORTS > /dev/null
    if [ $? -ne 0 ]
    then
      echo "$FS2EXP seems to be exported to \"world\" already, $EXPORTS unchanged"
      exit 0
    fi
  fi

    


  if [ $OS = "sun5" ]
  then
    awk '$1 == "share" && $NF == "'$FS2EXP'" {print $NF}' $EXPORTS \
		| cut -d# -f1 | egrep '(rw=)|(ro=)' > /dev/null 2>&1
    if [ $? -eq 0 ]
    then
      echo "It appears that $SHORTNAME could be added to $EXPORTS:"
      echo "but this script is not smart enough to edit it."
      echo "Please edit $EXPORTS manually and issue appropiate \"share\" command."
      echo ""
      exit 0
    else
      echo "$FS2EXP seems to be exported to \"world\" already, $EXPORTS unchanged"
      exit 0
    fi
  fi

  #
  # Is it exported to the world?
  #
  NFSCLIENTS=`egrep "^$FS2EXP([ 	]|$)" $EXPORTS | \
	cut -d# -f1 | \
	sed -e "s%^$FS2EXP[ 	]*%%" \
		-e '/root=/!s/[-,]ro[ 	]*//' \
		-e 's/[-,]async[ 	]*//' \
		-e 's/[-,]anon=[0-9]*[ 	]*//' \
		-e 's/[-,]rw=.*[, 	]*//'`

  if [ "x$NFSCLIENTS" = "x" ]
  then
    echo "filesystem $FS2EXP already exported to \"world\", $EXPORTS unchanged"
    echo ""
    exit 0
  fi


  #
  # Is file system already exported to this terminal?
  #
  echo "$NFSCLIENTS" | \
	egrep "([ 	=:]|^)$SHORTNAME([ 	:,]|$)" > /dev/null 2>&1

  if [ $? -eq 0 ]
  then
    echo "filesystem $FS2EXP already exported to $SHORTNAME, $EXPORTS unchanged"
    echo ""
    exit 0
  fi


  # A line exists for this file system but the terminal
  # has not been added to the access list yet.
  #
  # First calculate which line has the file system.
  #
  LNUM=`grep -n "^${FS2EXP}[ 	]" $EXPORTS | tail -1 | cut -f1 -d:`

  #
  # See if line seems too long 
  #
  CC=`sed -n "${LNUM}p" $EXPORTS | wc -c` 
  if [ $CC -gt 240 ]
  then
    echo "$FS2EXP line in $EXPORTS seems too long, please update manually."
    exit 6
  fi

  #
  # Add the X terminal name to the line for the file system
  #
  # First try adding to an existing -access option
  sed "${LNUM}s%^$FS2EXP[ 	].*access=%&$SHORTNAME:%" $EXPORTS > $TMP/$$
  grep "^$FS2EXP[ 	].*access=$SHORTNAME:" $TMP/$$ > /dev/null
  if [ $? -ne 0 ]
  then
    # check if any other options (except async)
    cut -d# -f1 $EXPORTS | grep "^$FS2EXP[ 	].*[-,][^a]" > /dev/null
    # if other options present, add an access option
    if [ $? -eq 0 ]
    then
      # add ahead of the comment, if present
      sed "${LNUM}s/[ 	]*#.*$/,access=$SHORTNAME&/" $EXPORTS > $TMP/$$
      grep "^$FS2EXP[ 	].*access=$SHORTNAME[ 	]" $TMP/$$ > /dev/null
      if [ $? -ne 0 ]
      then
        # otherwise add to the end of the line
	sed "${LNUM}s/$/,access=$SHORTNAME/" $EXPORTS > $TMP/$$
      fi
    else # no options present, just add the name of the X terminal
      # add ahead of the comment, if present
      sed "${LNUM}s/[ 	]*#.*$/ $SHORTNAME&/" $EXPORTS > $TMP/$$
      grep "^$FS2EXP[ 	].*[ 	]$SHORTNAME[ 	]" $TMP/$$ > /dev/null
      if [ $? -ne 0 ]
      then
        # otherwise add to the end of the line
	sed "${LNUM}s/$/ $SHORTNAME/" $EXPORTS > $TMP/$$
      fi
    fi
  fi

  cmp -s $EXPORTS $TMP/$$
  if [ $? -eq 0 ]
  then
    echo""
    echo "It appears that $SHORTNAME could be added to the following line in $EXPORTS:"
    sed -n "${LNUM}p" $EXPORTS
    echo "but this script is not smart enough to edit it."
    echo "Please edit $EXPORTS manually."
    echo ""
    exit 0
  fi	

  #
  # make a back-up before modifying $EXPORTS
  #
  cp $EXPORTS /tmp/`basename $EXPORTS`.$$
  echo "a copy $EXPORTS file has been saved in /tmp/`basename $EXPORTS`.$$"


  cp $TMP/$$ $EXPORTS 
  if [ $? -ne 0 ]
  then
    echo "Couldn't copy $TMP/$$ to $EXPORTS."
    exit 11
  fi
  rm $TMP/$$ 

  echo "$SHORTNAME has been added to $EXPORTS."

fi

if [ $OS = "sun5" ]
then
  share -F nfs $FS2EXP
  exit 0
fi

if [ -x $EXPORTFS ]
then
  $EXPORTFS -a
  RETCODE=$?
  if [ $RETCODE -ne 0 ]
  then
    echo "\"exportfs -a\" failed ($RETCODE). Couldn't export $FS2EXP to $SHORTNAME."
   exit 12
  fi
fi

exit 0
