#!/bin/bash
# Turn on cell radio, start its ppp daemon
radio=cell
# Get power port and serial port from configuration
powerSerial=`python /home/hotspot/smc-script/modemConfig.py $MODEM_CONFIG_FILE $radio`
if [ $? != 0 ]; then
   echo $powerSerial
   echo Could not get configuration for $radio radio
   exit 1
fi

# Get power port
powerPort=`echo $powerSerial | awk '{print $1}'`

# Get serial port
serialPort=`echo $powerSerial | awk '{print $2}'`

echo Turn on power port $powerPort
/home/smc/bin/SetPowerSwitch $powerPort 1
# Wait for modem initialization
sleep 8


echo Run pppd on port $serialPort
pppd connect '/usr/sbin/chat -v -f /home/hotspot/smc-script/verizon-attach.chat' $serialPort 115200 modem debug defaultroute persist maxfail 0 usepeerdns
echo ppp status: $?

# Test connection to internet try to ping google name server
success=false
for i in {1..30}
do
  echo Try \#$i to ping google nameserver at 8.8.8.8
  ping -c 3 8.8.8.8
  if [ $? -eq 0 ]; then
    echo Able to ping google nameserver at 8.8.8.8
    success=true
    break
  fi
  sleep 5
done

if ! $success; then
  echo Cannot ping google name server - no internet connection?
  exit 1
fi

echo synchronize clock with NTP
ntpdate -u 0.in.pool.ntp.org

