#!/bin/csh -f
#
#  This script must be run as root.
#
#  usage:
#
#     mountalex [server] 
# 
#  FLAGS: 
#     -f      =   don't bother checking for root since "mount" is setuid
#     -u      =   just unmounts and exit
#     -s server           = server hostname 
#     -l locationtomount  = where to mount Alex - default is MOUNTPOINT (/alex)
#     -m path             = directory to find mount command in
#
#  To do both must currently do  -f -u 
#

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 UFLAG = 0
set FFLAG = 0
set SPMOUNTPATH = "null"
set LOCALMPOINT = DEFAULTMPOINT
set SERVERHOST  = DEFAULTSERVERHOST

while (.$1. =~ .-*.)  
    if (.$1. == ".-u.") then
        set UFLAG = 1
    else if (.$1. == ".-f.") then
            set FFLAG = 1
        else if (.$1. == ".-m.") then
            set SPMOUNTPATH = $2
            shift
            else if (.$1. == ".-l.") then
                set LOCALMPOINT = $2
                shift
                else if (.$1. == ".-s.") then
                    set SERVERHOST = $2
                    shift
                    else 
                        echo "Unknown flag $1"
                        exit -1
                    endif
                endif
            endif
        endif
    endif
    shift
end
    

set TIMEOUT = 2
if (.$1. != ..) then
    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"

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

if (! -d $LOCALMPOINT) then
    echo "Doing mkdir $LOCALMPOINT "
    /bin/rm -f $LOCALMPOINT >& /dev/null
    mkdir $LOCALMPOINT
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
    set LIB = ""
    foreach POSSIBILITY (/sbin /etc)
        if (-f $POSSIBILITY/mount) then
            set MOUNTPATH = $POSSIBILITY
        endif
    end
    set MARG = ""
else
    echo "Looks like CMU 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 MARG = "-F"
endif

if ($SPMOUNTPATH != "null") then
    set MOUNTPATH = $SPMOUNTPATH
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 $LOCALMPOINT/edu) then
    echo "unmounting Alex just to be safe."
    $MOUNTPATH/umount $LOCALMPOINT
#endif

if ($UFLAG == 1) 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 $MARG -o ro,nosuid,timeo=$TIMEOUT,retrans=1000,rsize=$TRANSFSIZE,soft,intr ${SERVERHOST}:/ $LOCALMPOINT

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}:/ $LOCALMPOINT
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 $LOCALMPOINT.  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 $LOCALMPOINT" 
/bin/ls $LOCALMPOINT


