#!/bin/sh
#120131 rodin.s: internationalized.
#130505 rerwin: ensure network_tray icon shows wireless modem
#130812 rerwin: Modification as version 1.5 for Puppy integration
#151222 rerwin: Add device name to 'not detected' message
#160116 rerwin: restore display of current connection; disconnect network if resolveconf absent.

. /root/.config/gprs.conf #130812...

#set -x; test -f /tmp/xerrs.log && exec 1>&2 || exec &>/tmp/debug.log #send feedback and trace to xerrs.log or debug.log

export TEXTDOMAIN=pgprs
export OUTPUT_CHARSET=UTF-8
. gettext.sh

# connect
function connect_func () {
	export TEXTDOMAIN=pgprs
	export OUTPUT_CHARSET=UTF-8
	. gettext.sh
	ps aux | grep -q 'wvdial isp[12]' \
	 || rm -f /tmp/.network_tray-use_analog_dialup_icons 2>/dev/null #130505
	echo -e "$(eval_gettext "Attempting connection through \$GPRSDEV to APN \$GPRSAPN...\n")" #130812
	if [ ! -x /usr/sbin/resolvconf ];then #160116...
	 if [ -x /usr/sbin/networkdisconnect ];then #but not ppp interface.
	  /usr/sbin/networkdisconnect
	 else 
	  [ -n "$(busybox pidof wpa_supplicant)" ] \
	   && wpa_cli terminate #kills any running wpa_supplicant
	  if [ -x /etc/rc.d/rc.network ];then 
	   /etc/rc.d/rc.network stop
	  else
	   dhcpcd -k
	  fi
	 fi
	fi
	[ -e /dev/ppp ] || mknod /dev/ppp c 108 0
	# Start PPP daemon...
	# For sanity, keep a lock on the serial line (lock).
	logger "pgprs: $(eval_gettext "Starting PPP daemon for \$GPRSDEV")"
	/usr/sbin/pppd $GPRSDEV \
	 call gprs-generated \
	 connect "chat -v -T $GPRSNBR -f /etc/ppp/chatscripts/gprs-connect-chat" \
	 disconnect "chat -v -s -S -f /etc/ppp/chatscripts/gprs-disconnect-chat" \
	 linkname gprs \
	 nodetach \
	 lock
	local STATUS=$?
	echo "$(eval_gettext "Exit status is \$STATUS")" #130812
	echo "$(gettext 'DISCONNECTED')"
	logger "pgprs: $(eval_gettext "pppd exit status for \$GPRSDEV is \$STATUS")"
	exit $STATUS
}
export -f connect_func

#160116 connect dialog...
function connection_dialog_func () {
	local DISCSTOP="$(gettext 'DISCONNECT')"
	Xdialog --center --wmclass pgprs --title "Pgprs $(gettext 'GPRS Connection Log')" --backtitle "$(eval_gettext "NOTICE: If log shows a failure to connect, click '\$DISCSTOP', to exit.")" --ok-label "$DISCSTOP or stop trying" --cancel-label "$(gettext 'CLOSE window but stay online')" --fixed-font --tailbox /tmp/.gprs_connect.log 25 82
	local STATUS=$?
	if [ $STATUS -eq 0 -a -s /var/run/ppp-gprs.pid ];then
		PPPDPID=$(cat /var/run/ppp-gprs.pid)
		if [ "$PPPDPID" ];then
			kill $PPPDPID
			rm -f /var/run/ppp-gprs.pid
			gtkdialog-splash -timeout 15 -placement center -bg orange -text "$(eval_gettext "Mobile device \$GPRSDEV disconnected")"
		fi
	fi
	return $STATUS
}

#160116...
if [ -f /var/run/ppp-gprs.pid ];then
	connection_dialog_func
	exit $? #0 = disonnect/stop, 1 = close but stay connected, 255 = abort
fi #160116 end

echo "#Generated by pgprs-connect" > /etc/ppp/peers/gprs-generated
echo "call gprs-editable" >> /etc/ppp/peers/gprs-generated
if [ "$GPRSUSER" ];then #130812
	echo -e "auth\nuser $GPRSUSER" >> /etc/ppp/peers/gprs-generated
	[ "$GPRSPAPONLY" = "true" ] \
	 && echo "require-pap" >> /etc/ppp/peers/gprs-generated
else
	echo "noauth" >> /etc/ppp/peers/gprs-generated
fi

[ "${GPRSPIN}" != "" -a "${GPRSPIN}" != "gprspin" ] \
 && SAVEVAL="AT+CPIN=\"$GPRSPIN\"" \
 || SAVEVAL="AT"
[ "$SAVEVAL" != "$(cat /etc/ppp/chatscripts/gprs-cpin_command 2>/dev/null | grep -v ^\')" ] \
 && echo "$SAVEVAL" > /etc/ppp/chatscripts/gprs-cpin_command #130812 end
SAVEVAL="AT+CGDCONT=1,\"IP\",\"$GPRSAPN\""
[ "$SAVEVAL" != "$(cat /etc/ppp/chatscripts/gprs-cgdcont_command 2>/dev/null | grep -v ^\')" ] \
 && echo "$SAVEVAL" > /etc/ppp/chatscripts/gprs-cgdcont_command
SAVEVAL="ATD$GPRSNBR"
[ "$SAVEVAL" != "$(cat /etc/ppp/chatscripts/gprs-dial_command 2>/dev/null | grep -v ^\')" ] \
 && echo "$SAVEVAL" > /etc/ppp/chatscripts/gprs-dial_command

if [ -c $GPRSDEV ] \
 && echo ATZ | microcom -t 200 $GPRSDEV 2>/dev/null | grep -q 'ATZ'; then #130812 151224 160104
	echo -n > /tmp/.gprs_connect.log
	connect_func 2>&1 > /tmp/.gprs_connect.log &
	if [ $? -eq 0 ];then
		connection_dialog_func #160116
		exit $? #0 = disonnect/stop, 1 = close but stay connected, 255 = abort
	else
		Xdialog --center --wmclass pgprs --title "Pgprs $(gettext 'GPRS Connection Log')" --backtitle "\n$(gettext 'The attempt to connect was unsuccessful:')\n" --ok-label "$(gettext 'Configure')" --cancel-label $(gettext 'Close') --fixed-font --tailbox /tmp/.gprs_connect.log 25 82
	fi
else
	Xdialog --center --wmclass pgprs --title "Pgprs $(gettext 'GPRS Connect')" --icon $ICONLIB/error.xpm --ok-label "$(gettext 'Configure')" --cancel-label $(gettext 'Close') --yesno "\n$(gettext 'Cannot connect.')\n\n$(eval_gettext "Modem device '\$GPRSDEV' is not accessable")\n$(gettext 'It might not be plugged in.')\n" 10 60
fi
STATUS=$?
exit $STATUS #0 = Configure, 1 = Cancel, 255 = abort
