#!/bin/bash
# AQ
# 20120904

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
    /usr/local/bin/reset_modem $devpath

    logger -t gprsconfig "Setting parameters for 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 speed 9600. Setting parameters for 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) MODEM FOUND ($?)"
	      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) MODEM FOUND ($?)"
        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 "Setting parameters for 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 speed 9600. Setting parameters for 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) MODEM FOUND ($?)"
	      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) MODEM FOUND ($?)"
        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="net"
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 "Everything right? (y/n)                     "
read CONFIRM
[ "$CONFIRM" == "y" ] || error "Interrupted"

# Creating backups


# Writing values to files
file="/etc/ppp/peers/internet"
echo -n > $file
echo "hide-password" >> $file
echo "$MODEMPORT" >> $file
echo "$LINESPEED" >> $file
echo "user \"$PAPNAME\"" >> $file
echo "connect \"/usr/sbin/chat -v -f /etc/ppp/peers/chat\"" >> $file
chmod 0644 $file
chown 0.0 $file

file="/etc/ppp/peers/chat"
echo -n > $file
echo "TIMEOUT 45" >> $file
echo "ABORT \"NO CARRIER\"" >> $file
echo "ABORT ERROR" >> $file
echo "\"\" AT+CSQ" >> $file
echo "OK ATE1" >> $file
echo "OK AT+CGDCONT=1,\"IP\",\"$INIT\"" >> $file
echo "OK ATD*99***1#" >> $file
echo "CONNECT \c" >> $file
chmod 0644 $file
chown 0.0 $file

if [ ! -f /usr/bin/wvdial.orig ];then
  cp -f /usr/bin/wvdial /usr/bin/wvdial.orig
fi

file="/usr/bin/wvdial"
echo -n > $file
echo "#!/bin/bash" >> $file
echo "# AQ" >> $file
echo "# 20111209" >> $file
echo "" >> $file
echo "on_die()" >> $file
echo "{" >> $file
echo "    logger -t wvdial-replace \"Got TERM signal\"" >> $file
echo "" >> $file
echo "    killall pppd" >> $file
echo "    exit 0" >> $file
echo "}" >> $file
echo "" >> $file
echo "trap 'on_die' TERM" >> $file
echo "" >> $file
echo "COPYS=\`ps ax | grep wvdial | grep bash | wc | awk '{print \$1}'\`" >> $file
echo "if [ \"\$COPYS\" != \"2\" ] ; then" >> $file
echo "    exit 0" >> $file
echo "fi" >> $file
echo "" >> $file
echo "while true ; do" >> $file
echo "    pppdpid=\`pidof pppd\`" >> $file
echo "    if [ \"x\$pppdpid\" == \"x\" ] ; then" >> $file
echo "        logger -t wvdial-replace \"Start PPPD Call\"" >> $file
echo "        pppd call internet" >> $file
echo "    fi" >> $file
echo "    sleep 5" >> $file
echo "    sleep 5" >> $file
echo "    sleep 5" >> $file
echo "    sleep 5" >> $file
echo "    sleep 5" >> $file
echo "    sleep 5" >> $file
echo "done" >> $file
echo "" >> $file
echo "exit 0" >> $file
chmod 0755 $file
chown 0.0 $file

# Register 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
