#!/bin/csh -f
#
#  This script will install a new release of Alex in /usr/alexsrvr
#  and start up the server.  The user "alexsrvr" must exist
#  before this script is run.  This will also kill off any 
#  existing nfsd so do not run this on a machine that exports
#  NFS files like an NFS fileserver.
#
#  If there is an old Alex server running and mounted then 
#  this script should be run as "alexsrvr", if not it should be 
#  run as "root" so that we can mount alex and also start 
#  everything up after it is compiled.
#
#  The optional argument is a release name like alex.alpha.tar.Z 
#  The default is alex.tar.Z

echo "args = $0 $1"

set ALEXSRVR      = alexsrvr
set ALEXSRVRHOME  = ~$ALEXSRVR

if ($status != 0) then
    echo "It seems the user $ALEXSRVR has not yet been created."
    echo "Please make that user and then run getalex again."
    exit -1
endif

set Result = 0

set RELEASEDIR = /alex/edu/cmu/cs/sp/alex/src

set EFFUSER = `whoami`

if (($EFFUSER != $ALEXSRVR) && ($EFFUSER != root) && ($EFFUSER != rootl)) then
    echo "This script needs to be run as either: alexsrvr, root, or rootl"
    set Result = -1
    goto theend
else 
    echo "Looks like we are running with right user id"
endif

echo "Checking that /alex is mounted someplace"
if (! -d /alex/edu) then
  if (($EFFUSER != root) && ($EFFUSER != rootl)) then
      echo "ERROR: /alex is not working and I need to be run as root to fix that"
      set Result = -1
      goto theend
  else 
      mkdir /alex
      if (-f /usr/misc/.nfs/etc/mount) then
          set MOUNT = /usr/misc/.nfs/etc/mount
      else 
          if (-f /etc/mount) then
              set MOUNT = /etc/mount
          else
              set MOUNT = mount
          endif
      endif
      $MOUNT -o timeo=30,retrans=300,rsize=1000,soft,intr alex.sp.cs.cmu.edu:/ /alex
      if (! -d /alex/edu) then
          $MOUNT -o timeo=30,retrans=300,rsize=1000,soft,intr furmint.nectar.cs.cmu.edu:/ /alex
          if (! -d /alex/edu) then
              $MOUNT alex.sp.cs.cmu.edu:/ /alex
              if (! -d /alex/edu) then
                  echo "ERROR Could not mount alex"
                  set Result = -1
                  goto theend
              endif
          endif
      endif
  endif
endif

echo "/alex now exists"

echo "Forcing update of directory information"
cat $RELEASEDIR/.alex.update >& /dev/null

set MASTERGR = $RELEASEDIR/getalex

echo "Checking to see if this is the current version of getalex"
if (-f $MASTERGR) then
    diff $0 $MASTERGR > /dev/null
    if (($status != 0) && ($0 != $MASTERGR)) then
        if ( `ls -tL $0 $MASTERGR | head -1` != $0 ) then
            echo "We have an old version - going to use the new one"
            /bin/csh -f $MASTERGR $1
            exit $status
        else
            echo "It seems we are running a modified getalex."
            echo "Send vac@cs.cmu.edu the modifications you had to make."
        endif
    else
         echo "We are running the current version of getalex"
    endif
endif

if (($EFFUSER == root) || ($EFFUSER == rootl)) then
    echo "We are going to run as $ALEXSRVR so file ownership is right."

    set NORU = 0
    which ru > /dev/null
    if ($status != 0) then
        set NORU = 1
    else 
#       My "which" always exits with a $status of 0 - a bug to work around 
        which ru | grep -i 'no ru' > /dev/null
        if ($status == 0) then 
            set NORU = 1
        endif
    endif
    if ($NORU == 1) then 
        echo "We are going to do a funny su as an ru substitute."
        echo "When we are done you may need to do a control-D."
        (echo "/bin/csh -f $MASTERGR $1"; cat) | su $ALEXSRVR
    else
        ru $ALEXSRVR /bin/csh -f $MASTERGR $1
    endif


    if ($status != 0) then
        echo "Seems had a problem while $ALEXSRVR was running the getalex script"
        set Result = -1
        goto theend
    else
        echo "Finished with su"
    endif
    goto restartalex
endif



# special release or just the default?
if (.$1. == ..) then
    set ALEXRELEASE = $RELEASEDIR/alex.tar.Z
else
    set ALEXRELEASE = $RELEASEDIR/$1
endif


# In case we are using our own alexd we first copy the release then replace old
cd $ALEXSRVRHOME

mv alexsrvr alexsrvr.`/bin/date | awk '{print $2_$3_$4}'`
zcat $ALEXRELEASE | tar xvf -
if ($status != 0) then
    echo "Tar seems to have had trouble - we are exiting"
    set Result = -1
    goto theend
endif

echo "Finished the TAR"

set RELSIZE = `du alexsrvr | tail -1 | awk '{print $1}'`
echo "Release size is $RELSIZE"
if ($RELSIZE < 700) then
    echo "Don't believe that the alex release is this small $RELSIZE KB"
    set Result = -1
    goto theend
endif

set OLDDIR = old.`/bin/date | awk '{print $2_$3_$4}'`
mkdir $OLDDIR
mv -f man src doc $OLDDIR 
echo "Have moved the old directories off to $OLDDIR"

