#!/bin/bash

# query Simbad by object 
# objSim -h ObjectName ...  (pipe)
# NOTE:  this tool takes only ONE object at a time
# [It has to do with $@ coming through a pipe

#-----------------------------------------------------------------------
URL="http://simbad.u-strasbg.fr/simbad/sim-id?"
#-----------------------------------------------------------------------
HELP=;
#-----------------------------------------------------------------------


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

if [ $HELP ]; then
  echo "details of an object in the Simbad catalog"
  echo "objSim {-h} Objectname | pipe"
  echo "Objectname(s) should be quoted"
  exit
fi

#-----------------------------------------------------------------------
#input from pipe? if so, convert to positional parameters
#-----------------------------------------------------------------------

if [ -p /dev/stdin ]; then
    PIPE=$(cat -)
    set -- $PIPE
fi

#-----------------------------------------------------------------------
# process each object
#-----------------------------------------------------------------------

if [ $# -le 2 ]; then
    Object="$@"; shift 
    object=$(txt2uri "$Object")
    curl -s "${URL}output.format=ASCII&Ident=$object"
else
   echo "objSim: do not understand the position parameters"
fi