#!/bin/csh

cd /home/coolwx/public_html/extreme/archive/ids

set year = `date -u +%Y --date="3 hours ago"`
set sdtg = `date -u +%Y%m --date="3 hours ago"`
set dtg = `date -u +%Y%m%d --date="3 hours ago"`
set hr = `date -u +%H --date="3 hours ago"`


egrep "^[A-Z][A-Z,0-9][A-Z,0-9][A-Z,0-9] [0-9][0-9][0-9][0-9][0-9][0-9]Z" ../combo/${sdtg}.php | cut -c1-4 | sort | uniq -c | sort -n > ${sdtg}.ids
echo "done"
set cnt = `ls -1 ../${sdtg}/ | grep -c ${dtg}`
if ($cnt > 0) then
   egrep "^[A-Z][A-Z,0-9][A-Z,0-9][A-Z,0-9] [0-9][0-9][0-9][0-9][0-9][0-9]Z" ../${sdtg}/${dtg}* | cut -d":" -f2 | cut -c1-4 | sort | uniq -c | sort -n > today.ids
else 
   echo "" > today.ids
endif
echo "done"

set year2 = `date -u +%Y --date="27 hours ago"`
set sdtg2 = `date -u +%Y%m --date="27 hours ago"`
set dtg2 = `date -u +%Y%m%d --date="27 hours ago"`
set hr2 = `date -u +%H --date="27 hours ago"`

set cnt = `ls -1 ../${sdtg2}/ | grep -c ${dtg2}`
if ($cnt > 0) then
   egrep "^[A-Z][A-Z,0-9][A-Z,0-9][A-Z,0-9] [0-9][0-9][0-9][0-9][0-9][0-9]Z" ../${sdtg2}/${dtg2}* | cut -d":" -f2 | cut -c1-4 | sort | uniq -c | sort -n > yest.ids
else
    echo "" > yest.ids
endif
echo "done"


if ($hr == "13") then
   rm -f all.rank
   set ffs = `ls 2*.ids`
   set ids = `egrep -h "[0-9][0-9][0-9] " $ffs | cut -c9- | sort | uniq`
   foreach id ($ids)
      set vals = `egrep -h $id $ffs | cut -c1-7`
      set total = 0
      foreach val ($vals)
         set total = `expr $total {+} $val`
      end
      echo $id $total >> all.rank
   end
   sort -r -n -k 2 all.rank | head --lines=25 > all.sort
endif


rm -f year.rank
set ffs = `ls ${year}*.ids`
set ids = `egrep -h "[0-9][0-9][0-9] " $ffs | cut -c9- | sort | uniq`
foreach id ($ids)
   echo $id
   set vals = `egrep -h $id $ffs | cut -c1-7`
   set total = 0
   foreach val ($vals)
      set total = `expr $total {+} $val`
   end
   echo $id $total >> year.rank
end

sort -r -n -k 2 year.rank | head --lines=25 > year.sort

rm -f month.rank
set ffs = `ls ${sdtg}.ids`
set ids = `egrep -h "[0-9][0-9][0-9] " $ffs | cut -c9- | sort | uniq`
foreach id ($ids)
   set vals = `egrep -h $id $ffs | cut -c1-7`
   set total = 0
   foreach val ($vals)
      set total = `expr $total {+} $val`
   end
   echo $id $total >> month.rank
end

sort -r -n -k 2 month.rank | head --lines=25 > month.sort

rm -f yest.rank
set ffs = `ls yest.ids`
set ids = `egrep -h "[0-9] " $ffs | cut -c9- | sort | uniq`
foreach id ($ids)
   set vals = `egrep -h $id $ffs | cut -c1-7`
   set total = 0
   foreach val ($vals)
      set total = `expr $total {+} $val`
   end
   echo $id $total >> yest.rank
end

sort -r -n -k 2 yest.rank | head --lines=25 > yest.sort

rm -f today.rank
set ffs = "today.ids"
set ids = `egrep -h "[0-9] " $ffs | cut -c9- | sort | uniq`
foreach id ($ids)
   set vals = `egrep -h $id $ffs | cut -c1-7`
   set total = 0
   foreach val ($vals)
      set total = `expr $total {+} $val`
   end
   echo $id $total >> today.rank
end

sort -r -n -k 2 today.rank | head --lines=25 > today.sort

set subsets = "all year month yest today"
foreach subset ($subsets)
   echo "<OL>" > ${subset}.php
   set ids = `cut -c1-4 ${subset}.sort`
   foreach id ($ids)
      set chk = `egrep -c "^${id};" ../../stations.world`
      if ($chk > 0) then
          set data = `egrep "^${id};" ../../stations.world`
          set city = `echo $data | cut -d";" -f2`
          set country = `echo $data | cut -d";" -f3`
      else
          set city = "UNKNOWN"
          set country = "UNKNOWN"
      endif
      echo "<LI><font color=#ffff00>${id}:${city},${country}</font>" >> ${subset}.php
   end
   echo "</OL>" >> ${subset}.php
end
