#!/bin/csh -f
#++++++++++++++++
# Copyright:    (C) 2008-2017 UDS/CNRS
# License:       GNU General Public License
#.IDENTIFICATION simref
#.LANGUAGE       C-shell
#.AUTHOR         Francois Ochsenbein [CDS]
#.ENVIRONMENT    
#.KEYWORDS       
#.VERSION  1.0   18-Feb-1993
#.VERSION  1.1   25-Nov-1995: Allow writing like "A+A, 295, 66"
#.VERSION  1.2   27-Jun-2006: Option -f
#.VERSION  1.3   26-Jan-2007: Simbad4
#.VERSION  1.4   17-Sep-2009: option -head => show header
#.VERSION  1.5   21-Nov-2014: Accept options (-v, -tag, etc)
#.PURPOSE        Get All Texts from Simbad References
#.COMMENTS       
#----------------
set D_BIN = `dirname $0`
set path = ($D_BIN /usr/local/bin /usr/bin /usr/ucb /bin)

set acl_con = "aclient cdsarc.u-strasbg.fr 1660"
set cgi_con = "aclient_cgi cdsarc.u-strasbg.fr"

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 verbop = 0
if ($#argv < 1) then
    echo "Usage: `basename $0` [-v] [-cite] {Simbad-Reference|-f file_name}"
    echo "-cite: add in the output a citation (%q)"
    echo "    -f References are contained in file_name (- for standard input)"
    echo "A SIMBAD Reference is written YYYYJJJJJ.v.pA"
    echo "  with YYYY = Year"
    echo "      JJJJJ = Journal abbreviation",
    echo "      v=Volume, p=Page, A=1st letter of 1st Author"
    exit 1
#else
#    set call = `echo "$*" | tr '&/,: ' '+....' | tr -s .`
endif


#set verbose
set arg = ()
while ("$1" =~ "-"*)
    if ("$1" =~ "-v"*) then
	set arg = ($arg:q "$1")
        set verbop = 1; shift
	echo -n "#...`basename $0`"
	continue
    endif
    if ($verbop)  echo -n " $1"
    if ("$1" == "-f") then
        shift
        if ($verbop) then
	    echo ""
  	    echo "#... cat $* | $rcon savbib"
	endif
        set Ufile = `cat $* | $rcon savbib`
        if ($#Ufile < 1) then
            echo "****simref: could'nt transmit the file $*"
	    exit 1
        endif
        if ($verbop) then
  	    echo "#==> $Ufile"
	    echo "#...$rcon simref $argv:q -in $Ufile[1]"
	endif
        $rcon simref $argv:q -in $Ufile[1]
        exit $status
    endif
    if (("$1" =~ "-h"*) && ("$1" !~ "-head"*)) then	# assume -help
        exec $0
    endif
    set arg = ($arg:q "$1") ; shift
end
set call = `echo "$*" | tr '&/,: ' '+....' | tr -s .`
if ($verbop) echo "$call"
set arg = ($arg:q $call:q)

if ($verbop) echo "#...$rcon simref $arg:q "
$rcon simref $arg:q 
exit $status
