#!/bin/sh
# (C) 2008 Bearstech, LGPLv2, Marcus Bauer
# "persistence" changes by Michał 'rysiek' Woźniak <rysiek@brama.elka.pw.edu.pl>
#
# The script starts the pppd.
# The gsm modem must be registered to the network before 
# running it.

#
# look for providers in /etc/ppp/peers
#
PROVIDER="france-orange"

#
# if gsmd0710muxd is running this must be "yes"
#
MUX=yes
PERSIST=yes

# utility function - getting the muxed modem dev from the gsm muxer
get_mux_dev() {
	dbus-send  --system --print-reply --type=method_call --dest=org.pyneo.muxer /org/pyneo/Muxer org.freesmartphone.GSM.MUX.AllocChannel "string:GSM" | awk -F '"' '{ print $2 }'
}

# let the user know what's happening
echo "The GSM must be registered to the network before starting pppd"
echo "If it is not up in 20 seconds, kill it with Ctrl-C and start again"

# muxed?
if [ $MUX = no ]; then

	# nope, just kill the gsmd and set the default modem dev
	killall -9 gsmd
	PPPDEV="/dev/ttySAC0"

else

	# aye, muxed - get the muxed modem dev
	PPPDEV=`get_mux_dev`

fi

# some debug info
echo "GSMMUX: $MUX; PERSISTENT: $PERSIST; ppp device: $PPPDEV"


#
# start pppd, by default it is France Orange
#  see /etc/ppp/peers/france-orange as example
#

# are we supposed to persist?
if [ $PERSIST = yes ]; then

	# yes - looping, then
	while true; do

		# calling pppd and - after the connection dies - waiting some time for the modem to reset
		pppd $PPPDEV call $PROVIDER
		sleep 5

		# do we need to get a new muxed modem dev every time?
		if [ $MUX = yes ]; then
			# aye, we do!
			PPPDEV=`get_mux_dev`
			echo "GSMMUX: $MUX; PERSISTENT: $PERSIST; ppp device: $PPPDEV"
		fi

	done

else

	# nah, just connect once
	pppd $PPPDEV call $PROVIDER

fi
