#!/bin/sh

if [ -r $HOME/.source ]; then
        . $HOME/.source
else
        echo "Cannot find $HOME/.source"
fi

# This script takes 1 argument. 
# everything that is not a number gets stripped away
PHONE=`echo $@ | tr -d '[:alpha:]|[:punct:]|[:cntrl:]|[:blank:]'`

# we count the number of digits
PHONE_DIGIT_COUNT=`echo $PHONE | wc -c | awk '{print $1}'`

# You'll need your own one of these. It's free and instant from www.whitepages.com
# Just copy and paste it here on the next line in place of the 00000
API_KEY=00000

# FYI / NOTE: "echo 0123456789|wc -c" will give you a count of "11" even though
# any human would say there are 10 characters there
# 
if [ "$PHONE_DIGIT_COUNT" != "11" ]
then

	# If we get rid of anything that is not a number and it tells us we do not have
	# 10 (ok, 11, see above) then quit. (Sorry, non USA folks, but this script
	# probably wouldn't work for you anyway)
	
	echo "$0: can't handle phone numbers that aren't 10 digits (Found: $PHONE has $PHONE_DIGITS)"
	exit 1

else
	# If the phone number DOES have the right number of digits
	
		#####################
		# BEGIN CACHE SECTION
		if [ ! -d $HOME/.$NAME ]
		then
			# if dir isn't there, make it
			mkdir -p $HOME/.$NAME
		fi

		PHONE_FILE="$HOME/.$NAME/$PHONE"

		
		if [ -f "$PHONE_FILE.txt" ]
		then
		
			# OK, the idea here is that there are some numbers which are unlisted
			# such as cell phones, 
			# and some numbers which may have inaccurate/outdated/incomplete data
			# 
			# SO we want to be able to handle that scenario
			# we put the results into a TXT file the first time we get them
			# and then just use that file for results later on
			#
			# this not only saves on API hits and is fast fast fast
			# but means that we can edit the file to show whatever
			# information WE decide is proper
			# 
			# only possible problem is when a number changes hands, 
			# but how often does that really happen?
		
			cat "$PHONE_FILE.txt"
			exit 0
		fi	
		
		# END CACHE SECTION
		#####################		
		

	REVERSE_LOOKUP_URL="http://api.whitepages.com/reverse_phone/1.0/?phone=$PHONE;api_key=$API_KEY"

	lynx -source -dump -nomargins -nonumbers -width=99999999999999999 "$REVERSE_LOOKUP_URL" > $PHONE_FILE.xml
	
	PRETTY_PHONE=`fgrep '<wp:fullphone>' $PHONE_FILE.xml | sed 's#<wp:fullphone>##g; s#</wp:fullphone>##g; s#^ *##g;'`
	
	# We don't know if it is a business number or a person, so we check for a business name first
	# and if there isn't one, then we check for a first/last name
	FULL_NAME=""
	
	FULL_NAME=`fgrep '<wp:businessname>' $PHONE_FILE.xml | sed 's#<wp:businessname>##g; s#</wp:businessname>##g; s#^ *##g;'`
	
	if [ "$FULL_NAME" = "" ]
	then
		# OK, we didn't find a business name, so try a first/last name
		FULL_NAME=`egrep '<wp:firstname>|<wp:lastname>' $PHONE_FILE.xml |\
		sed 's#<wp:lastname>##g; s#</wp:lastname>##g; s#<wp:firstname>##g; s#</wp:firstname>##g;  s#^ *##g;' | tr -s '\012' ' '`
	fi

	# Street address	
	ADDRESS1=`egrep '<wp:fullstreet>' $PHONE_FILE.xml |\
		sed 's#<wp:fullstreet>##g; s#</wp:fullstreet>##g; s#^ *##g;'`
	
	# City/state/zip
	ADDRESS2=`egrep '<wp:city>|<wp:state>|<wp:zip>' $PHONE_FILE.xml |\
		sed 's#<wp:city>##g; s#</wp:city>##g;  s#<wp:state>##g; s#</wp:state>##g;  s#<wp:zip>##g; s#</wp:zip>##g;  s#^ *##g;' |\
		tr -s '\012' ' '`
	
	
	# initialize the file by telling us what the phone number is


	# now we take the information that we get back
	# *and* pipe it through grep to eliminate any blank lines
	# and store it in the cache
	#
	#
	# NOTE: if you want to disable caching, delete the 
	# 		| tee "$PHONE_FILE.txt"
	# below
echo "
$FULL_NAME
$ADDRESS1
$ADDRESS2
" | grep -i '[a-z]' | tee "$PHONE_FILE.txt"

	
fi


exit 0

#EOF

