#!/bin/sh
#version 0.1
if [ -z "$6" ]
then
    echo Script for making ppp connections using GSM device
    echo Part of Gammu package \(www.mwiacek.com\)
    echo
    echo startppp data device number user pass 9600/14400/19200/28800/43200
    echo Example: startppp data /dev/ttyS1 123 \"plusgsm\" \"plusgsm\" 14400
    echo 9600 and 14400 use one channel
    echo 19200 and 28800 two channels \(HSCSD\)
    echo 43200 three channels \(HSCSD\)
    echo
    echo startppp gprs device number user pass apn
    echo Example: startppp gprs /dev/ttyS1 *99***1# \"\" \"\" www.plusgsm.pl
    exit 0
fi
if [ "$1" = "data" ]
then
    PHONE_DATA_OUTSPEED="-1"
    if [ "$6" = "9600" ]
    then
        PHONE_DATA_OUTSPEED=1
    fi
    if [ "$6" = "14400" ]
    then
        PHONE_DATA_OUTSPEED=2
    fi
    if [ "$6" = "19200" ]
    then
        PHONE_DATA_OUTSPEED=3
    fi
    if [ "$6" = "28800" ]
    then
        PHONE_DATA_OUTSPEED=4
    fi
    if [ "$6" = "43200" ]
    then
        PHONE_DATA_OUTSPEED=6
    fi
    if [ "$PHONE_DATA_OUTSPEED" = "-1" ]
    then
        echo Unknown speed
        exit 0
    fi
    CHAT="TIMEOUT         	10                              	\
    	    ABORT           	'\\nBUSY\\r'                      	\
    	    ABORT           	'\\nNO ANSWER\\r'                 	\
    	    ABORT           	'\\nRINGING\\r\\n\\r\\nRINGING\\r'    	\
    	    ABORT           	'\\nNO CARRIER\\r'               	\
	    ''			"\\r\\dATZ"				\
	    ''			"AT+CBST=51,0,1"			\
	    ''			"AT+CHSN=$PHONE_DATA_OUTSPEED,0,0,0" 	\
    	    TIMEOUT         	10                              	\
    	    OK              	ATD$3                 			\
	    CONNECT		''"
else
    if [ "$1" = "gprs" ]
    then
        CHAT="TIMEOUT         	10                              	\
    		ABORT           '\\nBUSY\\r'                      	\
	        ABORT           '\\nNO ANSWER\\r'                 	\
	        ABORT           '\\nRINGING\\r\\n\\r\\nRINGING\\r'    	\
	        ABORT           '\\nNO CARRIER\\r'                	\
		''		"\\r\\dATZ"				\
		''		"AT+CGDCONT=1,\"IP\",\"$6\""		\
	        TIMEOUT         10                              	\
	        OK              ATD$3                  			\
		CONNECT		''"
    else
	echo Unknown bearer
	exit 0
    fi
fi
echo Device on $2 calling to $3
if [ -z "$4" ]
then
    USER=""
else
    USER="user $4"
    echo $USER
fi
if [ -z "$5" ]
then
    PASS=""
else
    PASS="password $5"
    echo $PASS
fi
/usr/sbin/pppd $2 115200 connect "/usr/sbin/chat -v $CHAT" $USER $PASS 	\
	modem crtscts defaultroute 		      			\
	-detach noccp ipcp-accept-local noipdefault   			\
	mtu 1500 mru 1500