set N = alexsrvr
mv -f $N/man $N/src $N/doc .
echo "Have moved the new directories in"

if (! -d lib) then
    echo "Looks like there is no old lib so bringing in new"
    mv -f $N/lib .
endif

if (! -d etc) then
    echo "Looks like there is no old etc so bringing in new"
    mv -f $N/etc .
endif


# see if he has done this before
if (-f $OLDDIR/src/local.h) then
    echo "Will assume all your local defines are in local.h"
    cp $OLDDIR/src/local.h src/local.h
else
if (-f $OLDDIR/src/config.h) then
#   He has done this before - see if his old config file is same as new
    mv src/config.h src/config.h.new
    cp $OLDDIR/src/config.h src/config.h
    diff src/config.h src/config.h.new
    if ($status != 0) then
        echo "Your old file < src/config.h is a bit different from the new > src/config.h.new"
        echo -n "Should I just use your old one < (o) or just the new > (n) or exit (x)? [o] "
        set INPUT = $<
        if ($INPUT == x) then
            echo "OK I will exit.  After fixing up config.h run make install."
            set Result = -1
            goto theend
        endif
        if ($INPUT == n) then
            mv src/config.h src/config.old
            mv src/config.h.new src/config.h
        endif
    else
        echo "Looks like you did not modify config.h"
    endif
#                                                       other INPUTs just fall through
    diff $OLDDIR/src/Makefile src/Makefile
    if ($status != 0) then
        echo "Your old makefile < is different from the new one > ."
        echo -n "Should I just use your old < (o) one or the new one > (n) or exit (x)? [n]"
        set INPUT = $<
        if ($INPUT == x) then
            echo "OK I will exit.  After fixing up config.h run make install."
            set Result = -1
            goto theend
        endif
        if ($INPUT == o) then
            mv src/Makefile src/Makefile.new
            cp $OLDDIR/src/Makefile src/Makefile 
        endif
    else
       echo "Looks like you did not modify the Makefile"
    endif
else
    echo "Looks like you have not installed this before so I'll try"
endif
endif 


# Lets compile and run
if ($EFFUSER == $ALEXSRVR) then
    tail -1 $ALEXSRVRHOME/src/alex.h

    echo "Going to run make"
    if (! -d $ALEXSRVRHOME/bin) mkdir $ALEXSRVRHOME/bin

    echo "Going to run make but without rpcgen"
    (cd $ALEXSRVRHOME/src; make norpcgen; make install)
    if ($status == 0) goto lookinggood

    echo "Looks like make had trouble with the full release - let me just try the basics"
    (cd $ALEXSRVRHOME/src; make norpcgen; make installbasic)
    if ($status == 0) goto lookinggood

    echo "Going to run make with rpcgen"
    (cd $ALEXSRVRHOME/src; make clean; make install)
    if ($status == 0) goto lookinggood

    echo "Seems we can not compile without your help.  Drat."
    echo "It is all in your hands now.  Please help the next person by"
    echo "by sending vac@cs.cmu.edu diffs for any changes you have to make."
    set Result = -1
    goto theend

lookinggood:
    echo "Think we compiled things ok"
    echo "Going to kill alexd if it is running so alex.nanny starts up new alexd"
    $ALEXSRVRHOME/bin/grepnkill alexd 

    set TMPDIR   = `$ALEXSRVRHOME/bin/defs tmpdir`
    if (-e $TMPDIR) then
        echo "Going to kill evictor.nanny"
        $ALEXSRVRHOME/bin/grepnkill evictor.nanny
        $ALEXSRVRHOME/bin/grepnkill alex-cache
        $ALEXSRVRHOME/bin/grepnkill printinfo

        echo "Going to restart evictor.nanny"
        /bin/rm -f $TMPDIR/evictor.nanny.log
        $ALEXSRVRHOME/bin/evictor.nanny > $TMPDIR/evictor.nanny.log &
    endif

    goto theend
endif


restartalex:

echo "Going to stop and restart Alex to be sure we are using all new code"
if (($EFFUSER == root) || ($EFFUSER == rootl)) then
    $ALEXSRVRHOME/bin/start.alex 
else
    $ALEXSRVRHOME/bin/grepnkill alexd
    sleep 60
endif


theend:
    echo "I will start an ls /alex in the background"
    echo " "
    /bin/ls /alex &
    echo " "
    echo "Your current directory has become "
    pwd

    if ($Result == 0) then

        echo "Shall I remove $ALEXSRVRHOME/old.* and $ALEXSRVRHOME/alexsrvr.* ?"
        echo -n "(y)es or (n)o? [y]"
        set INPUT = $<
        if ($INPUT != n) then
            cd $ALEXSRVRHOME
            /bin/rm -rf $ALEXSRVRHOME/old.*
            /bin/rm -rf $ALEXSRVRHOME/alexsrvr.*
        endif

        echo " "
        echo "User information is in /alex/edu/cmu/cs/sp/alex/user"
        echo " "
        echo "Run $ALEXSRVRHOME/bin/mountalex on other machines to mount Alex on them."    
        echo " "
        echo "Send mail to alex-servers-request@cs.cmu.edu to be added to the Alex mailing list."
        echo " "
    endif

    exit $Result

