#!/bin/bash
#
#	WeeGee v0.01
#
#	The script will monitor the Photos directory and Internet access.
# 	If internet access is detected, it will check a particular site
#	to see if the iPhone is stolen. If so, it will do the following:
#	
#	*	Creates a gzip archive of recent calls, web sites and SMS to /weegee/hist.gz
#	*	Finds latest camera photos since date of theft and makes /weegee/photos.tar.gz
#	*	Emails this data back to you
#	*	(FUTURE) Will archive personal data, move it to private directory
#
#

#	Path of working directory
workPath="/weegee"

#	Path of resizeJPEG
resizePath="/usr/bin/resizeJPEG"

#	Resize JPEG to this maximum width:
JPEGMaxWidth=600

#	JPEG quality:
JPEGQuality=35

if [ ! -r $workPath ]
then
mkdir $workPath
fi

#	Email command for sending data
#	Example below: GMail (used IP address to prevent DNS problems)
mailcmd="mailsend -starttls -port 587 -smtp 64.233.167.109 -auth -user yourname@gmail.com -pass yourpass -f yourname@gmail.com -t sendto@otheraddress.com -sub WeeGee_iPhone_STOLEN_report -attach "


#	URL of "Theft Watch" file. (Use IP address to avoid DNS problems)
watchURL="http://123.123.123.123/watch_me.php"


watchResponse=`curl -4 $watchURL`
currentTime=`date "+%y-%m-%d %H:%M:%S"`

if [ $watchResponse -eq 1 ]
then
	#	STOLEN!
	echo "Stolen!"
	
	echo "Archiving recent call data..."
	sqlite3 /var/root/Library/CallHistory/call_history.db .dump > $workPath/callhist.sql
	
	echo "Archiving SMS history..."
	sqlite3 /var/root/Library/SMS/sms.db .dump > $workPath/sms.sql
	
	echo "Archiving web history..."
	cp /var/root/Library/Safari/History.plist $workPath/web.plist
	
	#	Archive the call/web/sms history
	tar -cf $workPath/wd_hist.tar $workPath/callhist.sql $workPath/web.plist $workPath/sms.sql 
	gzip -f $workPath/wd_hist.tar
	
	rm $workPath/callhist.sql 
	rm $workPath/web.plist
	rm $workPath/sms.sql
	
	# Now we need to upload the history to the email...
	$mailcmd $workPath/wd_hist.tar.gz
	rm -f $workPath/wd_hist.tar.gz
	
	#	Now, prepare the images. We first check to see if there are any new 
	#	JPEGs since the last time we ran this script... if so, then we resize
	#	them, archive them and send them via email.
	
	if [ -r $workPath/weegee.txt ]
	then
		find /var/root/Media/DCIM -name *.JPG -newer $workPath/weegee.txt -exec cp {} $workPath/ \;
		
		numPics=`ls -l $workPath/*.JPG  | wc -l`
		
		if [ $numPics != "0" ]
		then
			for origpic in $workPath/*.JPG
			do
				$resizePath $origpic $JPEGMaxWidth $JPEGQuality
			done
			tar cf $workPath/wd_img.tar $workPath/*.JPG
		fi
		rm -f $workPath/*.JPG
	fi
	
	if [ -r $workPath/wd_img.tar ]
	then
		$mailcmd $workPath/wd_img.tar
		rm -f $workPath/wd_img.tar
	fi

else
	#	Not stolen
	echo "Not stolen."

fi

echo "Modifying $workPath/weegee.txt to current timestamp..."
if [ -r $workPath/weegee.txt ]
then
	rm -f $workPath/weegee.txt
fi
echo $currentTime > $workPath/weegee.txt



