#! /bin/bash
# "@(#) $Id: ferosPackSessionTGZ,v 1.11 2005/10/04 21:54:28 jpritcha Exp $"

usage() {
  echo "Usage: ferosPackSessionTGZ [-hv] [-t tar.gz] [-F FF-pref] [-W WLC-pref] <NNNN> <MMMM>"
  echo " "
  echo "<NNNN> <MMMM>"
  echo "   File numbers as in session<NNNN>_<MMMM>.tar.gz"
  echo "-v"
  echo "   Verbose"

  exit ${exstat:-0}
}

FFpref="FF"
WLCpref="ThAr"

while [ -n "$1" ]
do
  case $1 in
    -h)   usage;;
    -t)   targz=$2; shift; shift;;
    -v)   vbose="v"; shift;;
    -F)   FFpref="${2}"; shift; shift;;
    -W)   WLCpref="${2}"; shift; shift;;
     *)   NNNN=$1; MMMM=$2; shift; shift;;
  esac
done

if [ -z "${NNNN}" ] || [ -z "${MMMM}" ]; then
  echo "Error!!! Must at least supply file numbers NNNN and MMMM as in session<NNNN>_<MMMM>.tar.gz"
  exit 1
fi

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

[ -z "${targz}" ] && targz=session${NNNN}_${MMMM}.tar.gz

if [ -e ${targz} ]; then
  ThArIm="${WLCpref}${NNNN}.mt"
  if [ ! -e "${ThArIm}" ]; then
    echo "Error!!! Can not find image ${ThArIm}"
    exstat=1
    usage
  fi
  FFIm="${FFpref}${MMMM}.mt"
# Get Variables from header...
  if [ ! -z "`gethead ${ThArIm} PROGID`" ]; then
## Probably a BIAS image...
    ProgId="`gethead ${ThArIm} PROGID`"
    YYYYMMDD="`gethead ${ThArIm} DATE-OBS`"
    TMSTART="`gethead ${ThArIm} TM_START`"
    ArchFile="`gethead ${ThArIm} ARCFILE`"
    DSoN="`dateStartofNight.pl $YYYYMMDD $TMSTART`"
    ArchTar="${ArchFile/fits/tar}"
  elif [ ! -z "`gethead ${ThArIm} 'HIERARCH ESO OBS PROG ID'`" ]; then
## Probably a FIERA image...
    ProgId="`gethead ${ThArIm} 'HIERARCH ESO OBS PROG ID'`"
    ArchFile="`gethead ${ThArIm} ARCFILE`"
#    dateObs="`gethead ${ThArIm} 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 ${ThArIm} MJD-OBS`"
    DSoN="`ferosMJD2DSofN.pl $mjd`"
    ArchTar="${ArchFile/fits/tar}"
  fi

# 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...
  if [ "${vbose:-0}" == "v" ]; then
    echo "create symbolic link ${ProgId}/${DSoN}/${targz} to ../../${targz}"
  fi
  ln -fs ../../${targz} ${ProgId}/${DSoN}/${targz}
# Make the tar file that will be used to copy across to VADRL
  tar --force-local -hc${vbose}f pack${targz/.gz} ${ProgId}/${DSoN}/${targz}
  gzip -9${vbose} --force pack${targz/.gz}

# Do FEROS QC?
## Not needed to be separate while QC & VA are both done by e2p2ops@w2p2off...
#  if [ "${FEROSDoQC:-N}" == "Y" ]; then
#    [ ! -e .QC ] && mkdir .QC
#    ln pack${targz} .QC/pack${targz}
#    ferosProcImage --pidir ${FEROS_DRS}/procImage/QC `pwd`/.QC/pack${targz}
#  fi

# Process tar file onto VADRL
  if [ "${FEROSProcImage:-N}" == "Y" ]; then
    ferosProcImage --pidir ${FEROS_DRS}/procImage/VA `pwd`/pack${targz}
  fi

fi

exit
