#!/bin/bash
date=`date +%Y-%d-%m-%H:%M`
#this is the cool composite of IR and topology
nam=goes_nam_1070x_100.jpg
#IR of East Canada
ecan=goes_ecan_1070_100.jpg
#IR of East USA
eusa=goes_eusa_1070_100.jpg

destDir=/home/mkobele/pics/background
wget http://www.weatheroffice.gc.ca/data/satellite/$nam -O $destDir/$nam-$date >/dev/null 2>&1
wget http://www.weatheroffice.gc.ca/data/satellite/$ecan -O $destDir/$ecan-$date >/dev/null 2>&1
wget http://www.weatheroffice.gc.ca/data/satellite/$eusa -O $destDir/$eusa-$date >/dev/null 2>&1

#cleanup old files
#find $destDir/ -mtime +2 -exec rm {} \;

function handleNew
{
    file=$1
    new=$destDir/$1-$date
    files=`ls -1r $destDir/$file*`    
    echo handle $new
    for i in $files 
    do
        if [ $i == $new ]
        then
            #echo "    don't delete the new file: $i"
            continue
        fi
        d=`diff $i $new`
        if  [ $? == 0 ] 
        then
            echo "    delete $i"
            rm $i
        else
            echo "    keep $i, it is different than $new"
        fi
    done
}

handleNew $nam 
echo --
handleNew $ecan 
echo --
handleNew $eusa


