#!/bin/bash

# UPDATE THE FOLLOWING VARIABLES TO RELECT LOCATION AS PER http://www.hostip.info/bulk/index.html
COUNTRY=AU
STATE=
CITY=Brisbane
# END MANUALLY UPDATED SECTION

case "$1" in
 -d) host "$2" | grep "has address" | awk '{print $4;}' | while read ip
     do
      echo "IP Address: $ip"
      "$0" "$ip"
      echo
     done
     ;;
 -u) IP=`wget -q http://whatismyip.org/ -O-`
     BLOCK=`echo "$IP" | sed "s/\([0-9]*.[0-9]*.[0-9]*\).[0-9]*/\1.0/"`
     DATA="$BLOCK,24,$COUNTRY,$STATE,$CITY"
     echo "Updating $BLOCK/24 to $CITY,$STATE,$COUNTRY"
     echo "$DATA" | curl -F nets=@- -F submit="Upload file" "http://www.hostip.info/bulk/upload.html" \
      && echo "Data successfully uploaded"
     ;;
 -h|--help|-*) cat << EOF
Usage:
$0                 geocode current machine
$0 ip-address      geocode given IP address
$0 -d domain-name  geocode given domain name
$0 -h|--help       display this help and exit
$0 -u              send location to server to update database
EOF
;;
 *) wget -q "http://api.hostip.info/rough.php?position=true&ip=$1" -O-
    echo
    ;;
esac
