#!/bin/posix/sh
  #
  # xtutil_AddToExports
  #
  #  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.
  #

set -u				# Find undefined variables
set -a				# Export all new variables
. xtutil_scriptInit
set +a


if [ $# -ne 1 ]
then
  $NLECHO "missingArgNFS AddToExports $EXPORTS"
  exit 1
fi

terminalName=$1
fileToExport=$terminalDirectory

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

# Find out local file system of $1

FS2EXP=`xtutil_wherelives $fileToExport`	; exitIfNonZero $? 1 "$FS2EXP"
#
# File system exported?
#

osIsFSExported $FS2EXP

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

  #
  # make a back-up before modifying $EXPORTS
  #
  cp $EXPORTS /tmp/`basename $EXPORTS`.$$
  osMakeExportsLine $FS2EXP $terminalName >$TMP/line.$$
  exitIfNonZero $? 1 ""

  xtutil_userIO userQAddLineToExports $TMP/line.$$
  if [ $? -eq 0 ]
  then
    cat $TMP/line.$$ >> $EXPORTS
    exitIfNonZero $? 1 `$NLECHO "couldntAppendNFS $EXPORTS"`

    $NLECHO "lineAppendedNFS $EXPORTS"
  else
    rm $TMP/line.$$
    exit 1
  fi

  rm $TMP/line.$$

else	# File system IS already exported

  osExportsAccessCheck			; exitIfNonZero $? 0 ""

  #
  # Is it exported to the world?
  #
  NFSCLIENTS=`xtutil_nfsClients $FS2EXP`

  if [ "$NFSCLIENTS" = "" ]
  then
    $NLECHO "alreadyExported $FS2EXP \"world\" $EXPORTS"
    exit 0
  fi


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

  exitIfZero $? 0 `$NLECHO "alreadyExported $FS2EXP $terminalName $EXPORTS"`

  # 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
    $NLECHO "tooLongNFS $FS2EXP $EXPORTS"
    exit 1
  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=%&$terminalName:%" $EXPORTS > $TMP/$$
  grep "^$FS2EXP[ 	].*access=$terminalName:" $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=$terminalName&/" $EXPORTS > $TMP/$$
      grep "^$FS2EXP[ 	].*access=$terminalName[ 	]" $TMP/$$ > /dev/null
      if [ $? -ne 0 ]
      then
        # otherwise add to the end of the line
	sed "${LNUM}s/$/,access=$terminalName/" $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/[ 	]*#.*$/ $terminalName&/" $EXPORTS > $TMP/$$
      grep "^$FS2EXP[ 	].*[ 	]$terminalName[ 	]" $TMP/$$ > /dev/null
      if [ $? -ne 0 ]
      then
        # otherwise add to the end of the line
	sed "${LNUM}s/$/ $terminalName/" $EXPORTS > $TMP/$$
      fi
    fi
  fi

  cmp -s $EXPORTS $TMP/$$
  exitIfZero $? 0 `$NLECHO "tooDumbNFS $terminalName $EXPORTS $LNUM"`

  #
  # make a back-up before modifying $EXPORTS
  #
  cp $EXPORTS /tmp/`basename $EXPORTS`.$$
  $NLECHO "copyNFS $EXPORTS /tmp/`basename $EXPORTS`.$$"

  cp $TMP/$$ $EXPORTS 
  exitIfNonZero $? 1 `$NLECHO "couldntCopy $TMP/$$ $EXPORTS"`

  rm $TMP/$$ 

  $NLECHO "addedNFS $terminalName $EXPORTS"

fi

osExportFS $FS2EXP $terminalName

exit $?



