#!/bin/csh

cd /home/coolwx/public_html/record/archive

set dtg = `date -u +%Y%m --date="25 minutes ago"`

set dds = `ls -d ${dtg}`
foreach dd ($dds)
   echo $dd
    cd $dd
    set ffs = `ls *.png`
     foreach ff ($ffs)
        if ( ! (-e ${ff}.gif)) then
              convert $ff ${ff}.gif
        endif
     end
    cd ..
end

set hr = `date -u +%H --date="25 minutes ago"`

set areas = "usa globe europe"
foreach area ($areas)
   set ffs = `ls 2*/*${area}*gif | tail --lines=24 | egrep -v "0229[0-2][0-9]"`
   set lastf = `ls 2*/*${area}*gif | egrep -v "0229[0-9][0-9]" | tail --lines=1`
   set laststring = "${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf} ${lastf}"
   gifsicle --colors 256 -O2 --delay=15 --loop $ffs $laststring -o  ${area}.movieday.gif

   set ffs = `ls 2*/*${area}*gif | tail --lines=96 | egrep -v "0229[0-2][0-9]"`
   gifsicle --colors 256 -O2 --delay=8 --loop $ffs $laststring -o ${area}.movie4day.gif

   set ffs = `ls 2*/*${area}*gif | tail --lines=168 | egrep -v "0229[0-2][0-9]"`
   gifsicle --colors 256 -O2 --delay=10 --loop $ffs $laststring -o ${area}.movieweek.gif

   set ffs = `ls 2*/*${area}*gif | tail --lines=744 | egrep -v "0229[0-2][0-9]"`
   gifsicle --colors 256 -O2 --delay=10 --loop $ffs $laststring -o ${area}.moviemonth.gif

   if ($hr == "10") then
       set ffs = `ls 2*/*${area}*gif | tail --lines=2190 | egrep -v "0229[0-2][0-9]"`
       gifsicle --colors 256 -O2 --delay=5 --loop $ffs $laststring -o ${area}.movie3month.gif

       set ffs = `ls 2*/*${area}*gif | tail --lines=8760 | egrep -v "0229[0-2][0-9]"`
       gifsicle --colors 256 -O2 --delay=5 --loop $ffs $laststring -o ${area}.movieyear.gif
   endif
end

mv *movie*gif ../movies
