#!/bin/csh

#Base Directory
setenv DILROOT /usr/local/gsd_script/dilbert
#location of downloaded index.html
setenv DIL_HTML $DILROOT/dilbert.html
#location of unprocessed url
setenv DIL_URL $DILROOT/url.txt
#location of graphic filename text file
setenv DIL_FILE $DILROOT/filename.txt
setenv DIL_DATE $DILROOT/date.txt
setenv DIL_LOG  $DILROOT/dilbert.log
setenv GIFPATH  /usr/home/gsd/dilbertgifs/

#if date.txt doesnt match todays date
if (`cat $DIL_DATE` != `date +%d`) then
        #get index.html file from dilbert.com 
        fetch -a -T 50 -o $DIL_HTML http://www.unitedmedia.com/comics/dilbert/index.html
        #$status_test = $status + $status_test
        echo 'http://www.unitedmedia.com' > $DIL_URL
        #set up temp storage for dilbert graphic url sans hostname
        setenv DIL_FILE `cat $DIL_HTML | grep 'Dilbert comic' | awk '{print $4}' | awk -F '"' '{print $2}'` 
        echo $DIL_FILE >>  $DIL_URL
        setenv DIL_FILENAME `echo $DIL_FILE | awk -F '/' '{print $6}'` 
        if (-e $GIFPATH$DIL_FILENAME) then
                echo `date` fail >> $DIL_LOG
        else
                fetch -n -o /usr/home/gsd/dilbertgifs/$DIL_FILENAME `cat $DIL_URL | awk '{line=line $0} END {print line}'` 
                if (-e $GIFPATH$DIL_FILENAME) then
                        date +%d > $DIL_DATE
                        echo `date` success  >> $DIL_LOG
                else
                        echo  `date` got index failed gif >> $DIL_LOG
                endif
        endif        
endif

