#!/bin/bash
# AQ
# 20120321

CHAT=`which chat`
unset Modems
declare -a Modems[0]

function get_operator {
declare -a gsmids=(\
[22601]='VODAFONE' \
[22610]='ORANGE' \
);
NNGSM="0"
local gsmid="`$CHAT -v -e -t 2 "" "AT+CIMI" "OK" 2>&1 < $devpath >$devpath`"
gsmid=`echo $gsmid | sed -n "s/.*\([0-9]\{5\}\)[0-9]\{10\}.*/\1/p"`
gsm=${gsmids[$gsmid]};
logger -t gprsconfig "$gsm"
if [ -z "$gsm" ]; then gsm="$gsmid";NNGSM="1";return 1; fi
return 0
}

rm -rf /etc/ppp/peers/ppp* > /dev/null 2>&1

echo "Scanning COM ports"
logger -t gprsconfig "Scanning COM ports"

serports=`cat /proc/tty/driver/serial|grep -v " uart:unknown "| grep uart |\
    awk '{print $1}'|sed -n 's/\([0-9]\)\+.*/ttyS\1/p'`

for port in $serports; do
    devpath=/dev/$port

    logger -t gprsconfig "Setting parameters for the port $devpath"
    stty -F $devpath raw -hup cread clocal -crtscts -parenb iexten echoe echok 115200
    $CHAT -v -t 1 '' '\K' < $devpath > $devpath

    echo -n $devpath "=>"
    $CHAT -v -t 2 '' 'AT' 'OK' <$devpath >$devpath
    if [ $? -gt 0 ]; then
	logger -t gprsconfig "Trying to search at a speed of 9600. Setting parameters for the port $devpath"
	stty -F $devpath raw -hup cread clocal -crtscts -parenb iexten echoe echok 9600
	$CHAT -v -t 1 '' '\K' < $devpath > $devpath
	$CHAT -v -t 2 '' 'AT' 'OK' <$devpath >$devpath
	    if [ $? -gt 0 ]; then
	      logger -t gprsconfig "$devpath no modem ($?)"
	      echo " No modem "
	    else
	      logger -t gprsconfig "$devpath (9600) FOUND MODEM ($?)"
	      echo " Modem found "
	      get_operator;
		 if [ "$NNGSM" == "0" ]; then
		    Modems[${#Modems[*]}]="$port $gsm 9600";
		 else 
		    echo "Unknown $gsm provider on $devpath "
		 fi
	    fi
    else
	logger -t gprsconfig "$devpath (115200) FOUND MODEM ($?)"
        echo " Modem found "
	get_operator;
	  if [ "$NNGSM" == "0" ]; then
	    Modems[${#Modems[*]}]="$port $gsm 115200";
	  else 
	    echo "Unknown $gsm provider on $devpath "
	  fi
    fi
done

unset devpath;
for devpath in `ls /dev/ttyD* 2>/dev/null`; do
	port=`echo $devpath | sed "s/\/dev\///"`
	logger -t gprsconfig "Set parameters for $devpath port"
	stty -F $devpath raw -hup cread clocal -crtscts -parenb iexten echoe echok 115200
	$CHAT -v -t 1 '' '\K' < $devpath > $devpath

    echo -n $devpath "=>"
    $CHAT -v -t 2 '' 'AT' 'OK' <$devpath >$devpath
    if [ $? -gt 0 ]; then
	logger -t gprsconfig "Trying to search at a speed of 9600. Set parameters for $devpath port"
	stty -F $devpath raw -hup cread clocal -crtscts -parenb iexten echoe echok 9600
	$CHAT -v -t 1 '' '\K' < $devpath > $devpath
	$CHAT -v -t 2 '' 'AT' 'OK' <$devpath >$devpath
	    if [ $? -gt 0 ]; then
	      logger -t gprsconfig "$devpath No modem ($?)"
	      echo " No modem "
	    else
	      logger -t gprsconfig "$devpath (9600) FOUND MODEM ($?)"
	      echo " Modem found "
	      get_operator;
		 if [ "$NNGSM" == "0" ]; then
		    Modems[${#Modems[*]}]="$port $gsm 9600";
		 else 
		    echo "Unknown $gsm provider on $devpath "
		 fi
	    fi
    else
	logger -t gprsconfig "$devpath (115200) FOUND MODEM ($?)"
        echo " Modem found "
	get_operator;
	  if [ "$NNGSM" == "0" ]; then
	    Modems[${#Modems[*]}]="$port $gsm 115200";
	  else 
	    echo "Unknown $gsm provider on $devpath "
	  fi
    fi
done

echo "Scanning USB ports"
logger -t gprsconfig "Scanning USB ports"

unset devpath;
for devpath in `ls /dev/gsmmodem* 2>/dev/null`; do
    get_operator;
    if [ "$NNGSM" == "0" ]; then
      devpath=`echo $devpath | sed -n 's/\/dev\/\(.*\)/\1/p'`
      Modems[${#Modems[*]}]="$devpath $gsm 115200";
    else
      echo "Unknown $gsm provider on $devpath "
    fi
done

unset devpath;
for devpath in `ls /dev/ttyACM* 2>/dev/null`; do
    get_operator;
    if [ "$NNGSM" == "0" ]; then
      devpath=`echo $devpath | sed -n 's/\/dev\/\(.*\)/\1/p'`
      Modems[${#Modems[*]}]="$devpath $gsm 115200";
    else 
      echo "Unknown $gsm provider on $devpath "
    fi
done

unset devpath;
for devpath in `ls /dev/ttyUSB* 2>/dev/null`; do
    get_operator;
    if [ "$NNGSM" == "0" ]; then
      devpath=`echo $devpath | sed -n 's/\/dev\/\(.*\)/\1/p'`
      Modems[${#Modems[*]}]="$devpath $gsm 115200";
    else 
      echo "Unknown $gsm provider on $devpath "
    fi
done

if [ ${#Modems[*]} -eq 0 ]; then
    echo ""
    echo "Modems not found"
    echo ""
    sleep 5;
    exit 1;
fi

echo
echo "Ports found:"
for (( i=0; i<${#Modems[*]}; i++ )) do
    if [ -z "$choices" ]; then
        choices="${Modems[$i]}"
    else
        choices="$choices;${Modems[$i]}"
    fi
done

if [ -z "${Modems[1]}" ]; then
	choice="${Modems[0]}"
	echo "1) $choice"
else
	echo " Enter the number of the modem you are using";
	IFS=';'
	select choice in $choices; do
	if [ -n "$choice" ]; then break; fi
	done
	unset IFS;
fi

devname=`echo $choice | awk '{print $1}'`
opsos=`echo $choice | awk '{print $2}'`
LINESPEED=`echo $choice | awk '{print $3}'`

unset devpath;
devpath=/dev/$devname

unset ports
declare -a ports
echo "GPRS setup"

function error() {
	echo "  $@"
	exit 1
}

if [ "$opsos" == "VODAFONE" ];
then 
	NAME="vodafone"
	PAPNAME="vodafone"
	PAPPASSWORD="vodafone"
	DEVICE="ppp0"
	INIT="internet.vodafone.ro"
fi

if [ "$opsos" == "ORANGE" ];
then 
	NAME="orange"
	PAPNAME=""
	PAPPASSWORD=""
	DEVICE="ppp0"
	INIT="internet"
fi

echo "New settings"

if [ "$NAME" == "not specified" ];
then
    error "Operator not specified"
fi

echo "Provider name: $NAME"

if [ ! -z $INIT2 ];
then
echo "Select an initialization string for this connection:"
echo "1 - $INIT"
echo "2 - $INIT2"
read WHICH_INIT

if [ "$WHICH_INIT" == "2" ];then
  INIT=$INIT2
fi
fi

echo "Initialization string: $INIT"

MODEMPORT=$devpath
echo "Modem port: $MODEMPORT"

MTU="1000"

echo "Is everything right? (y/n)                     "
read CONFIRM
[ "$CONFIRM" == "y" ] || error "Interrupted"

# Creating backups
cp -f /root/.wvdial.conf /root/.wvdial.conf.bak

# Writing values to files
file="/root/.wvdial.conf"
echo -n > $file
echo "[Dialer Defaults]" >> $file
echo "Modem = $MODEMPORT" >> $file
echo "ISDN = off" >> $file
echo "Modem Type = Analog Modem" >> $file
echo "Baud = $LINESPEED" >> $file
echo "Init = ATZ" >> $file
echo "Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0" >> $file
echo "Init3 = AT+CGDCONT=1,\"IP\",\"$INIT\"" >> $file
echo "Init4 = " >> $file
echo "Init5 = " >> $file
echo "Init6 = " >> $file
echo "Init7 = " >> $file
echo "Init8 = " >> $file
echo "Init9 = " >> $file
echo "Phone = *99***1#" >> $file
echo "Phone1 = " >> $file
echo "Phone2 = " >> $file
echo "Phone3 = " >> $file
echo "Phone4 = " >> $file
echo "Dial Prefix = " >> $file
echo "Dial Attempts = 2" >> $file
echo "Dial Command = ATM1L3DT" >> $file
echo "Ask Password = off" >> $file
echo "Password = $PAPPASSWORD" >> $file
echo "Username = $PAPNAME" >> $file
echo "Auto Reconnect = off" >> $file
echo "Abort on Busy = off" >> $file
echo "Carrier Check = on" >> $file
echo "Check Def Route = on" >> $file
echo "Abort on No Dialtone = on" >> $file
echo "Stupid Mode = off" >> $file
echo "Idle Seconds = 0" >> $file
echo "Auto DNS = on" >> $file

chmod 0644 $file
chown 0.0 $file

# Registering user
PAPAUTHFILE="/etc/ppp/pap-secrets"
TMP="/tmp/rc.$RANDOM"

if [ "$PAPNAME" != "" ];
then
	res=`grep $PAPNAME $PAPAUTHFILE`
	if [ "x$res" == "x" ] ; then
		echo "$PAPNAME	*	$PAPPASSWORD" >> $PAPAUTHFILE
	else
		sed "/$PAPNAME/d" $PAPAUTHFILE > $TMP
		mv -f $TMP $PAPAUTHFILE
		echo "$PAPNAME	*	$PAPPASSWORD" >> $PAPAUTHFILE
	fi
fi
chown 0.0  $PAPAUTHFILE
chmod 0600 $PAPAUTHFILE
