#!/bin/sh
# 
#  Copyright (C) 1999-2000,2003,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.
#

##
##JCC(7/99)-initial version
##USAGE="usage: axbary (orbitFile infile outfile)"
##(7/31/00) - remove 'verbose'
##(4/2003)-rename  axBary to axbary_ar 

counter=0
#paramList="infile orbitfile outfile ra dec refframe verbose clobber"
paramList="infile orbitfile outfile ra dec refframe clobber"
autoParamList="infile orbitfile outfile"
enteredParamList=""

#check whether the parameters specified in the command line; 
#if not, use pquery to get it
while [ $# -gt 0 ]
do
    #--- count the parameters in the command line
    counter=`echo $counter | awk '{print $1 + 1}'`

    with_params=`echo $1 | grep =`

    if [ "$with_params" = "$1" ]   #-- param name was specified
    then
      param=`echo $1 | awk -F= '{print $1}'`    #--parameter name
      value=`echo $1 | cut -f2- -d'='`          #--parameter value
    else    #-- param name not specified
      param=`echo $paramList | cut -f$counter -d' '`
      value="$1"
    fi
    enteredParamList=`echo $enteredParamList $param`
    pset axbary ${param}="${value}" 
    shift
done

## get the values of "infile orbitfile outfile"
for in_params in $autoParamList
do
  in_or_out=`echo $enteredParamList | grep $in_params`
  if [ "$in_or_out" = "" ]  # need an entry of auto parm  
  then
      pquery axbary $in_params > /dev/null
  fi
done


#---get parameters from axbary.par ----
#orbit_file=`pquery axbary orbitfile`
#event_file=`pquery axbary infile`
#out_file=`pquery axbary outfile`
event_file=`pget axbary infile`
orbit_file=`pget axbary orbitfile`
out_file=`pget axbary outfile`
ra=`pget axbary ra`
dec=`pget axbary dec`
refframe=`pget axbary refframe`
#verbose=`pget axbary verbose`
clobber=`pget axbary clobber` # will return 'yes' for entry of 'y ye yes'
                              # will query if entry containing capital letter

#---out_file exists ? ----
if ( test -f $out_file )      # True if $out_file exists
then
  if ( test $clobber = "yes" )  
  then
      rm  $out_file
      echo "Clobber the existing output file : " $out_file
      echo ""
  else
      echo "Can't clobber the existing output file (clobber=no)"
      echo ""
      exit 1     # fatal error
  fi
fi

#---copy event_file to out_file
#dmcopy infile="$event_file" outfile=$out_file kernel=FITS verbose=0 option="" clobber=yes mode=ql

#---set a command line
#command="/home/arots/bin/axbary_ar -i ${orbit_file} -f ${out_file} -o ${out_file}"
command="axbary_ar -i ${orbit_file} -f ${event_file} -o ${out_file} "

if [ $ra != "INDEF" ]
then
command="$command -ra $ra "
fi

if [ $dec != "INDEF" ]
then
command="$command -dec $dec "
fi

if [ $refframe != "INDEF" ]
then
command="$command -refframe  $refframe "
fi

#command="$command -debug $verbose" 
command="$command -debug"

#if [ $verbose -gt 0 ] then
echo " "
echo $command
echo " "
#fi

eval $command

dmhistory "${out_file}" axbary action=put


