#!/bin/sh
#
# Matt's Grabber Thingy
#

cmd="ftp"
dir="/var/www/users/mklein/dilbert/"
u="http://www.geek.nl/pics/dilbert-arch/"
s=52
e=71
done="2001,2002,2003,2004,2005,2006"
#done=""

lynx $u --source > dilbert.tmp
echo "Grabbed Dilbert... processing.."

cp dilbert.tmp dilbert2.tmp
for i in `echo "$done" | tr ',' "\n"`
do
	echo "Ignoring $i.."
	grep -v "\-$i" dilbert.tmp > dilbert2.tmp
	cp dilbert2.tmp dilbert.tmp
	k=`echo "$k\n$i"`
done

for i in `cat dilbert.tmp | cut -c$s-$e`
do
	if [ `echo $i | grep gif` ] 
	then

		j=`echo "$i" | cut -c9-12`
		l=`echo "$j/"`
		
		echo "Testing '$dir$l$i'"

		if [ -s $dir$l$i ]
		then
			echo "Skipped $dir$l$i -- existed..";
		else
			if [ -d $dir$j ]
			then
				echo "Grabbing $i";
				ftp -o $dir$l$i $u$i;
			else
				echo "$dir$j doesn't exist -- creating..."
				mkdir $dir$j
				echo "Grabbing $i";
				ftp -o $dir$l$i $u$i;	
			fi
		fi

		if [ `echo "$k" | grep $j` ]
		then
			echo '' >>/dev/null;
		else
			k=`echo "$k\n$j"`
		fi
	fi
done

rm dilbert2.tmp dilbert.tmp

k="$k\n"

for i in `echo "$k"`
do
	echo "Redoing index.html for $dir$i...."
	./index.sh $dir$i> $dir$i/index.html
done
