#!/bin/csh -f
#++++++++++++++++
# Copyright:    (C) 2008-2017 UDS/CNRS
# License:       GNU General Public License
#.IDENTIFICATION find_cats
#.LANGUAGE       C-shell
#.AUTHOR         Francois Ochsenbein [CDS]
#.ENVIRONMENT    
#.KEYWORDS       
#.VERSION  1.0   19-Jan-2008
#.PURPOSE        Find results in large catalogues
#.COMMENTS       
#----------------
set D_BIN = `dirname $0`
set path = ($D_BIN /usr/local/bin /usr/bin /usr/ucb /bin)
set pgm  = `basename $0`
set call = `basename $0|tr -d find_`

# Decide to use wwwget or wget ?
# Proxy definition requires the firewall...
set acl_con = "aclient 130.79.129.161 1660"
set cgi_con = "aclient_cgi 130.79.129.161"

set rcon="$acl_con"
which aclient |& grep -v ' ' >& /dev/null # note SUN needs the grep
if( $status > 0 || $?http_proxy ) set rcon="$cgi_con"
set rpgm = "$rcon $call"

if ($#argv < 1 || "$1" =~ -h* || "$1" =~ hel* ) then
    echo "Usage: $pgm file_with_centers [catalog|option]..."
    echo "   or  $pgm -  [catalog|option]...          (data in stdin)"
    echo "--------------------------------------------------------------------"
    echo "Query one or several catalogues from a list of positions among:"
    $rpgm.list
    echo "--------------------------------------------------------------------"
    $rpgm.help
    exit 1
endif
if ("$pgm:e" == "gz") then
    set filter = "gzip -c"
else
    set filter = "cat"
endif

set file = ""

echo "#======== Multi-Survey Server =========== CDS, Strasbourg ========" \
| $filter

set go = 1
set rarg = ()

while (($#argv>0) && ("$file" == ""))
    if ("$1" == "-f") then		# file as next argument
	shift
        set file = "$1"
    else if ("$1" == "-") then
        set file = "$1"
    else if ("$1" =~ "-"*) then
        set rarg = ($rarg:q "$1")
    else if ("$1" =~ [0-9]*) then
        set rarg = ($rarg:q "$1")
    else
        set file = "$1"
    endif
    shift
end

if ("$file" == "")  then
    echo "#***Missing file argument" | $filter
    echo "#($0 -help for help)" | $filter
    exit 1
endif

if ("$file" != "-") then
    if (-r $file) then
        set ok = 1
    else
        echo "#***inaccessible file: $file" | $filter
        echo "#($0 -help for help)" | $filter
	exit 1
    endif
endif

if ("$filter" == "cat") \
echo "cat $file | $rcon putmp \; $call $rarg:q $argv:q \; rmtmp"
cat $file | $rcon putmp \; $call $rarg:q $argv:q \; rmtmp
exit $status
