#! /bin/bash
# "@(#) $Id: ferosPackRedFiles,v 1.17 2006/01/19 13:06:09 jpritcha Exp $"

usage() {
  echo "Usage: ferosPackRedFiles [-hv] <NNNN>"
  echo " "
  echo "<NNNN>"
  echo "   File number as in fero<NNNN>.mt"
  echo "-v"
  echo "   Verbose"

  exit $exstat
}

FFHVArchiveDir=${FEROSFFHVArchiveDir:-/data/E2P2OPS/FEROS-Archive/FFHV}

while [ -n "$1" ]
do
  case $1 in
    -h)   usage;;
    -v)   vbose="v"; shift;;
     *)   NNNN=$1; shift;;
  esac
done

if [ ${NNNN:-0} == 0 ]; then
  echo "Error!!! Must at least supply file number NNNN as in fero<NNNN>.mt"
  exit 1
fi

if [ -e ${INTROOT}/bin/ferosSetEnvVars ]; then
  . ${INTROOT}/bin/ferosSetEnvVars
fi

# Get Variables from header...
  if [ ! -z "`gethead fero${NNNN}.mt PROGID`" ]; then
## Probably a BIAS image...
    ProgId="`gethead fero${NNNN}.mt PROGID`"
    YYYYMMDD="`gethead fero${NNNN}.mt DATE-OBS`"
    TMSTART="`gethead fero${NNNN}.mt TM_START`"
    ArchFile="`gethead fero${NNNN}.mt ARCFILE`"
    DSoN="`dateStartofNight.pl $YYYYMMDD $TMSTART`"
    ArchTar="${ArchFile/fits/tar}"
  elif [ ! -z "`gethead fero${NNNN}.mt 'HIERARCH ESO OBS PROG ID'`" ]; then
## Probably a FIERA image...
    ProgId="`gethead fero${NNNN}.mt 'HIERARCH ESO OBS PROG ID'`"
    ArchFile="`gethead fero${NNNN}.mt ARCFILE`"
#    dateObs="`gethead fero${NNNN}.mt DATE-OBS`"
#    ymd="`echo $dateObs | cut -b1-10`"
#    hh="`echo $dateObs | cut -b12-13`" ; hh=`printf %d $hh`
#    mm="`echo $dateObs | cut -b15-16`" ; mm=`printf %d $mm`
#    ss="`echo $dateObs | cut -b18-19`" ; ss=`printf %d $ss`
#    (( tmstart = hh * 3600 + mm * 60 + ss ))
#    DSoN="`dateStartofNight.pl $ymd $tmstart`"
    mjd="`gethead fero${NNNN}.mt MJD-OBS`"
    DSoN="`ferosMJD2DSofN.pl $mjd`"
    ArchTar="${ArchFile/fits/tar}"
  fi

FFHVImInfoFile="FFHV.${ArchFile/FEROS./}"
FFHVImInfoFile="${FFHVImInfoFile/.fits/.info}"
if [ -e ${FFHVArchiveDir}/$DSoN/${FFHVImInfoFile} ]; then
  FFHVImFitsFile="`cat ${FFHVArchiveDir}/$DSoN/${FFHVImInfoFile}`.gz"
  ln -fs${vbose} ${FFHVArchiveDir}/$DSoN/${FFHVImInfoFile} ${FFHVImInfoFile}
  ln -fs${vbose} ${FFHVArchiveDir}/$DSoN/${FFHVImFitsFile} ${FFHVImFitsFile}
  [ ! -e $FFHVImFitsFile ] && sleep 60
fi

for file in \
    {f,o,bf,bo}${NNNN}{,ext}{1,2}.mt \
    {f,o,bf,bo}${NNNN}{1,2}00??.mt \
    {f,o}${NNNN}{1,2}*.ps \
    noFF{f,o,bf,bo}${NNNN}{,ext}{1,2}.mt \
    noFF{f,o,bf,bo}${NNNN}{1,2}00??.mt \
    noFF{f,o}${NNNN}{1,2}*.ps \
    efficiency${NNNN}{1,2}.{mt,ps} \
    response${NNNN}{1,2}.{mt,ps} \
    fiberratio${NNNN}.mt \
    r.${ArchFile/.fits}.????.fits \
    efficiency.${ArchFile/.fits}.{1,2}.fits \
    response.${ArchFile/.fits}.{1,2}.fits \
    fiberratio.${ArchFile/.fits}.fits \
    r.${ArchFile/.fits}.*.ps \
    ${FFHVImInfoFile} \
    ${FFHVImFitsFile} \
    ; do
  [ -e $file ] && flist="$flist $file"  
done

if [ "${flist}" != "" ]; then
  [ ! -z ${vbose} ] && echo "File list to ADD = $flist"

