#!/bin/bash

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."

RETRY="y"
RETURN_CODE==$1

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

    chat -f /etc/ppp/peers/verizon-chat-program < /dev/ttyUSB3 > /dev/ttyUSB3

    RETURN_CODE=$?

    if [[ $RETURN_CODE != 0 ]] ; then
        echo "Programming failed."

        echo -n "Retry? [y/n] "
        read RETRY
    fi
done

exit 0

