#! /bin/tcsh -f
# "@(#) $Id: ferosPrepDateDir,v 1.6 2005/11/20 06:22:06 jpritcha Exp $"

set exstat=0
set defguess=0196
set guess=$defguess
set midUnit="99"

if ( ! $?FEROS_DRS ) echo "Warning\!\!\! environment variable FEROS_DRS does NOT exist."
set ssdir="${FEROS_DRS}/saved_session"
set swd="`pwd`"

while ( $#argv )
  switch ( $1 )
  case -h:
    goto usage
  case -c:
    set convLinks; shift; breaksw
  case -g:
    set guess=$2; shift; shift; breaksw
  case -r:
    set rawDataDir=$2; shift; shift; breaksw
  case --ssdir:
    set ssdir=$2; shift; shift; breaksw
  case -u:
    set midUnit=$2; shift; shift; breaksw
  case --templateDir:
    set templateDir=$2; shift; shift; breaksw
  default:
    set dateDir=$1; shift; breaksw
  endsw
end

if ( ! $?dateDir ) then
  echo "Error\!\!\! Must at least supply <dateDir>"
  set exstat=1
  goto usage
endif

set fmw=
if ( $?FEROS_MIDWORK ) then
  set fmw="-m $FEROS_MIDWORK"
else
  echo "Warning\!\!\! Environment variable FEROS_MIDWORK does NOT exist"
  if ( -d `pwd`/midwork ) then
    echo "Found `pwd`/midwork, setting FEROS_MIDWORK to `pwd`/midwork."
    setenv FEROS_MIDWORK `pwd`/midwork
    set fmw="-m $FEROS_MIDWORK"
  endif
endif

which drs >& /dev/null
if ( $status == 0 ) then
  set drs="drs"
else
  if ( $?MIDASHOME && $?MIDVERS ) then
    if ( -e $MIDASHOME/$MIDVERS/system/unix/drs ) set drs="$MIDASHOME/$MIDVERS/system/unix/drs"
  endif
endif
if ( ! $?drs ) then
  echo "Error\!\!\! Can not find MIDAS command drs."
  echo "It is normally located in:"
  echo "   <MIDASHOME>/<MIDVERS>/system/unix/drs"
  echo "Please either add it to your path (e.g. make a softlink to it) or set"
  echo "the environment variables MIDASHOME & MIDVERS."
endif

if ( $?MID_WORK ) then
  set saveMW="${MID_WORK}"
endif
setenv MID_WORK "$FEROS_MIDWORK"

set guessFileList="BLAZE.tbl ThAr50000.tbl ThAr${guess}_{{GORDER,INIT,ORDER,{LINE,WLC}{1,2}}.tbl,TEMPLATE.bdf}"

if ( ! -e ${FEROS_DRS}/Objects.tbl ) then
  if ( -e ${FEROS_DRS}/fmt/FEROS-Objects.fmt && -e ${FEROS_DRS}/dat/null.dat ) then
    echo "Warning\!\!\! Creating ${FEROS_DRS}/Objects.tbl"
#    inmidas ${fmw} -p ${midUnit} <<eomidas
#crea/tabl ${FEROS_DRS}/Objects.tbl 10 1 ${FEROS_DRS}/dat/null.dat ${FEROS_DRS}/fmt/FEROS-Objects.fmt
#exit
#eomidas
    $drs crea/tabl ${FEROS_DRS}/Objects.tbl 10 1 ${FEROS_DRS}/dat/null.dat ${FEROS_DRS}/fmt/FEROS-Objects.fmt
  else
    echo "Error\!\!\! Don't know how to make required init guess files.."
    echo "Error\!\!\! Can't find FEROS-Objects.fmt and/or null.dat"
    exit 1
  endif
endif

if ( ! -d $dateDir ) mkdir -pv $dateDir

if (( ! -e $dateDir/Objects.tbl ) && ( -e ${FEROS_DRS}/Objects.tbl )) then
  ln -fsv ${FEROS_DRS}/Objects.tbl $dateDir/Objects.tbl
endif

if ( $?templateDir ) then

  if ( -d $templateDir ) then
    which rsync >& /dev/null
    if ( $status ) then
      echo "Error\!\!\! --templateDir option requires rsync to be installed"
      exit 1
    endif
    rsync -av $templateDir/* $dateDir
  else
    echo "Error\!\!\! Can't find specified template directory $templateDir"
    exit 1
  endif

else

  set i=0
  foreach file ( $guessFileList )
    if ( ! -e ${FEROS_DRS}/guess${guess}/$file ) @ i++
  end
  if ( $i != 0 ) then
    if ( -e ${ssdir}/guess${guess}.tar.gz ) then
      # make required files...
      if ( ! -d ${FEROS_DRS}/guess${guess} ) mkdir -p ${FEROS_DRS}/guess${guess}
      set lcwd="`pwd`"
      cd ${FEROS_DRS}/guess${guess}
      ln -fvs  ${ssdir}/guess${guess}.tar.gz guess${guess}.tar.gz
      if ( $?convLinks ) symlinks -c .
      if ( $?convLinks ) symlinks -cs .
#    tar -zxvf guess${guess}.tar.gz
#    inmidas ${fmw} -p ${midUnit} <<eomidas
#@@ getguess ${guess}
#exit
#eomidas
      $drs @@ getguess ${guess}
      cd "${lcwd}"
    else
      # Don't know how to make required init guess files...
      echo "Error\!\!\! Don't know how to make required init guess files.."
      echo "Error\!\!\! Can't find guess${guess}.tar.gz"
      set exstat=1
      goto usage
    endif
  endif

## Make links...
  if ( ! -e ${FEROS_DRS}/BLAZE.tbl )     ln -fvs guess${guess}/BLAZE.tbl     ${FEROS_DRS}/BLAZE.tbl
  if ( ! -e ${FEROS_DRS}/ThAr50000.tbl ) ln -fvs guess${guess}/ThAr50000.tbl ${FEROS_DRS}/ThAr50000.tbl

  if ( ! -d $dateDir ) mkdir -pv $dateDir
  set lcwd="`pwd`"
  cd $dateDir
  foreach file ( $guessFileList )
    ln -fsv ${FEROS_DRS}/guess${guess}/$file $file
  end
  cd "${lcwd}"

endif

cd $dateDir
if ( $?convLinks ) symlinks -c .
if ( $?convLinks ) symlinks -sc .
if ( $?saveMW ) then
  set MID_WORK="${saveMW}"
endif

exit

usage:
echo "Usage: ferosPrepDateDir [-h] [-c] <dateDir> [-g <NNNN>] [--ssdir <save_session_dir>]"
echo "                        [--templateDir <dirspec>]"
echo " "
echo "<dateDir>"
echo "   Directory to prepare"
echo "-g <NNNN>"
echo "   The four digit index number of the initial guess session. Default is $defguess."
echo "-c"
echo "   Use symlinks programme to attempt convert absolute links to relative."
echo "--ssdir <save_session_dir>]"
echo "   Location of save_session directory containing guess archives."
echo "--templateDir <dirspec>"
echo "   Instead of processing a guess session, rsync a template directory"
echo "   rsync MUST be installed."
exit $exstat
