#!/bin/bash

#qsSim .. quick syummary of output of "csSim"
# qsSim [-v VERBOSITY] {-h} file | pipe
# input file is the output of csSim


#-----------------------------------------------------------------------
VERB=1; HELP=; 
	NREFMAX=30	#not call ADS nrefs > NREFMAX
JSON_FILE="qsSim.json"
#-----------------------------------------------------------------------
TF="OUT_qsSim.tmp" TFt=${TF}t 
:> $TF; : > $TFt 
trap "[ -e $TF ] && rm $TF; [ -e $TFt ] && rm $TFt" EXIT
#-----------------------------------------------------------------------

while getopts v:h OPTVAL 
do
    case $OPTVAL in
	v) VERB=$OPTARG;;
	h) HELP=1;;
	*) echo "qsSim -h for help"; exit -1;;
    esac
done
shift $((OPTIND-1))

if [ $HELP ]; then
  echo "qsSim [-v VERBOSITY] {-h} file | pipe"
  echo "-v [1] for top-level summary; 2 add IDs; 3 add references"
  echo "input file is supplied by csSim"
  exit
fi

#-----------------------------------------------------------------------
# check to see if data is being supplied by pipe
#-----------------------------------------------------------------------

case $# in
    0) if [ -p /dev/stdin ]; then
          set -- "/dev/stdin"                     #set $1=/dev/stdin
        else
          echo "no file given nor is there a trailing pipe"
	  exit -1;
       fi
       cat $1 > $TF; IF=$TF;;
    1) IF=$1;;
    *) echo "can accept only one file"; exit -1;;
esac

#-----------------------------------------------------------------------
#preparing json file
#-----------------------------------------------------------------------
printf "program\tqsSim\n" >>$TFt
printf "facility\tSimbad\n" >>$TFt


#-----------------------------------------------------------------------
#No source found 
#-----------------------------------------------------------------------

grep '^!! No astronomical' $IF > /dev/null 
if [ $? -eq 0 ]; then
    echo "qsSim: nada"
    awk '/coord/{print "ra\t"$2;print "dec\t"$3;print "name_simbad\tnada"}' $IF  >> $TFt
    radius=$(grep 'coord' $IF | sed  's/^.*radius://;s/arcsec//' |xargs)
    printf "radius\t%d" $radius >> $TFt
    col2json $TFt > $JSON_FILE
    exit
fi

#-----------------------------------------------------------------------
#One source found 
#-----------------------------------------------------------------------

Object=$(grep '^Object ' $IF)

if [ $? -eq 0 ]; then

    RADEC=$(grep '^Coordinates(ICRS' $IF | awk '{print $2,$3,$4,$5,$6,$7}' | coco -d -b)
    echo $RADEC | awk '{print "ra\t",$1; print "dec\t",$2}'  >> $TFt
 
    Name=$(echo $Object | sed 's/\(^Object \)\([^-]*\)\(.*$\)/\2/;s/^ *//;s/ *$//;s/.*/&/')
    Type=$(echo $Object | sed 's/\(^[^-]*---\)\([^-]*\)\(.*$\)/\2/;s/ //g')
    printf "name_simbad\t%s\n" "$Name"  >> $TFt
    printf "type\t%s\n" "$Type" >> $TFt
   
    ID=$(grep '^Identifiers' $IF)
    nID=$(echo $ID | sed 's;\(^Identifiers[^(]*(\)\([0-9]*\)\().*\);\2;')

    IDENTIFIERS=$(sed -n '/^Identifiers/,/^ *$/p' $IF | gsed '1d;s/^  *//;s/   */,/g' |xargs | sed 's/,$//' | awk -F"," 'BEGIN{printf("[")}{for (i=1;i<=NF;i++){sub(/^ */,"",$i);printf("\"%s\",",$i)};printf("\"%s\"]\n",$NF)}')
    printf "number_avatars\t%s\n" $nID >> $TFt
    printf "avatars\t%s\n" "$IDENTIFIERS" >> $TFt 

    Bib=$(grep '^Bibcodes' $IF)
    nBib=$(echo $Bib | sed 's/^Bibcodes[^(]*(//;s/) *(//;s/).*$//')  

    BIBCODE=$(sed -n '/^Bibcodes/,/^Measures/p' $IF | gsed '1d;$d;s/^ *//;s/ *$//;/^$/d')
    NBibCode=$(echo $BIBCODE | awk '{print NF}')
    printf "nbibliography\t%s\n" "$nBib" >> $TFt 
    printf "BibCodes\t" >>$TFt 

   sed -n '/^Bibcodes/,/^ *$/p' $IF | sed '1d;/^ *$/d;s/^ *//g' | xargs | awk 'BEGIN{printf "["}{for (i=1;i<NF;i++){printf("\"%s\",",$i)};printf("\"%s\"]\n",$NF)}' >> $TFt

   Notes=$(sed -n '/^Notes/,/^ *$/p' $IF | gsed '1d;/^ *$/d;s/\t//g'|tr '\n' ' ')
   printf "Notes\t%s" "$Notes"  >> $TFt

   if [ $VERB -ge 1 ]; then
      echo -e "Simbad obj:$Name type:$Type avatars:$nID refs: $nBib"
   fi
 
   if [ $VERB -eq 2 ]; then
      echo "Simbad: number of references: $NBibCode"
      echo "Notes: $Notes"
      exit
   fi
  
   if [ $VERB -ge 3 ]; then
      echo -n "Simbad: number of references: $NBibCode."
        if [ $NBibCode -le $NREFMAX ]; then 
          bc2top "$BIBCODE"
       else
	  echo "popular object. Not calling ADS"
       fi
   fi 

   col2json $TFt > $JSON_FILE
   exit 
fi

#-----------------------------------------------------------------------
#Mutiple objects found
#-----------------------------------------------------------------------

NumberOfObjects=$(grep '^Number of object' $IF)
if [ $? -eq 0 ]; then
    nSources=$(grep "Number of objects" $IF| awk -F":" '{print $NF}' | sed 's/ //g')
     echo $nSources " sources within error circle"
  if [ $VERB -ge 2 ]; then
    dline
    grep "|" $IF | awk -F'|' \
         '/^[^1-9]/{print $2,$3,$4,$12}
	  /^[1-9]/{print $2,$3,$4,$12;
		   if ($12>mxbib){mxbib=$12;mxobj=$3;mxoffset=$2;}}	
		  END{sub(/ *$/,"",mxobj); sub(/^ */,"",mxoffset)
	              print "\""mxobj"\"", mxoffset, mxbib}'
  fi

  if [ $VERB -ge 3 ]; then
	echo "hello $VERB"
  fi	
fi