#! /bin/sh

# Start the flare patrol from the flare page directory
# with crontab running this script each day at 0 h UT.
# Patrol will finish at about 1 minute before midnight.

# The script make_flare_jpeg also should be running every 10 minutes.

cd /home/www/naa/today/

# If there is an old flare.dat still here, save it in the archive.

if test -e flare.dat 
then
  mv flare.dat flare.old
fi

# Start the patrol for the new day.
# This program will run for 1439 minutes.  

/home/www/naa/bin/patrol 

# Use local dates so that sunrise and sunset make sense.  
# Assign the current UTC YYYYmmdd to variables for use as file names.

base=`date -u +%Y%m%d`
datfile=../archive/data/${base}.dat
oldfile=../archive/data/${base}_old.dat
jpgfile=../archive/jpeg/${base}.jpg

# Generate archive jpg from the dat file.

cat frame.mgr flare.dat > tmp.mgr
/usr/local/grace/bin/gracebat -hardcopy -hdevice JPEG -printfile tmp.jpg tmp.mgr >/dev/null
cp tmp.jpg $jpgfile 
rm tmp.mgr
rm tmp.jpg

# Move flare.dat and flare.old to archive.

mv flare.dat $datfile
if test -e flare.old  
then
  mv flare.old $oldfile
fi

# This cycle is done.  The directory should be clean and ready for a new day.
