#!/bin/sh
#
# This script can be used as a callto: (or other) protocol handler in
# Mozilla Firefox-based browser.
# In Firefox use Preferences > Applications and set the callto handler
# to this script.


# Test if a SFLphone client is already open, if not open a new one
# Opening a new client will start sflphoned if not already running
SFLPHONEC=`ps -A | grep sflphone-client`
if [ "$SFLPHONEC" = "" ]; then
    /usr/bin/sflphone-client-gnome&
fi

# Check 1st argument (phone number)
if [ -z $1 ]; then
    echo "Error: argument 1 (phone number) not provided."
    exit 1
fi

# Cleanup destination, keeping numbers only
TO="`echo $1 | sed -e 's/[^0123456789]//g'`"

# Generate call id.
CALLID=${RANDOM}$$

# Dbus placeCallFirstAccount method does not reach sflphoned if starting
# Should find another way to do this
sleep 1

dbus-send                                                       \
    --type="method_call"                                        \
    --dest="org.sflphone.SFLphone"                              \
    "/org/sflphone/SFLphone/CallManager"                        \
    "org.sflphone.SFLphone.CallManager.placeCallFirstAccount"   \
    string:"$CALLID"                                            \
    string:"$TO"

exit 0

# EOF
