#!/bin/bash
#
listoftokens=("$@")
#associate parameter value tokens
declare -a param
declare -a value
declare -a datatype
declare -a unit
declare -i nparam
declare -i j

for (( i = 1 ; i < $# ; i+=2 ))
do
  j=i-1
  param[$nparam]=${listoftokens[$j]:1}
  value[$nparam]=${listoftokens[$i]}
  datatype[$nparam]="double"
# treat exceptions 1...
  case ${param[$nparam]} in
    "objectName"|"band"|"ra"|"dec"|"file") 
       datatype[$nparam]="string" ;;
    "bright"|"oldScenario") 
       datatype[$nparam]="boolean" ;;
  esac 
# treat exceptions 2...
  case ${param[$nparam]} in
    "ra")
       unit[$nparam]="HH:MM:SS.TT" ;;
    "dec") 
       unit[$nparam]="DD:MM:SS.TT" ;;
    "diffRa" |  "diffDec" | "radius" ) 
       unit[$nparam]="arcmin" ;;
    "baseMax")
       unit[$nparam]="m" ;;
    "wlen")
       unit[$nparam]="um" ;;
    "diamVK")
       unit[$nparam]="mas" ;;
  esac 
  nparam=nparam+1
done
#build xml file. no not write "<?xml version="1.0" encoding="UTF-8"?>"
cat <<EOF 
<config entityName="tagger.52100" to="JavaGui"></config>
<scl_command>
<VOTABLE xmlns="http://www.ivoa.net/xml/VOTable/v1.1"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1"
 ID="http://www.ivoa.net/xml/VOTable/v1.1
 http://www.ivoa.net/xml/VOTable/VOTable-1.1.xsd">
 <DESCRIPTION>
 SearchCal software: http://www.mariotti.fr/aspro_page.htm
 (In case of problem, please report to jmmc-user-support@ujf-grenoble.fr)
 Request parameters: 
EOF
echo $@
echo Generated on `date` 
echo \</DESCRIPTION\>
echo \<COOSYS ID=\"J2000\" equinox=\"J2000.\" epoch=\"J2000.\" system=\"eq_FK5\"/\>
echo \<RESOURCE name=\"SearchCal called by Aspro\"\>
echo \<TABLE\>
            
for (( i = 0 ; i < nparam ; i+=1 ))
do
  WORD="unit=\"${unit[$i]}\""
  HASUNIT="${unit[$i]:+${WORD}}"
  echo "<PARAM  datatype=\"${datatype[$i]}\" name=\"${param[$i]}\" value=\"${value[$i]}\" ${HASUNIT} />" 
done
cat <<EOF
<DATA>
<TABLEDATA>
</TABLEDATA>
</DATA>
</TABLE>
</RESOURCE>
</VOTABLE>
</scl_command>
EOF