#!/bin/bash

logger "Programmer: started."

if [[ ! -e /dev/de910-1 ]] ; then
    logger "A DE910 modem does not appear to be plugged in.  Terminating."
    echo "A DE910 modem does not appear to be plugged in.  Terminating."
    exit 1
fi

if [[ $UID != 0 ]] ; then
    logger "Programmer: started without root.  Terminating."
    echo "While you can use the cell modem without root access, "
    echo "programming the cell modem must be done as sudo or root."
    exit 1
fi


BG_MODE=""

if [[ $# -gt 0 ]] ; then
    if [[ $1 == "-b" ]] ; then
        logger "Programmer:  Starting in background mode, sleeping briefly to ensure device is available..."
        sleep 5
        BG_MODE="x"
    else
        echo "DE910 Programmer started with invalid args.  Terminating."
        logger "DE910 Programmer started with invalid args.  Terminating."
        exit 1
    fi
fi

if [[ ! -e /dev/de910-1 ]] ; then
    if [[ -z $BG_MODE ]] ; then 
        logger "A DE910 modem does not appear to be plugged in.  Terminating."
    else
        echo "A DE910 modem does not appear to be plugged in.  Terminating."
    fi
    exit 1
fi

if [[ ! -z $BG_MODE && -f /etc/DE910_programmed_datetime ]] ; then
    logger "DE910 Programmer: started in background mode, but the file /etc/DE910_programmed_dattime exists."
    logger "This indicates the modem has already been programmed.  Assuming script misconfiguration and terminating."

    exit 1
fi

if [[ ! -z $BG_MODE && ! -f /etc/DE910_autoprogram ]] ; then
    logger "DE910 Programmer: started in background mode, but the file /etc/DE910_autoprogram does not exist."
    logger "Assuming script misconfiguration and terminating."

    exit 1
fi

if [[ -z $BG_MODE ]] ; then

  echo ""
  echo "NOTE:"
  echo ""
  echo "For best results:"
  echo " - the modem should be in an area with good signal"
  echo " - your device must already be added to your account"
  echo ""
  echo "Even in ideal conditions this can take a few attemps,"
  echo "depending on load on the cell network."
  echo ""
  echo "Having to retry a few times is not abnormal."
  echo ""
  echo "After successful programming, you will see SUCEESS "
  echo "followed by errors reported to the console as the "
  echo -n "modem reboots with its new program, this is normal."
  sleep 1
  echo -n ".."
  sleep 1
  echo ".."
  echo ""

  echo "Force disconnecting all existing calls if any exist."
  /etc/ppp/peers/telit-verizon-disconnect
  echo "All existing calls should now be disconnected."

else
  logger "DE910 Programmer:  Force disconnecting all existing calls if any exist."
  /etc/ppp/peers/telit-verizon-disconnect | logger
  logger "DE910 Programmer:  All existing calls should now be disconnected."
fi


RETRY="y"
RETURN_CODE=1

while [[ "$RETRY" == "y" && $RETURN_CODE != 0 ]] ; do

    if [[ -z $BG_MODE ]] ; then
        chat -f /etc/ppp/peers/verizon-chat-program < /dev/de910-1 > /dev/de910-1 ; RETURN_CODE=${PIPESTATUS[0]}
    else
        logger "DE910 Programmer:  Attempting programming..."
        bash -c "chat -f /etc/ppp/peers/verizon-chat-program < /dev/de910-1 > /dev/de910-1" | logger ; RETURN_CODE=${PIPESTATUS[0]}
        echo "RETURN CODE IS $RETURN_CODE"
    fi

    #RETURN_CODE=$?

    if [[ $RETURN_CODE != 0 && -z $BG_MODE ]] ; then
        echo "Programming failed."
        echo -n "Retry? [y/n] "
        read RETRY
    elif [[ $RETURN_CODE != 0 && ! -z $BG_MODE ]] ; then
        logger "DE910 Programmer:  failed OTASP programming.  Sleeping 60 seconds..."
        sleep 60
    fi

done

echo $(date) > /etc/DE910_programmed_datetime
python /usr/local/bin/de910-get-meid.py > /etc/DE910_MEID

logger "DE910 Programmer:  Done."

exit 0


