#!/bin/bash
# The away message
away_msg="Je ne suis plus à mon bureau"

# The available message
available_msg="Je suis devant l'ordinateur"

#The welcome message
welcome_msg="Bienvenue $USER"

# Send updates to chat programs and external websites?
# Highly recommended to use a .netrc file for external websites
exchange=0
exch_link="http://barracuda/exchange/NButterworth/?Cmd=options"
twitter=0
pidgin=`pgrep pidgin` # stays null if it's not running

# Tests for running audio applications. Add yours!
if [ `pgrep vlc` ]
then
	audio_player="vlc"
elif [ `pgrep banshee` ]
then
	audio_player="banshee"
elif [ `pgrep exaile` ]
then
	audio_player="exaile"
elif [ `pgrep rhythmbox` ]
then
	audio_player="rhythmbox"
fi

# Misc. variables
DOC_REQUEST=70

: <<DOCUMENTATIONXX
This script is used by BlueProximity (http://blueproximity.sourceforge.net/),
and allows you to automatically set your current status as away or available.
It will:
* Toggle pause state of audio playback
* Set your current status in Pidgin
* Send updates to Twitter
* Update your Microsoft Exchange Out-of-Office status via Outlook Web Access
* Lock your desktop when away, and unlock it upon your return
* Any other status-dependent changes to your desktop or applications

It will also poke the screen saver when you're close, to keep it from
activating and interrupting your work.

Usage: btmon-status <status>
where <status> is one of:
* help -- This help text
* away -- Set your status to away
* available -- Set your status to available
* update -- Set your status-to-be, when you will be away for a while
* poke -- Keep your screen saver from activating

--Nelson Butterworth, Apr 3 2008
nelson.butterworth@gmail.com
-- Modified by Boula-Bytes, Jan 6 2010
DOCUMENTATIONXX

# Function declarations
pause_audio () # This function pauses whatever audio player is playing. Add yours!
{
	player=$1
	case $player in
		"vlc")
			if [ "x`echo status | nc localhost 9000 -q 1 | grep "play state: 1"`" != "x" ]
			then
				amixer set PCM 0 # vlc doesn't like to stream playback, at least not via the telnet pipe
			fi;;
		"exaile")
			exaile --play-pause;;
		"rhythmbox")
			rhythmbox-client --pause --no-start;;
		*)
			$player --pause;;
	esac
}

resume_audio () # This function resumes whatever audio player is playing. Add yours!
{
	player=$1
	case $player in
		"vlc")
			if [ "x`echo status | nc localhost 9000 -q 1 | grep "play state: 1"`" != "x" ]
			then
				amixer set PCM 70% # vlc doesn't like to stream playback, at least not via the telnet pipe
			fi;;
		"rhythmbox")
			rhythmbox-client --play --no-start;;
		*)
			$player --play;;
	esac
}

update_status_msg ()
{
	# Display dialog to enter status
	location=`zenity --entry --title="Where am I?" --text="Current location: $1" --entry-text="I'm..."`
		
	# If text entry isn't null
	if [ -n "$location" ]
	then
		# Update away-status
		sed -e "3 s/away_msg=.*/away_msg=\"I\'m $location\.\"/" $0 > tmp
		mv --update tmp $0
		chmod a+x $0
	fi
}

# Connect to the web services of your choice, and set their statuses
# Add your own functions for other web services
update_twitter ()
{
	status="$1"
	curl --netrc --data status="$status" http://www.twitter.com/statuses/update.xml
}

update_pidgin ()
{
	status="$1"
	message="$2"
	purple-remote setstatus?status="$status"
	purple-remote setstatus?message="$message"
}

update_exchange ()
{
	case $1 in
		"away")
			status="1";;
		"available")
			status="0";;
	esac
	link="$2"
	message="$3"
	curl --netrc --ntlm --data OofState="$status" --data OofReply="$message" $link
}

case $1 in
	"away")	# I've moved away
		if [ -n "$audio_player" ]
		then
			pause_audio "$audio_player"
		fi
		if [ $pidgin ]
		then
			update_pidgin "away" "$away_msg"
		fi
		if [ $twitter ]
		then
			update_twitter "$away_msg"
		fi
		if [ $exchange ]
		then	
			update_exchange "away" "$exch_link" "$away_msg"
		fi
		# Lock desktop
		gnome-screensaver-command -l
		espeak -g 15 "Computer Locked Down."
		;;
	"available") # I'm back
		# Unlock desktop and wake monitor(s)
		gnome-screensaver-command --deactivate
		gnome-screensaver-command --poke
		espeak -g 15 "Computer Unlocked."
		espeak -v fr -g 15 "$welcome_msg"
		if [ $exchange ]
		then	
			update_exchange "available" "$exch_link" "$available_msg"
		fi
		if [ $pidgin ]
		then
			update_pidgin "available" "$available_msg"
		fi
		if [ $twitter ]
		then
			update_twitter "$available_msg"
		fi
		if [ "x$audio_player" != "x" ]
		then
			resume_audio "$audio_player"
		fi
		;;
	"update") # Update my away status
		update_status_msg "$away_msg"
		;;
	"poke") # Keep things awake when close-by
		gnome-screensaver-command --poke
		;;
	*) # How to use
		echo
		sed --silent -e '/DOCUMENTATIONXX$/,/^DOCUMENTATIONXX$/p' "$0" |
		sed -e '/DOCUMENTATIONXX$/D'
		exit $DOC_REQUEST;;
esac
