#!/bin/bash

# sugar-telepathies-0.1
# Copyright (c) 2007, 2008 OLPC
# Author: Giannis Galanis <echo gxlxnis@lxptop.org | sed s/x/a/g>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

if [ -e /tmp/olpc-session-bus -a ! "$DBUS_SESSION_BUS_ADDRESS" ]; then
    export DBUS_SESSION_BUS_ADDRESS="unix:path=/tmp/olpc-session-bus";
fi

telepathies=`dbus-send --session --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.ListNames 2>&1|grep 'Telepathy\.Connection\.'|sed 's/^.*"\(.*\)".*$/\1/'`

echo $telepathies | grep "^Fail" > /dev/null && exit

[ -n "$telepathies" ] && for dest in $telepathies
do
	obj=`echo $dest|sed 's/\./\//g'`
	echo -n $dest|sed 's/^.*Connection\.//'|sed 's/\..*$//'
	echo -n -e "\t"
	dbus-send --session --print-reply --dest=$dest /$obj org.freedesktop.Telepathy.Connection.GetStatus 2>&1|
	awk 'END{print $NF}'|sed 's/^.*[a-zA-Z].*$/2/'
	#any error translates as "2", ie "Disconnected"
done
