#!/bin/csh -f
#
#  This script must be run as root.
#  
#  If given a "-u" it just unmounts and exits.
#

echo "mountalex"

# We are more patient with remote servers (we assume remote if they give
#    us a hostname with dots in it)
# Timeout values are in tenths of a second (just how soon to retry)
#
set TIMEOUT = 2
if (.$1. == ..) then
    set SERVERHOST = DEFAULTSERVERHOST
else 
    set SERVERHOST = $1
    echo $SERVERHOST | grep '\.' 
    if ($status == 0) then
       echo "Using slower 1 second retries since full hostname"
       set TIMEOUT = 10
    endif
endif

echo "planning on mounting $SERVERHOST with timout $TIMEOUT/10 sec"

set EFFUSER = `whoami`
if (($EFFUSER != root ) && ($EFFUSER != rootl)) then
    echo "You must run mountalex as root or rootl"
    exit -1
endif

if (! -d /alex) then
    echo "Doing mkdir /alex "
    /bin/rm -f /alex >& /dev/null
    mkdir /alex
endif

if (-x /usr/misc/.afs/bin/sys) then
    set ATSYS = `/usr/misc/.afs/bin/sys`
else
    if (-x  /usr/vice/bin/sys) then
         set ATSYS = `/usr/vice/bin/sys`
    else
         set ATSYS=""
    endif
endif

echo $ATSYS | grep mach > /dev/null
if ($status != 0) then
    echo "Non-Mach"
    set MOUNTPATH=/etc
    set MFLAG = ""
else
    echo Mach
    set MOUNTPATH=/usr/misc/.nfs/etc

#   if on a server machine we need to get rid of the rfs links to be able to mount
    if (-f /usr/alexsrvr/bin/grepnkill9) then
        /usr/alexsrvr/bin/grepnkill9 rfs
        sleep 5
    endif

    if (! -e $MOUNTPATH/mount) then
        echo "I am going to run modmisc to install mount command - just wait."
        /usr/cs/etc/modmisc - -release alpha cs.misc.nfs
    endif

    echo "Note that at CMU doing an NFS mount probably screws up df - don't worry be happy".
    set MFLAG = "-F"
endif

set TRANSFSIZE=8192
echo $ATSYS | grep -i aix > /dev/null
if ($status == 0) then
    echo "Looks like AIX - using small transfer size"
    set TRANSFSIZE=1000
endif
echo "Using a transfersize of $TRANSFSIZE"

#if (-e /alex/edu) then
    echo "unmounting Alex just to be safe."
    $MOUNTPATH/umount /alex
#endif

if ($1 == "-u") then
    exit 0
endif

echo "About to mount Alex."

# Some of these options don't really make a difference:
#  F       -  nfs mount may screw up /etc/mtab at CMU
#  ro      -  this server will not let you write even if you don't mount it this way
#  nosuid  -  right now Alex does not show any files as executable, let alone suid 
#
# Timeo is in tenths of a second.
# Retrans should be high since sometimes FTPs take a long time.

$MOUNTPATH/mount $MFLAG -o ro,nosuid,timeo=$TIMEOUT,retrans=1000,rsize=$TRANSFSIZE,soft,intr ${SERVERHOST}:/ /alex

if ($status != 0) then
    echo "It seems the mount failed so I will try a simpler one "
    $MOUNTPATH/mount -o timeo=$TIMEOUT,retrans=1000,rsize=$TRANSFSIZE,soft,intr ${SERVERHOST}:/ /alex
endif

# if your system only gets the first 8k of a file you may want to add a
# rsize=1000 
#
# alex.sp.cs.cmu.edu   is really BURGUNDY.NECTAR.CS.CMU.EDU
# which is 128.2.209.13

if ($status != 0) then
    echo 'Drat.  The mount did not work.'
    echo 'If you see an error of "device busy" it means that some process someplace'
    echo 'has a current directory under /alex.  If it is a shell, change the'
    echo 'current directory, etc. then try mountalex again.'
endif

echo "As a test I will do an    ls /alex" 
/bin/ls /alex


