#!/bin/bash
#latitude2brightkite.sh
# Checks in your Google Latitude position to Brightkite
#
# Author :
# Jean-Marc Liotier 
#
# Changelog :
# 20090604 - 0.1 - Initial release
# 20090605 - 0.2 - Only check-in with Brightkite if position has changed.
# 20090607 - 0.3 - The working directory is now a parameter
# 20090612 - 0.4 - Only post updates if the _name_ of the location changes,
#                  not if only the _internal BK id_ of the place does.
#                  Contribution by Yves Le Jan 
# 20090615 - 0.5 - Perl 5.8.8 compatibility of the JSON coordinate parsing.
#                  Contribution by Jay Rishel 
# 20100830 - 0.6 - Hackish ways to keep up with a modification of
#                  the Brightkite API : tail and md5sum...
#
# Requirements beyond Bash :
# - The Perl JSON library (http://search.cpan.org/dist/JSON/)
# (available in Debian as libjson-perl)
# - Curl
#
# Known bugs and limitations :
# - Requires your Google Public Location Badge to be enabled and to show the
# best available location. For this script to work, your location has to be
# public.
#
# This script is an ugly mongrel hack. I'm an aged script kiddie.
#
# It proves that it can be done, it works for me, and the official Google
# Latitude API will hopefully soon make it obsolete !
#
# The right way to do it would be to parse Latitude's JSON output cleanly using
# the Perl library. But that dirty prototype took me all of ten minutes to set
# up, and it works...
# 
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.

# Parameters
#
# Enter your Brightkite credentials
YourBrightkiteLogin=pleasefillin
YourBrightkitePassword=pleasefillin
#
# Replace this Google Public Location Badge URL with your own
# (choose the JSON one !)
badge="http://www.google.com/latitude/apps/badge/api?user=7832225593622256926&type=json"
#
# Working directory for temporary files
workdir=/home/jim/applications/latitude2brightkite
#
# End of parameters

# We'll use an "oldplace" file to remember where we were during
# the previous invocation
oldplace_file=$workdir/oldplace
if [ ! -f "$oldplace_file" ]
 then
  touch $oldplace_file
 fi
oldplace=`cat $oldplace_file`

coordinates=`wget -qO- "$badge" | perl -0007 -MJSON -ne 'print to_json(from_json($_, {allow_nonref=>1}), {pretty=>1})' | grep -A 2 coordinates | grep -v coordinates | sed s/,//`
longitude=`echo $coordinates | awk -F" " '{print($1)}'`
latitude=`echo $coordinates | awk -F" " '{print($2)}'`
id=`wget -qO- "http://brightkite.com/places/search.xml?q=$latitude%2C$longitude" | grep "" | sed s/\ \ \// | sed s/\<\\\/id\>// | tail -n 1`
place=`wget -qO- "http://brightkite.com/places/search.xml?q=$latitude%2C$longitude" | grep "" | sed s/\ \ \// | sed s/\<\\\/name\>// | md5sum | awk '{print $1}'`

echo $oldplace
echo $place

# Only check-in in with Brightkite if the Google Latitude position has changed
if [ "$place" != "$oldplace" ];
 then
  curl -X POST -u $YourBrightkiteLogin:$YourBrightkitePassword http://brightkite.com/places/$id/checkins.xml
  rm -f $oldplace_file
  echo $place >> $oldplace_file
 fi