#!/bin/bash -norc
###############################################################################
#
# Used to sweep OLDSOFT account.
#
# Author: Mike Zelazny 18-Feb-2011
# Mod:    Jingchen Zhou 22-May-2012
#         Added -d `echo $CVSROOT` to cvs update to overwrite CVS/Root so
#         that any CD can run sweep
###############################################################################
#
if [ ! `/usr/bin/whoami` = "fphysics" ] ; then
  echo Please sweep from the fphysics account.
else
  DT=`date "+%D - %T"`
  SLAC="@slac.stanford.edu"
  MAILLIST="sw-log$SLAC controls-software-reports$SLAC"
#
# Setup FACET Environment
#
    if [ -f /usr/local/facet/tools/script/ENVS_facet.bash ]; then
    . /usr/local/facet/tools/script/ENVS_facet.bash
#
# Log file
#
    if [ -z $PHYSICS_USER ]
    then
      user=`whoami`
    else
      if [ none = $PHYSICS_USER ]
      then
        user=`whoami`
      else
        user=$PHYSICS_USER
      fi
    fi
    date=`date`
    year=`echo $date | cut -d" " -f6`-
    month=`echo $date | cut -d" " -f2`-
    day=`echo $date | cut -d" " -f3`-
    time=`echo $date | cut -d" " -f4 | cut -c1-5`

    if [ -e /u1/facet/tools/sweep ]; then
      log_file=/u1/facet/tools/sweep/sweep-$user-$year$month$day$time.log
    else
      log_file=sweep-$user-$year$month$day$time.log
    fi
#
# Start the script
#
    echo "Sweeping /home/fphysics/OLDSOFT ($DT)" > $log_file
    pushd /home/fphysics/OLDSOFT > /dev/null
    chmod +w matlab
    pushd /home/fphysics/OLDSOFT/matlab > /dev/null
    chmod +w `find .`
    rm -f `find . | grep Entries.Static`
    cvs -d `echo $CVSROOT` update -P -d >> $log_file 2>&1
    chmod -w `find .`
    popd > /dev/null
    chmod -w matlab
    popd > /dev/null
#
# Update sweep time stamp
#
    caput SIOC:SYS1:ML01:AO100.PROC 1 > /dev/null
  else
    echo "Sorry, can't find FACET environment setup"
  fi

  cat $log_file | mailx -s "Swept FACET Matlab Files on ($DT)" $MAILLIST
  echo "FACET Sweep Complete.  See $MAILLIST for details."
fi
