#!/bin/sh
#++++++++++++++++
#.IDENTIFICATION oldsim
#.LANGUAGE       C-shell
#.AUTHOR         Francois Ochsenbein [CDS]
#.ENVIRONMENT
#.KEYWORDS
#.VERSION  1.0   02-Sep-2008
#.VERSION  1.1   24-Aug-2011: First trial without asking simbad
#.VERSION  1.2   04-Nov-2013: Adapt to various Simbad outputs
#.VERSION  1.3   09-May-2014: Separator (sep) is '!'
#.VERSION  1.4   06-Jun-2014: Return status 0 (not 1) in case of not-found
#.PURPOSE        Find Stars in Old Versions of Simbad
#.COMMENTS       Possible argument  -S simbad.cfa.harvard.edu
#----------------

#http://simbad.u-strasbg.fr/simbad/sim-normid?ident=

test -z "$SIMBAD_SERVER" && SIMBAD_SERVER=simbad.u-strasbg.fr
test -z "$verbop" && verbop=0

if test $# -eq 0; then
    echo "Usage: $0 [-S server-name] identifier"
    echo "Default server: simbad.u-strasbg.fr"
    exit 0
fi

while test $# -gt 0 ; do
  case "$1" in
   -h*) exec $0;;	# Will execute the help (no argument)
    -S) SIMBAD_SERVER=$2; shift; shift; continue;;
   -v*) verbop=1; shift; echo "#...RSIMBAD0_STRICT=$RSIMBAD0_STRICT"; 
        export verbop; continue;;
  esac
  break
done

sep="!" # sep=":"
if test -z "$RSIMBAD0_STRICT"; then
    RSIMBAD0_STRICT=0; export RSIMBAD0_STRICT
    ID="$@"
else
   test $verbop -gt 0 &&
     echo "#...wwwget -s -q http://$SIMBAD_SERVER/simbad/sim-normid ident='$*'"
   ID=`wwwget -s -q http://$SIMBAD_SERVER/simbad/sim-normid "ident=$*" \
     | sed 's/[*]/\\\*/g'`
   test $verbop -gt 0 && 
        echo "#...normalized name: $ID"
   RSIMBAD0_STRICT=`expr $RSIMBAD0_STRICT + 1`; export RSIMBAD0_STRICT
   test $RSIMBAD0_STRICT -gt 1 && sep=""
fi

# vsim may be used to specify a given frozen version
# Return code is 0 if source found, 1 if error, 2 if nothing found
test -z "$vsim" && vsim=0

# CGI case : encode + in URL  (G.Landais 29-jul-2019)
if test -n "$QUERY_STRING" ; then
    ID=`echo $ID|sed 's/+/ /g'|sed 's/%2[bB]/+/g'`
fi

echo "#...aclient simbad.u-strasbg.fr 1660 sim.arg $vsim ${ID}$sep"
aclient simbad.u-strasbg.fr 1660 sim.arg $vsim "$ID"$sep \
| awk '\
function pr() { print t U J P A; if (nr>0) print "#B", nr; print "";
                t=""; U=""; J=""; P=""; A=""; nr=0; dr=1 }
function gJ(s){ n=split(s,a," / "); if(n>1) P=""; i=index(a[1],"["); 
  if(i==0) J="\n%J " a[1];
  else { J="\n%J " substr(a[1],1,i-1); P="\n%J.E " substr(a[1],i)}
  if(n>=2) { if(match(a[3], /[0-9]/)>0) P=P "\n%V " a[3] }
  if(n>=3) { if(match(a[4], /[0-9]/)>0) P=P "\n%X " a[4] } }
/^==/{ gsub(/=/, ""); print "#===", $0; next}
/^%#Parfile/{ p++; if(NR<4){ while($0 != ""){ print "#Skipped:",$0; getline }}}
/^%[U@]/{ if(nU==0) print "=\t!=+="; nU++; printf "=+/\t!";
          t=" " substr($1,2); U= "\n%U " substr($0,4); next }
/^%#B/ { if(nr==0) { nr=$2+0; dr=0 } next }
/^%B / { nr += dr; next} 
/^%J / { t = t " J"; gJ(substr($0,4)); next }
/^%J.E /{ J = J "\n" $0; next }
/^%JD/ { t =t " JD"; gJ(substr($0,5)); next }
/^%[PXV]/{ if(match($0,/[0-9]/)==0) next; if(P!="") next }
/^%M [A-Za-z]/{ sub(/M /, "M.") }   # Magnitude (but not %M value)
/^%F /{ sub(/F /, "M.") }   # Flux = magnitude
/^%/ { m=" " substr($1,2); if(index(t,m)==0) t = t m; A = A "\n" $0; next }
/^$/ { if(t!="") pr(); next }
     {  A = A "\n" $0; next }
 END { if(t!="") pr(); if(p==0) exit(1); if(nU==0) exit(2) }'
st=$?
if test $st -eq 2 -a $RSIMBAD0_STRICT -lt 2 ; then
    $0 "$@"; st=$?
    if test $st -eq 2 ; then
	echo "!! Identifier not found in frozen-Simbad: $@"
	st=0
    fi
fi
exit $st
