#!/bin/csh -f
#
#  This script must be run as root.

echo "mountalex"

# be a bit more patient with remote servers
if (.$1. == ..) then
    set SERVERHOST = sphinx.warp.CS.CMU.EDU
    set TIMEOUT = 3
else 
    set SERVERHOST = $1
    set TIMEOUT = 10
endif

echo "planning on mounting $SERVERHOST"

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

    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 "Blindly unmounting Alex just to be safe."
    echo "I will remove any core file after umount finishes (don't, worry be happy)"
    $MOUNTPATH/umount /alex
    /bin/rm -f core
#endif

echo "About to mount Alex."
echo "There is a good chance that mount will work but give an error message anyway."

# 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, so 0.2 seconds for each retransmit.
# 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


