#!/bin/sh
#++++++++++++++++
# Copyright:    (C) 2008-2017 UDS/CNRS
# License:       GNU General Public License
#.IDENTIFICATION aclient_gen
#.LANGUAGE       Bourne
#.AUTHOR         Francois Ochsenbein [CDS]
#.ENVIRONMENT    CDSclient Scripts (generic)
#.KEYWORDS       
#.VERSION  1.0   23-Aug-2009: Bourne shell
#.PURPOSE        Generic definitions for findxxx
#.COMMENTS       The "CDSCLIENT" env. variable indicates node ,
#		 eventually preceded by 'http:'
#    $1 = name of calling program
#    $2 = Service to call
#----------------

# Decide the server
server=130.79.129.161	# Default server (cocat1.u-strasbg.fr)

if test $# -lt 3; then
    echo "Usage: $0 calling_name catalog_name catalog-specific-arguments..."
    tty -s && read -p "#---List of accessible large catalogs (hit Return)" junk
    aclient_cgi $server cats.gen
    exit 1
fi

D_BIN=`dirname $0`
PATH="$D_BIN:$PATH"
pgm=$1; shift
rpc=$1; shift
#echo "#pgm=$pgm; rpc=$rpc"

# Use http or aclient/aserver protocol? 
# Decide for aclient/aserver if http_proxy undefined and aclient exists
use_http=1
test `which aclient | wc -w` -eq 1 -a -z "$http_proxy" && use_http=0
case "$CDSCLIENT" in
   http) use_http=1; ;;
 http:*) use_http=1; server=`echo $CDSCLIENT | sed 's%http:/*%%'`; ;;
     "") ;;
      *) server="$CDSCLIENT"; ;;
esac
#echo "# use_http=$use_http"

if [ $use_http -eq 0 ]; then
    rcon="aclient $server 1660"
else
    rcon="aclient_cgi $server"
fi
rcall="$rcon $rpc"
#echo "rcall=$rcall"

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

#echo "#===cdsclient: $pgm"

case "$1" in
  -rhelp) # Remote help only
    echo "#--- Details on other options:"
    $rcall -help | grep -v "generated by cocat" \
    | sed 's/^Usage: [^ ]*/         /'
    exit 1 ;;

  -h*) # Help
    echo "Usage: $pgm J2000-center-decimal-degrees"
    echo "   or  $pgm -f [file_with_centers] [other_options]"
    echo "   or  $pgm -  [other_options]     (centers in stdin)"
    echo "--------------------------------------------------------------------"
    echo "#--- Details on other options:"
    $rcall -help | grep -v "generated by cocat" \
    | sed 's/^Usage: [^ ]*/         /'
    exit 1 ;;

   -) # Standard input as list of targets
    file="$1"; shift; ;;

  -f) # Input from a file
    file=$2; shift; shift; ;;

 -if) # IDs in a file
    opti="-i"; file=$2; shift; shift; ;;

  -i) # Is a single ID if followed by a number
    if [ "$2" = "-" ]; then
	opti="-i"; file=$2; shift; shift;
    elif [ "$2" = "-f" ]; then
	opti="-i"; file=$3; shift; shift; shift
    fi
    ;;

 [0-9]*:*)  # Position in sexagesimal
    call="-sr -e+s -c"; ;;	# Sort, sexagesimal edition, center

 [0-9]*)   # Position (don't care decimal or sexa)
    call="-sr -c"; ;;	        # Sort, center

esac

if test -z "$file" ; then
# No list of targets.
    echo "#...$rcall $call $@" 
    exec $rcall $opti $call "$@"
fi

# Here we must propagate the list of targets.
if [ $use_http -eq 0 ]; then	# aclient/aserver: save file, and exec
    tt=/tmp/cds$$			# Contains the foreign file name
    test "$file" = "-" && file=""	# "cat -" does not always work :-(
    echo "#...cat $file | $rcon save"
    cat $file | $rcon save > $tt
    ffile=`head -1 $tt`; awk 'NR>1{print "#...upload ==>",$0}' $tt; rm -f $tt
    echo "#...$rcall $opti -f $ffile $@"
    exec $rcall $opti -f $ffile "$@"
fi

# HTTP-mode: use aclient_cgi host catalog query-param query-param...
exec $rcall $opti -f "$file" "$@"
