#! /bin/csh -f

# Script to make html table row for a single JSOC dataseries
# Makes  keyword file with name <seriesname>.txt

set TMP = /tmp/keylists.$$

set SERIES=$1

set noglob

# make table entries
show_series $SERIES'$' -p | head --lines=3 > $TMP
set GOT = `wc -l < $TMP`
if ($GOT != 3) then
  echo $SERIES > $TMP
  ed -s $TMP  <<!
1 s/^/<TD align="right">/
1 s/\./<\/TD><TD>/
1 s/\$/<\/TD>/
wqmore
!
 cat >>$TMP <<!
<TD colspan="6" align="center"><I>dataseries is not defined</I></TD>
!
else
  ed -s $TMP  <<!
1,3 s/^  *//
1 s/^/<TD align="right">/
1 s/\./<\/TD><TD>/
2 s/Prime Keys are: /<TD>/
3 s/Note: /<TD><A HREF=http:\/\/jsoc.stanford.edu\/keylists\/$SERIES.txt>$SERIES.txt<\/A><\/TD><TD>XXXCXXX<\/TD><TD>XXXFXXX<\/TD><TD>XXXLXXX<\/TD><TD>/
1,3 s/\$/<\/TD>/
wq
!

# get record count
  set COUNT = `show_info -c $SERIES`
  if ($COUNT[1] > 0) then
# get first and last rec spec for first key
    set TMP2 = $TMP.fl
    show_info -s $SERIES >$TMP2
    set FIRST = `grep First $TMP2 | sed -e "s/First.*\[//" -e "s/].*//"` 
    set LAST = `grep Last $TMP2 | sed -e "s/Last.*\[//" -e "s/].*//"` 
    rm $TMP2
  else
    set FIRST = none
    set LAST = (none none)
  endif

  ed -s $TMP <<!
3 s/XXXCXXX/$COUNT[1]/
3 s/XXXFXXX/$FIRST/
3 s/XXXLXXX/$LAST[1]/
wq
!

# make file of keywords
show_info -l ds=$SERIES >$SERIES.txt
endif

cat $TMP
rm $TMP
