#!/bin/sh
# 
# 
#  Copyright (C) 1999-2001,2005,2007,2022  Smithsonian Astrophysical Observatory
#
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

#
# This script reads in parameters and sends them to mtl_build_gti.
# It has to do some parsing so that arguments with spaces
# (ex: foo.fits[cols -time]) will be properly quoted.
#

counter=0
num_auto_params=8
param_list="infile outfile mtlfile userlimit lkupfile smooth clobber verbose"
entered_param_list=""

# soak up parameters in the command line
while test $# -gt 0;
do
  # keep track of the counter, matching parameter order
  counter=`echo $counter | awk '{print $1 + 1}'`

  with_param=`echo $1 | grep '\[.*='`
  if test "$with_param" = "$1"  # a parameter with DM filter present
  then
    with_params=`echo $1 | grep '=.*\['`
    if test "$with_params" = "$1" # param name was specified
    then
      param=`echo $1 | awk -F= '{print $1}'`
      value=`echo $1 | cut -f2- -d'='`
    else     # param name not specified
      param=`echo $param_list | cut -f$counter -d' '`
      value="$1"
    fi
  else    # no DM filter present
    with_params=`echo $1 | grep =`
    if test "$with_params" = "$1" # param name was specified
    then
      param=`echo $1 | awk -F= '{print $1}'`
      value=`echo $1 | cut -f2- -d'='`
    else     # param name not specified
      param=`echo $param_list | cut -f$counter -d' '`
      value="$1"
    fi
  fi

  # if the param is not automatic, counter=(counter - 1)
  is_auto_par=`echo $param_list | grep $param`
  if test "$is_auto_par" = ""  #not an auto parameter
  then
    counter=`echo $counter | awk '{print $1 - 1}'`
  else
    # take out the name of the param form entered_param_list
    for test_param in $param_list
    do
      if test "$test_param" = "$param"
      then
        entered_param_list=`echo $entered_param_list $test_param`
      else
        entered_param_list=$entered_param_list
      fi
    done
  fi

  case ${param} in

# The multi-value parameters which are read in as quoted strings must
# have their values re-quoted before feeding them to pset()

   infile | outfile | mtlfile | userlimit | lkupfile ) \
     pset dmgti ${param}="${value}" ;;

   *)  pset dmgti $1 ;;
  esac

  shift

done


# when the counter and num_auto_params are not the same,
# prompt the user for the autoparams, since there was a mismatch
if test $counter -lt $num_auto_params
then
  for in_params in $param_list
  do
    in_or_out=`echo $entered_param_list | grep $in_params`
    if test "$in_or_out" = ""  # has not been entered
    then
      pquery dmgti $in_params > /dev/null
    fi
  done
fi

# get the verbose parameter
my_verbose=`pget dmgti.par verbose`

if test $my_verbose -ge 2; then
  echo "starting mtl_build_gti"
fi

# get the automatic parameters
my_infile=`pget dmgti.par infile`
my_outfile=`pget dmgti.par outfile`
my_mtlfile=`pget dmgti.par mtlfile`
my_userlimit=`pget dmgti.par userlimit`
my_lkupfile=`pget dmgti.par lkupfile`
my_smooth=`pget dmgti.par smooth`
my_clobber=`pget dmgti.par clobber`

# build execution string, quoting each value
exestr="mtl_build_gti @@dmgti infile=\"$my_infile\" outfile=\"$my_outfile\" mtlfile=\"$my_mtlfile\" userlimit=\"$my_userlimit\" lkupfile=\"$my_lkupfile\" smooth=\"$my_smooth\" clobber=\"$my_clobber\" verbose=\"$my_verbose\""

# run mtl_build_gti
eval $exestr
exit_status=$?

if test $exit_status -ne 0
then
  exit $exit_status
fi

dmhistory "$my_outfile" dmgti action=put
exit $?
