#!/bin/csh -f
#++++++++++++++++
# Copyright:    (C) 2008-2015 UDS/CNRS
# License:       GNU General Public License
#.IDENTIFICATION findpmm
#.LANGUAGE       C-shell
#.AUTHOR         Francois Ochsenbein [CDS]
#.ENVIRONMENT    
#.KEYWORDS       
#.VERSION  1.0   12-Mar-1997: for USNO-A1.0
#.VERSION  2.0   22-Oct-1998: Applies on USNO-A2.0
#.VERSION  2.3   01-Sep-2008: for proxy usage
#.PURPOSE        Find Stars in Monet's PMM (USNO-A2.0) Catalogue
#.COMMENTS       The "syscds" env. variable indicates an interactive usage (CDS)
#----------------
set D_BIN = `dirname $0`
set path = ($D_BIN /usr/local/bin /usr/bin /usr/ucb /bin)
set pgm = `basename $0`
if ($?MACHINE) then
    set path = ($path /usr/$MACHINE/bin)
endif

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 rpmm = "$rcon pmm2"
if ($#argv < 1 || "$1" =~ *h*) then
    echo "Usage: $pgm J2000-center [other_options]"
    echo "   or  $pgm -id PMM-ID [other_options]"
    echo "   or  $pgm -id - [other_options]        (PMM identifiers in stdin)"
    echo "   or  $pgm -f [file_with_data] [other_options]"
    echo "   or  $pgm - [other_options]            (data in stdin)"
    echo "--------------------------------------------------------------------"
    echo "Other options:"
    $rpmm -help | sed '1 s/Usage: pmm/          /'
    echo "--------------------------------------------------------------------"
    exit 1
endif

set file = ""

switch("$1")
case "-f":
    set file = $2 ; shift
case "-":
    shift
    if( "$rcon" == "$cgi_con" ) then
        if( "$file" == "" ) then
            $rcon pmm2 - $argv:q
        else
            $rcon pmm2 -f $file $argv:q
        endif
        exit $status
    endif

    cat $file | $rcon putmp \; pmm2f $argv:q \; rmtmp
    exit $status
case "-i*":
    if ("$2" == "-") then
        echo "   (PMM-identifiers transmitted from standard input)"
	shift ; shift
        if( "$rcon" == "$cgi_con" ) then
            $rcon pmm2 - -i $argv:q
            exit $status
        endif

	cat | $rcon putmp \; pmm2f -i $argv:q \; rmtmp
	exit $status
    endif
    $rpmm $argv:q
    exit $status
case "J[012]*":				# Use J-name
case "-*":
    echo "$rpmm $*"
    $rpmm $argv:q
    exit $status
endsw


set call = "-sr"
set show = cat
if ($?syscds) then
    setenv LESS '-BeiqPLine %lb (Space=next b=back q=Quit h=Help)?e (END). '
    set show = less
endif

echo ' ====== USNO-A2.0 server ========  Simbad, CDS, Strasbourg ========'

$rpmm $call -c $argv:q | $show
exit $status
