#!/bin/bash
# shellcheck disable=SC1091 # Skip sourced checks.
#200829 (v2.0) Correct display of connect log to work around Xdialog regression; correct not-connected logic; make pppd detach after connection; specify busybox microcom because full version interprets -t as "work in telnet mode" instead of timeout; add version to window title; replace pidof with pgrep; resolve shellcheck warnings; reactivate after networkdisconnect.

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

CONNECTLOG=/tmp/.gprs_connect.log #200829

function connect_func {
	export TEXTDOMAIN=frisbee
	export OUTPUT_CHARSET=UTF-8
	. gettext.sh
	pgrep -f 'wvdial isp[12]' >/dev/null \
	  || 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" >> $CONNECTLOG #130812 200829
	if [ ! -x /usr/sbin/resolvconf ]; then #160116...
	    if [ -x /usr/sbin/networkdisconnect ]; then #but not ppp interface.
	        /usr/sbin/networkdisconnect
	    else 
	        ps -C wpa_supplicant >/dev/null \
	          && wpa_cli terminate #kills any running wpa_supplicant #200829
	        dhcpcd -k
	    fi
	    frisbee --activate #200829
	fi
	[ -e /dev/ppp ] || mknod /dev/ppp c 108 0
	# Start PPP daemon...
	# For sanity, keep a lock on the serial line (lock).
	logger "frisbee: $(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 \
	  updetach \
	  lock \
	  >> $CONNECTLOG 2>&1 #200829
	local STATUS=$?
# shellcheck disable=SC2005 # echo needed for newline.
	echo "$(eval_gettext "Exit status is \$STATUS")" >> $CONNECTLOG #130812 200829
	logger "frisbee: $(eval_gettext "pppd exit status for \$GPRSDEV is \$STATUS")"
	if [ -s /var/run/ppp-gprs.pid ]; then #200829...
# shellcheck disable=SC2005 # echo needed for newline.
	    echo "$(gettext 'CONNECTED')" >> $CONNECTLOG
	else
# shellcheck disable=SC2005 # echo needed for newline.
	    echo "$(gettext 'NOT CONNECTED')" >> $CONNECTLOG
	fi
	sleep 2 #Let connection_dialog_func show the entire log before stopping the feed.
	TAILPID="$(pgrep -f "tail -f $CONNECTLOG")"
	[ -n "$TAILPID" ] && kill "$TAILPID"
	exit $STATUS
}
export -f connect_func

function connection_dialog_func {
	local READLOG='cat' #200829...
	local TAIL_PATH
	TAIL_PATH="$(which tail)"
	if [ -n "$CONNECT_PID" ] && [ -n "$TAIL_PATH" ]; then
	    if [ -L "$TAIL_PATH" ]; then #busybox tail, cannot follow (-f)
	        gtkdialog-splash -timeout 10 -placement center -bg orange -text "$(eval_gettext "Attempting connection to APN \$GPRSAPN.")  $(gettext 'Please wait.')" &
	        sleep 10
	    else
	        READLOG="tail -f"
	    fi
	fi
	$READLOG $CONNECTLOG | \
	  Xdialog --center --wmclass frisbee --title "$(eval_gettext "Frisbee \$VERSION - GPRS Connection Log")" --backtitle "$(gettext 'NOTICE: If log shows a failure to connect, click DISCONNECT to exit.')" --ok-label "$(gettext 'DISCONNECT or stop trying')" --cancel-label "$(gettext 'CLOSE window but stay online')" --logbox - 25 82
	local STATUS=$? #0 = disconnect/stop, 1 = close but stay connected, 255 = abort
	if [ $STATUS -eq 0 ] && [ -s /var/run/ppp-gprs.pid ]; then #200829 end
	    frisbee-gprs-disconnect
	fi
	return $STATUS
}

. /root/.config/gprs.conf

if [ -s /var/run/ppp-gprs.pid ]; then
	GPRS_PPPD_PID="$(grep -ow '[0-9]*' /var/run/ppp-gprs.pid)" #200829...
	#Kill connection if not for current device.
	if pgrep -ax 'pppd' | grep -w "^$GPRS_PPPD_PID" | grep -qv "$GPRSDEV"; then
	    kill "$GPRS_PPPD_PID"
	    rm -f /var/run/ppp-gprs.pid
	fi
	CONNECT_PID='' #200829
	connection_dialog_func
	exit $? #0 = disconnect/stop, 1 = close but stay connected, 255 = abort
else
	echo "#Generated by frisbee-gprs-connect" > /etc/ppp/peers/gprs-generated
	echo "call gprs-editable" >> /etc/ppp/peers/gprs-generated
	if [ "$GPRSUSER" ]; then
	    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" ] \
	  && SAVEVAL="AT+CPIN=\"$GPRSPIN\"" \
	  || SAVEVAL="AT"
	[ "$SAVEVAL" != "$(grep -v "^'" /etc/ppp/chatscripts/gprs-cpin_command 2>/dev/null)" ] \
	  && echo "$SAVEVAL" > /etc/ppp/chatscripts/gprs-cpin_command
	SAVEVAL="AT+CGDCONT=1,\"IP\",\"$GPRSAPN\""
	[ "$SAVEVAL" != "$(grep -v "^'" /etc/ppp/chatscripts/gprs-cgdcont_command 2>/dev/null)" ] \
	  && echo "$SAVEVAL" > /etc/ppp/chatscripts/gprs-cgdcont_command

	if [ -c "$GPRSDEV" ] \
	  && echo ATZ | busybox microcom -t 200 "$GPRSDEV" 2>/dev/null | grep -q 'ATZ'; then #160104
	    echo -n > $CONNECTLOG
	    connect_func & #200829
# shellcheck disable=SC2181 # status test necessary due to '&' above.
	    if [ $? -eq 0 ]; then
	        CONNECT_PID=$! #200829
	        connection_dialog_func #160116
	        exit $? #0 = disconnect/stop, 1 = close but stay connected, 255 = abort
	    else
	        Xdialog --center --wmclass frisbee --title "$(gettext 'Frisbee GPRS Connection Log')" --backtitle "\n$(gettext 'The attempt to connect was unsuccessful:')\n" --no-ok --cancel-label "$(gettext 'Close')" --fixed-font --logbox $CONNECTLOG 25 82
	    fi
	else
	    Xdialog --center --wmclass frisbee --title "$(gettext 'Frisbee GPRS Connect')" --icon "$ICONLIB/error.xpm" --msgbox "\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
fi
exit 0
