#!/bin/sh
##
#	Start PPP daemon

modemNotInstalled() {
    grep -qF "no modem" /var/local/telem/ordercode/modem_type
}

modemConfigured() {
    local sim1conf='/etc/ppp/peers/sim1.conf'
    local sim2conf='/etc/ppp/peers/sim2.conf'
    [ -e "${sim1conf}" ] || [ -e "${sim2conf}" ]
}

start() {
    echo -ne "Starting PPP ... "

    if modemNotInstalled; then
        logger -s -p "user.info" -t "$0" "Device has no modem (according to order code). Exiting"
        return 0
    fi
    modemConfigured || { logger -s -p "user.info" -t "$0" "Modem is not configured. Exiting"; return 0; }

    /usr/local/bin/telem/modemst.sh &
    /usr/local/bin/ppp/InitPPP &

    echo -e "OK"
}
stop() {
    echo -ne "Stopping PPP ... "
    killall -9 StartPPP
    killall -9 chat
    killall -9 pppd
    killall -9 modemst.sh
    echo -e "OK"
    return 0
}
restart() {
    stop
    start
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart|reload)
    restart
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac

exit $?
