#!/bin/sh
#------------------------------------------------------------------------------
# Script to update the "speclog" product for one or more MJDs
# from the astrolog directory.  Files of interest are copied from
# $ASTROLOG_DIR/$MJD to $SPECLOG_DIR/$MJD.  Then a "cvs add" and "cvs commit"
# are performed in the $SPECLOG_DIR directory.
#
# Examples:
# Update the relevant log files from $ASTROLOG_DIR/52780 to $SPECLOG_DIR/52780:
#   % speclog_update 52780
# Do the same for the next three MJDs:
#   % speclog_update 52781 52782 52783
#
# D. Schlegel, Princeton, 18 Jun 2003
#
# Added a "-forcecvs" option. CPL 2008-05-30
#
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
# Test that certain environment variables are already set.

echo Begin SPECLOG_UPDATE at `date`

if [ -z "$ASTROLOG_DIR" ] ; then
  echo "ASTROLOG_DIR must be set!"
  exit
fi

if [ -z "$SPECLOG_DIR" ] ; then
  echo "SPECLOG_DIR must be set!"
  exit
fi

if [ -z "$CVS_RSH" ] ; then
  echo "CVS_RSH must be set!"
  exit
fi

if [ $ASTROLOG_DIR == $SPECLOG_DIR ] ; then
  echo "ASTROLOG_DIR and SPECLOG_DIR are identical, so do not do anything"
  exit
fi

if [ ! -d $SPECLOG_DIR/CVS ] ; then
  echo "Directory $SPECLOG_DIR/CVS not found, so do not do anything"
  exit
fi

datestring=`date`

# Allow -forcecvs as a 1st argument, to recover from situations where the files
# were copied but were not added to CVS.
if test "$1" == "-forcecvs"; then
    forcecvs=yes
    shift 1
fi

thismjd=$1
while [ -n "$thismjd" ] ; do
  echo Working on MJD=$thismjd
  indir=$ASTROLOG_DIR/$thismjd
  outdir=$SPECLOG_DIR/$thismjd

  if [ -d $indir ] ; then
    # First find the files that we want to copy
    files=`( \cd $indir; \ls guiderMon-$thismjd.par idWeather-$thismjd.par plPlugMapM-*.par sdHdrFix-$thismjd.par 2> /dev/null)`

    # Only continue if there are any files of interest in this directory.
    if [ -n "$files" ] ; then

      # Create the output directory, if not already there
      if [ ! -d $outdir -o "$forcecvs" == "yes" ] ; then
	if [ ! -d $outdir ]; then
	    echo Make directory $outdir
	    \mkdir $outdir
	fi
	(cd $SPECLOG_DIR; cvs add $thismjd)
	(cd $SPECLOG_DIR; cvs commit -m "Auto-added on $datestring" $thismjd)
      else
        echo Dir already exists $outdir
      fi

      for thisfile in $files; do
        if [ ! -e "$outdir/$thisfile" -o "$forcecvs" == "yes" ] ; then
	  if [ ! -e "$outdir/$thisfile" ]; then
            echo Adding file $outdir/$thisfile
            \cp $indir/$thisfile $outdir/$thisfile
	  fi
          (cd $outdir; cvs add $thisfile)
          (cd $outdir; cvs commit -m "Auto-added on $datestring" $thisfile)
        else
          echo File already exists $outdir/$thisfile
        fi
      done
    else
      echo No files of interest in dir=$indir
    fi
  fi
  shift 1
  thismjd=$1
done

echo End SPECLOG_UPDATE at `date`

#------------------------------------------------------------------------------
