#!/bin/sh
#++++++++++++++++
# Copyright:    (C) 2008-2017 UDS/CNRS
# License:       GNU General Public License
#.IDENTIFICATION find_gen1
#.LANGUAGE       Bourne shell
#.AUTHOR         Gilles Landais + Francois Ochsenbein [CDS]
#.VERSION  1.0   23-Apr-2012
#.PURPOSE        Generic 
#----------------

# Decide the server
server=130.79.129.161	# Default server (cocat1.u-strasbg.fr)
test -z "$CDSCLIENT" || server=$CDSCLIENT	#  Non-default server

file=""		# Input file
call=""		# Additional arguments required if '-c' missing
opti=""		# Option: input is a list of identifiers

U1="http://$server/viz-bin/catClient.sh?-source="

# -----------------------------------------------------------------------------
usage='Usage: find_gen1 catalog [-h] [-r[ds] radius] [-m max_records] 
       [-c center| -i # | -whole]'
#echo "#---There are $# arguments: $*"
help()
{
    echo "$usage"
    echo "Query catalog ($1) for a result in TSV (tab-separated-values)"
    echo "     -h: this help"
    echo "     -r: target radius in arcmin; -rs: in arcsec; -rd: in degrees"
    echo "     -m: max number of objects to retrieve"
    echo "     -c: target center in decimal or sexagesimal"
    echo " -whole: the entire catalog"
}
# -----------------------------------------------------------------------------

pos_unit="arcmin"
max_records=-1
center=""
radius=""
more=""
wget_arg=""

# 1st argument = Catalog Name
if [ $# -lt 1 ]
then
    echo "#***catalog name as arg#1 is missing"
    echo "$usage"
    tty -s && read -p "#---List of accessible large catalogs (hit Return)" junk
    aclient_cgi $server cats.gen
    exit 1
fi
C=$1; shift
U="$U1$C"

if [ $# -lt 1 ] ; then
    help $C
    exit 0
fi

while [ $# -gt 0 ]
do
    case "$1" in

     -c=*) center="$1"; shift
	  ;;

       -c*) 
	  if [ $1 = "-col" ] ; then
              shift
              more="$more&$1"; shift	# Assume another condition
	  else
	  center="-c=$2"; shift; shift
	  # Accept several arguments with components of position
	  while [  `expr "$1" : '[+-]*[0-9]'` -gt 0 ]; do
	      test `expr "$1" : '[0-9]'` -gt 0  && center="${center}:"
	      center="${center}$1"; shift
	  done
	  fi
	  ;;

      -h*) help $C; exit 0
	  ;;

      -m*) max_records=$2; shift; shift
	  ;;

      -i) center="-recno=$2"; shift; shift
          ;;

   -rec*=*) center="$1"; shift
          ;;

     -rec*) center="-recno=$2"; shift; shift
          ;;

      -rs=*) pos_unit="s";
           echo "option -rs= doesn't exit (try without '=')" >&2
           help
           exit 1
           ;;
      -rd=*) pos_unit="d";
           echo "option -rd= doesn't exit (try without '=')" >&2
           help
           exit 1
           ;;
      -r=*) pos_unit="m";
           echo "option -rd= doesn't exit (try without '=')" >&2
           help
           exit 1
           ;;

      -rs) pos_unit="s"; shift
	   radius="$1"; shift
           ;;
      -rd) pos_unit="d"; shift
	   radius="$1"; shift
           ;;

      -r*) pos_unit="m"; shift
	   radius="$1"; shift
           ;;

   -whole) center="$1=true"; shift
	   ;;

       -f) file=$2; shift; shift # Input of centers from a file
           wget_arg="--post-file=$file"
	   ;;

    [0-9]*)
          if [ ! -z "$center" ] ; then
	      echo "#***unknown argument: $1"
	      echo "$usage"
	      exit 1
	  fi
	  center="-c=$1"; shift
	  # Accept several arguments with components of position
	  while [  `expr "$1" : '[+-]*[0-9]'` -gt 0 ]; do
	      test `expr "$1" : '[0-9]'` -gt 0  && center="${center}:"
	      center="${center}$1"; shift
	  done
	  ;;

      *=*) more="$more&$1"; shift	# Assume another condition
           ;;

        *) echo "#***unknown argument: $1"
	   echo "$usage"
	   exit 1
	   ;;
    esac
done

if [ -z "$center" -a -z "$wget_arg" ]; then
    echo "$usage"
    exit 1
fi

if [ "$radius" != "" ]; then
    radius="&-c.r$pos_unit=$radius"
else
    radius=""
fi

if [ $max_records -gt 0 ]; then
    #max_records="&limit=$max_records"
    max_records="&-out.max=$max_records"
else
    max_records=""
fi

center=`echo $center|sed 's/[+]/%2b/'`

exec wget -q -O - $wget_arg "$U&$center$radius$max_records$more"
