#!/bin/csh -f
#++++++++++++++++
# Copyright:    (C) 2008-2015 UDS/CNRS
# License:       GNU General Public License
#.IDENTIFICATION findenis
#.LANGUAGE       C-shell
#.AUTHOR         Francois Ochsenbein [CDS]
#.ENVIRONMENT    
#.KEYWORDS       
#.VERSION  1.0   02-Mar-2001
#.VERSION  1.1   04-Jun-2003: 2nd Version
#.VERSION  1.2   23-Sep-2005: 3rd versions -- all versions in same physical file
#.VERSION  1.2   31-Jul-2007: Accept Jnames
#.VERSION  1.3   01-Sep-2008: for proxy usage
#.PURPOSE        Find Stars in DENIS
#.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`

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 rdenis = "$rcon denis2"
if ($#argv < 1 || "$1" =~ *h*) then
    echo "Usage: $pgm [-p] J2000-center-decimal-degrees"
    echo "   or  $pgm [-p] -f [file_with_centers] [other_options]"
    echo "   or  $pgm [-p] - [other_options]            (data in stdin)"
    echo "Versions: denis1 (1999, Cat. II/240), denis2 (2003, Cat. II/252)"
    echo "          denis3 (2005, Cat. II/263)"
    echo "--------------------------------------------------------------------"
    $rdenis -help
    exit 1
endif

set titles = ( 	"1stRelease (1999Sep)" \
	     	"2ndRelease (2003Apr)" \
		"3rdRelease (2005Sep)" )
set rcalls = ( denis      denis2     denis3     )
set fcalls = ( denif      denif2     denif3     )

set file = ""
set vers = 3		# Defaut Version

if ($0 =~ *-p) then
    set vers = 1
endif
if ($0 =~ *[123]) then
    set vers = `echo $0 | rev | cut -c1`
    shift
endif

set   vtit = "$titles[$vers]"
set rdenis = "$rcon $rcalls[$vers]"
set fdenis = "$rcon $fcalls[$vers]"

echo "#======== DENIS $vtit server  ========== CDS, Strasbourg ========"

switch("$1")
case "-f":
    set file = $2 ; shift
    if ("$file" == "-") set file = ""	# Accept -f - (identical to -)
case "-":
    shift
    if( "$rcon" == "$cgi_con" ) then
        if( "$file" == "" ) then
            $rcon $rcalls[$vers] - $argv:q
        else
            $rcon $rcalls[$vers] -f $file $argv:q
        endif
        exit $status
    endif

    cat $file | $rcon putmp \; $fcalls[$vers] $* \; rmtmp
    exit $status
case "J[012]*":				# Use J-name
case "-*":
    #echo "$rdenis $*"
    $rdenis $argv:q
    exit $status
endsw

set call = "-s"
set show = cat
if ($?syscds) then
    setenv LESS '-BeiqPLine %lb (Space=next b=back q=Quit h=Help)?e (END). '
    set show = less
endif
$rdenis $call -c $argv:q | $show
exit $status
