#!/bin/sh
#120131 rodin.s: internationalized.
#130505 rerwin: ensure network_tray icon shows wireless modem
#130812 Modification as version 1.5 by R. Erwin for Puppy integration

. /etc/ppp/gprs.conf #130812...

#If no setup in place, do it now...
while [ "$GPRSDEV" = "" ] || [ "$GPRSNBR" = "" ] || [ "${GPRSAPN}" = "" ];do
	/usr/bin/pgprs-setup
	[ $? -ne 0 ] && exit
	. /etc/ppp/gprs.conf
done #130812 end

export TEXTDOMAIN=pgprs-connect
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
[ -d /tmp/.network_tray ] \
 && touch /tmp/.network_tray/use-digital-dialup-icons \
 && ( ps aux | grep -q 'wvdial isp[12]' || rm -f /tmp/.network_tray/use_analog_dialup_icons 2>/dev/null ) #140209

if [ "$GPRSUSER" ];then #130812
	echo -e "auth\nuser $GPRSUSER" > /etc/ppp/peers/gprs-auth
	[ "$GPRSPAPONLY" = "true" ] \
	 && echo "require-pap" >> /etc/ppp/peers/gprs-auth
else
	echo "noauth" > /etc/ppp/peers/gprs-auth
fi
SAVEVAL="'ATD$GPRSNBR'"
[ "$SAVEVAL" != "$(cat /etc/ppp/chatscripts/gprs-dial_command 2>/dev/null)" ] \
 && echo "$SAVEVAL" > /etc/ppp/chatscripts/gprs-dial_command
SAVEVAL="'AT+CGDCONT=1,\"IP\",\"$GPRSAPN\"'"
[ "$SAVEVAL" != "$(cat /etc/ppp/chatscripts/gprs-cgdcont_command 2>/dev/null)" ] \
 && echo "$SAVEVAL" > /etc/ppp/chatscripts/gprs-cgdcont_command
[ "${GPRSPIN}" != "" -a "${GPRSPIN}" != "gprspin" ] \
 && SAVEVAL="'AT+CPIN=\"$GPRSPIN\"'" \
 || SAVEVAL="'AT'"
[ "$SAVEVAL" != "$(cat /etc/ppp/chatscripts/gprs-cpin_command 2>/dev/null)" ] \
 && echo "$SAVEVAL" > /etc/ppp/chatscripts/gprs-cpin_command #130812 end

n="r"
while  [ "$n" = "r" ]
do
#/usr/sbin/pppd call gprsmm
echo -e "`eval_gettext "Attempting connection through modem device $GPRSDEV to APN $GPRSAPN...\n"`" #130812
# Keep pppd attached to the terminal: nodetach
if [ "$(file $GPRSDEV 2>&1 | grep 'character special')" ];then #130812
 /usr/sbin/pppd $GPRSDEV \
  file /etc/ppp/options.gprs \
  call gprs-auth \
  connect "/usr/sbin/chat -f /etc/ppp/chatscripts/gprs-connect-chat" \
  disconnect "/usr/sbin/chat -v -s -S -f /etc/ppp/chatscripts/gprs-disconnect-chat" \
  nodetach #130812
 echo "`eval_gettext "Exit status is $?"`" #130812
 echo "$(gettext 'DISCONNECTED')"
else #130812
 echo "$(gettext 'Modem device not detected -- probably not plugged in')" #130812
fi #130812 end
echo ""
echo "$(gettext 'Press ENTER to quit')"
echo "$(gettext 'Press r ENTER to reconnect/try again')"
read n
done
