#!/bin/sh
#
# Starts and stops the CDMA modem for Spark
#
# chkconfig: - 55 45
#
# processname: pppd
RETVAL=0
prog=pppd
case "$1" in
  start)
  if [ ! -z `pidof pppd` ]; then
  echo " PPPD already start";
  else
  echo " PPPD is offline. Trying to start ... ";
      echo -n "Trying to switch: "
      /etc/ppp/modemsw
      switch=`cat /tmp/switch`
     if [ -z `cat /tmp/switch` ]; then
     echo "Can't find usb modem" 
     exit 0
     fi
     if [ "$switch" = "19d2:2000" ] || [ "$switch" = "19d2:0016" ] || [ "$switch" = "19d2:0117" ]; then
     echo "Find ZTE Modem "
     /etc/ppp/comgt
     elif [ "$switch" = "1199:0fff" ] || [ "$switch" = "1199:0120" ]
     then
     insmod /root/spark/modules/sierra.ko
     sleep 2
     fi
    if [ `cat /proc/tty/driver/usbserial | grep -c "pl2303 name"` = 2 ]; then
    echo "Find Huawei EC325"
    start-stop-deamon -S -c root:root -a /etc/ppp/pppd --call cdmaIT
    else
     echo -n "Trying find port:"
     i=0
     while [ `cat /proc/tty/driver/usbserial | grep -c "GSM modem"` = 0 ] && [ $i -le 100 ] ; do
     echo -n "."
     i=$(( $i + 1))
     done
     echo "[ OK ]";
     echo "Starting $prog:"
     if [ "$switch" = "19d2:2000" ] || [ "$switch" = "19d2:0117" ] || [ "$switch" = "19d2:0016" ]
     then
     start-stop-daemon -S -c root:root -a /etc/ppp/pppd -- call 3gZTE
     elif [ "$switch" = "12d1:1412" ] || [ "switch" = "12d1:1413" ]
     then
     start-stop-daemon -S -c root:root -a /etc/ppp/pppd -- call cdma2IT
     elif [ "$switch" = "1199:0fff" ] || [ "$switch" = "1199:0120" ]
     then
     start-stop-daemon -S -c root:root -a /etc/ppp/pppd -- call cdma2IT
     elif [ "$switch" = "12d1:1001" ] || [ "$switch" = "12d1:1446" ] || [ "$switch" = "12d1:1449" ] || [ "$switch" = "12d1:1553" ]
     then
     start-stop-daemon -S -c root:root -a /etc/ppp/pppd -- call 3g
     elif [ "$switch" = "106c:3714" ] || [ "$switch" = "106c:3b03" ] || [ "$switch" = "1410:2110" ]
     then
     start-stop-daemon -S -c root:root -a /etc/ppp/pppd -- call cdmaIT
     else
     start-stop-daemon -S -c root:root -a /etc/ppp/pppd -- call cdma
     fi
    fi 
  if [ "$?" = "0" ]; then
  echo "[ OK ]";
  cat /etc/ppp/resolv.conf > /root/spark/etc/resolv.conf
  else
  echo "[FAILED]";
  fi
 fi	  
  ;;
  stop)
  if [ ! -z `pidof pppd` ] ; then
  echo " PPPD already start. Trying to stop "
      echo -n "Stopping $prog: "
	  start-stop-daemon -K -q -x /etc/ppp/pppd
 i=0
 while [ -n `pidof pppd` ] && [ $i -le 30 ] ; do
 echo -n "."
 i=$(( $i + 1 ))
 done
  echo "[ OK ]";
  else
 echo " PPPD already stop."
 fi
   ;;
  *)
	echo $"Usage: $0 {start|stop}"
	RETVAL=1
esac
exit $RETVAL
