#!/bin/bash -norc
###############################################################################
#
# Used to sweep OLDSOFT account.
#
# Author: Mike Zelazny 18-Feb-2011
#
###############################################################################
#
if [ ! `/usr/bin/whoami` = "physics" ] ; then
  echo Please sweep from the physics account.
else
  DT=`date "+%D - %T"`
  SLAC="@slac.stanford.edu"
  MAILLIST="sw-log$SLAC controls-software-reports$SLAC"
#
# Setup LCLS Environment
#
    if [ -f /usr/local/lcls/tools/script/ENVS.bash ]; then
    . /usr/local/lcls/tools/script/ENVS.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/lcls/tools/sweep ]; then
      log_file=/u1/lcls/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/physics/OLDSOFT ($DT)" > $log_file
    pushd /home/physics/OLDSOFT > /dev/null
    chmod +w matlab
    pushd /home/physics/OLDSOFT/matlab > /dev/null
    chmod +w `find .`
    rm -f `find . | grep Entries.Static`
    rm -f `find . | grep CVS/Root`
    cvs update -d -P >> $log_file 2>&1
    chmod -w `find .`
    popd > /dev/null
    chmod -w matlab
    popd > /dev/null
#
# Update sweep time stamp
#
    caput SIOC:SYS0:ML01:AO100.PROC 1 > /dev/null
  else
    echo "Sorry, can't find LCLS environment setup"
  fi

#  cat $log_file | mailx -s "Swept Matlab Files on ($DT)" $MAILLIST
# See CATER 137513 - filter out control characters

  tr -d '\000-\011\013\014\015\016-\037' < $log_file | mailx -s "Swept Matlab Files on ($DT)" $MAILLIST

  echo "Sweep Complete.  See $MAILLIST for details."
fi
