#!/bin/csh -f

set datetime = `date +%Y%m%d_%H%M%S`
cat $1 | sed -e 's/,/ , /g' | sed -e 's/*/@/g' | sed -e 's/?/ /g' > HISTORYRC.$datetime

if( .$2 == . ) then
    set PLOT_SCRIPTS = `echo $GEOSUTIL/plots/landscape.script $GEOSUTIL/plots/portrait.script`
else
    set PLOT_SCRIPTS = `echo $GEOSUTIL/plots/$2`
endif

set exports = ''
set collections = `$GEOSUTIL/post/get_streams HISTORYRC.$datetime`

set found = false
foreach line ("`cat HISTORYRC.$datetime`")

   set word1 = `echo $line | awk '{print $1}'`
   set word2 = `echo $line | awk '{print $2}'`
   set word3 = `echo $line | awk '{print $3}'`
   set word4 = `echo $line | awk '{print $4}'`
   set word5 = `echo $line | awk '{print $5}'`

if( $found == false ) then
     foreach collection  ($collections)
        if ( $word1 == "$collection.fields:" ) then
             set coll  = $collection
             set found = true
          set word2 = `echo $line | awk '{print $2}'`
          set word3 = `echo $line | awk '{print $3}'`
          set word4 = `echo $line | awk '{print $4}'`
          set word5 = `echo $line | awk '{print $5}'`
          if( $word2 != '' ) then
              set len1 = `echo $word2 | awk '{print length}'`
              set len2 = `echo $word4 | awk '{print length}'`
              @   len1 = $len1 - 1
              @   len2 = $len2 - 1
              set word2 = `echo ${word2} | cut -b2-$len1`
              set word4 = `echo ${word4} | cut -b2-$len2`
              set export = "${word2}:${word4}"
            # echo  ${coll}: $export
              set new = true
              foreach item ($exports)
                if( $item == $export ) set new = false
              end
              if( $new == true ) set exports = `echo $exports $export`
          endif
        endif
     end
else
        if ( $word1 != "::" ) then
              if( $word1 != "#" ) then
              set len1 = `echo $word1 | awk '{print length}'`
              set len2 = `echo $word3 | awk '{print length}'`
              @   len1 = $len1 - 1
              @   len2 = $len2 - 1
              set word1 = `echo ${word1} | cut -b2-$len1`
              set word3 = `echo ${word3} | cut -b2-$len2`
              set export = "${word1}:${word3}"
            # echo  ${coll}: $export
              set new = true
              foreach item ($exports)
                if( $item == $export ) set new = false
              end
              if( $new == true ) set exports = `echo $exports $export`
              endif
        else
        # echo "  "
          set found = false
        endif
endif

end
set dum = ''
foreach export ($exports)
    set tst1 = `echo $export | grep -v   "SLP:DYN" \
                             | grep -v     "U:DYN" \
                             | grep -v     "V:DYN" \
                             | grep -v     "T:DYN" \
                             | grep -v   "ZLE:DYN" \
                             | grep -v "OMEGA:DYN" \
                             | grep -v   "Q:MOIST" \
                             | grep -v "RH2:MOIST" \
                             | grep -v  "O3:CHEMISTRY" `
    if( $#tst1 != 0 ) then
      set   tst2 = `grep $tst1 $PLOT_SCRIPTS`
      if( $#tst2 == 0 | .$2 == .Check_ALL ) set dum = `echo $dum $tst1`
    endif
end

/bin/rm -f HISTORYRC.$datetime
echo $dum