# Make Directories if necessary
  if [ ! -d "${ProgId}" ]; then
    mkdir "${ProgId}"
  fi
  if [ ! -d "${ProgId}/${DSoN}" ]; then
    mkdir "${ProgId}/${DSoN}"
  fi

# Make the tar file with the data...
## Check if something else is (e.g. TCCD image archiving) appending to/creating
## the tar file... wait till modification time is more than 10 secs ago...
  tSinceLastMod=0
  [ -e ${ProgId}/${DSoN}/${ArchTar} ] && (( tSinceLastMod = `date +%s` - `stat -t ${ProgId}/${DSoN}/${ArchTar} | cut -d\  -f13` ))
  while [ -e ${ProgId}/${DSoN}/${ArchTar} ] && [ $tSinceLastMod -lt 10 ]; do
    sleep 10
    (( tSinceLastMod = `date +%s` - `stat -t ${ProgId}/${DSoN}/${ArchTar} | cut -d\  -f13` ))
  done
## Check if something else (e.g. TCCD image archiving) is gzipping the tar
## file... wait till modification time is more than 10 secs ago...
  AppendCreate="c"
  tSinceLastMod=0
  if [ -e ${ProgId}/${DSoN}/${ArchTar}.gz ]; then
    (( tSinceLastMod = `date +%s` - `stat -t ${ProgId}/${DSoN}/${ArchTar}.gz | cut -d\  -f13` ))
    [ "${FEROSConcatTarFiles}" == "Y" ] && AppendCreate="A"
  fi
  while [ -e ${ProgId}/${DSoN}/${ArchTar}.gz ] && [ $tSinceLastMod -lt 10 ]; do
    sleep 10
    (( tSinceLastMod = `date +%s` - `stat -t ${ProgId}/${DSoN}/${ArchTar}.gz | cut -d\  -f13` ))
  done
## Ok, now we can safely work on the file... (provided everything else that
## tries to append to/create/gzip the tar file has similar safeguards built in...
  if [ -e ${ProgId}/${DSoN}/${ArchTar}.gz ]; then
    if [ "${AppendCreate}" == "A" ]; then
      gzip -d${vbose} ${ProgId}/${DSoN}/${ArchTar}.gz
    else
      \rm -f ${ProgId}/${DSoN}/${ArchTar}.gz
    fi
  fi
  tar --force-local -h${AppendCreate}${vbose}f ${ProgId}/${DSoN}/${ArchTar} $flist
  if [ "${FEROSDRStars2DFS}" == "Y" ]; then
    [ ! -d /data/reduced/${DSoN} ] && mkdir -p${vbose} /data/reduced/${DSoN}
    cp -a${vbose} ${ProgId}/${DSoN}/${ArchTar} /data/reduced/${DSoN}/${ArchTar}
  fi
  gzip -9${vbose} --force ${ProgId}/${DSoN}/${ArchTar}
  [ "${vbose}" == "v" ] && echo "create symbolic link ${ProgId}/${DSoN}/fero${NNNN}.tar.gz to ${ArchTar}.gz"
  ln -fs ${ArchTar}.gz ${ProgId}/${DSoN}/fero${NNNN}.tar.gz
# Make the tar file that will be used to copy across to VADRL
  tar -c${vbose}f packfero${NNNN}.tar ${ProgId}/${DSoN}/${ArchTar}.gz ${ProgId}/${DSoN}/fero${NNNN}.tar.gz
  gzip -9${vbose} --force packfero${NNNN}.tar

# Do FEROS QC?
## Not needed to be separate while QC & VA are both done by e2p2ops@w2p2off...
#  if [ "${FEROSDoQC:-N}" == "Y" ]; then
#    if [ ! -z "`ls efficiency${NNNN}?.mt 2>/dev/null`" ] || \
#      [ ! -z "`ls fiberratio${NNNN}.mt 2>/dev/null`" ]; then
#      [ ! -e .QC ] && mkdir .QC
#      ln packfero${NNNN}.tar.gz .QC/packfero${NNNN}.tar.gz
#      [ "${vbose}" == "v" ] && echo "Submitting to procImage: [ferosProcImage --pidir ${FEROS_DRS}/procImage/QC pwd/.QC/packfero${NNNN}.tar.gz]"
#      ferosProcImage --pidir ${FEROS_DRS}/procImage/QC `pwd`/.QC/packfero${NNNN}.tar.gz
#    fi
#  fi

# Process tar file onto VADRL
  if [ "${FEROSProcImage:-N}" == "Y" ]; then
    [ "${vbose}" == "v" ] && echo "Submitting to procImage: [ferosProcImage --pidir ${FEROS_DRS}/procImage/VA pwd/packfero${NNNN}.tar.gz]"
    ferosProcImage --pidir ${FEROS_DRS}/procImage/VA `pwd`/packfero${NNNN}.tar.gz
  fi

else
  [ ! -z ${vbose} ] && echo "File list empty, nothing to do..."
fi

exit
