-#!/bin/csh -f
+#!/bin/sh
# This script performs nfs updates on servers.
#
# $Header$
-# The following exit codes are defined and MUST BE CONSISTENT with the
-# MR error codes the library uses:
-set MR_NOCRED = 47836470
-set MR_MKCRED = 47836474
-set MR_TARERR = 47836476
-
-set path=(/etc /bin /usr/bin /usr/etc /usr/athena/etc)
-set nonomatch
-
-# The file containg the information for the update
-set TARFILE=/var/tmp/nfs.out
-# The directory into which we will empty the tarfile
-set SRC_DIR=/var/tmp/nfs.dir
-
-# Alert if the tarfile does not exist
-if (! -r $TARFILE) then
- exit $MR_TARERR
-endif
-
-# Create a fresh source directory
-rm -rf $SRC_DIR
-mkdir $SRC_DIR
-chmod 755 $SRC_DIR
-
-# Note that since MR is going to be exported, assuming .MIT.EDU is
-# incorrect. For now however, it is probably not worth the effort
-# to canonicalize the hostname, especially with the upcoming update
-# protocol redesign
-set uchost=`hostname | tr '[a-z]' '[A-Z]'`.MIT.EDU
-
-cd $SRC_DIR
-
-# Just extract everything since some of what we need exists as
-# hardlinks and tar doesn't deal well with extracting them in isolation.
-tar xf $TARFILE
-if ($status) exit $MR_TARERR
-
-foreach type (dirs quotas)
- echo "Installing ${type}:"
- foreach i ( ${uchost}*.${type} )
- if (-e $i) then
- # Convert the from the filename HOST.@dev@device.type to /dev/device
- set dev=`echo $i | sed "s,.${type},," | sed "s,${uchost}.,," | sed "s,@,/,g"`
- echo ${uchost}:$dev
- ./install_${type} $dev < $i
- if ($status) exit $status
- if ($type == quotas) ./zero_quotas $dev < $i
- if ($status) exit $status
- # save the files used here for later debugging
- mv $i /var/tmp
- endif
- end
-end
-
-# build new credentials files.
-rm -f /usr/etc/credentials.new
-cp ${uchost}.cred /usr/etc/credentials.new
-if ($status) exit $MR_NOCRED
-
-# After this point, if /tmp gets cleared out by reactivate (which
-# happens on a combined server/workstation) we don't care.
-
-mkcred /usr/etc/credentials.new
-if ($status) exit $MR_MKCRED
+# redirect output, and find the credentials directory (not robust, but
+# works for now).
+if [ -d /var/athena ] && [ -w /var/athena ]; then
+ exec >/var/athena/moira_update.log 2>&1
+else
+ exec >/tmp/moira_update.log 2>&1
+fi
+
+creddir=/var/athena
+if [ -d /usr/etc ]; then
+ creddir=/usr/etc
+fi
+
+# The following exit codes are defined and MUST BE CONSISTENT with the
+# error codes the library uses:
+MR_NOCRED=47836470
+MR_MISSINGFILE=47836473
+MR_MKCRED=47836474
+MR_TARERR=47836476
+
+PATH=/etc:/bin:/usr/bin:/usr/etc:/usr/athena/etc
+
+TARFILE=/var/tmp/nfs.out
+SRCDIR=/var/tmp/nfs.dir
+
+uchost=`hostname | tr '[a-z]' '[A-Z]'`
+echo $uchost | egrep -e "\." >/dev/null
+if [ $? != 0 ]; then
+ uchost=$uchost.MIT.EDU
+fi
+
+# Alert if the tar file does not exist
+test -r $TARFILE || exit $MR_MISSINGFILE
+
+# Make a temporary directory to unpack the tar file into
+rm -rf $SRCDIR
+mkdir $SRCDIR || exit $MR_MKCRED
+cd $SRCDIR || exit $MR_MKCRED
+
+# Extract everything
+tar xpf $TARFILE || exit $MR_TARERR
+
+for type in dirs quotas; do
+ echo "Installing ${type}:"
+ for i in $uchost.$type; do
+ if [ -f $i ]; then
+ # Convert from the filename HOST.@dev@device.type to /dev/device
+ dev=`echo $i | sed "s,.${type},," | sed "s,${uchost}.,," | \
+ sed "s,@,/,g"`
+ echo ${uchost}:$dev
+ ./install_${type} $dev < $i
+ if [ $? != 0 ]; then
+ exit $MR_NOCRED
+ fi
+ if [ ${type} = "quotas" ]; then
+ ./zero_quotas $dev < $i
+ if [ $? != 0 ]; then
+ exit $MR_NOCRED
+ fi
+ fi
+ mv -f $i /var/tmp
+ fi
+ done
+done
+
+# build new credentials files
+rm -f $creddir/credentials.new
+cp ${uchost}.cred $creddir/credentials.new
+if [ $? != 0 ]; then
+ exit $MR_NOCRED
+fi
+if [ -s $creddir/credentials.local ]; then
+ cat $creddir/credentials.local >> $creddir/credentials.new
+fi
+
+# After this point, if /tmp gets cleared out by reactivate (which happens
+# on a combined server/workstation), we don't care.
+mkcred $creddir/credentials.new
+if [ $? != 0 ]; then
+ exit $MR_MKCRED
+fi
# Try to install the files
-foreach e ( "" .dir .pag)
- mv -f /usr/etc/credentials.new$e /usr/etc/credentials$e
-end
+for e in "" .dir .pag; do
+ mv -f $creddir/credentials.new$e $creddir/credentials$e
+done
# If any of them didn't get installed, fail
-foreach e ( "" .dir .pag)
- if (! -e /usr/etc/credentials$e) exit $MR_NOCRED
-end
-
-
+for e in "" .dir .pag; do
+ if [ ! -f $creddir/credentials$e ]; then
+ exit $MR_NOCRED
+ fi
+done
# cleanup
-if ( -f $TARFILE ) rm -f $TARFILE
-if ( -d $SRC_DIR ) then
- cd $SRC_DIR/..
- rm -rf $SRC_DIR
-endif
-if ( -f $0 ) rm -r $0
+cd /
+rm -rf $SRCDIR
+test -f $TARFILE && rm -f $TARFILE
+test -f $0 && rm -f $0
exit 0