#!/bin/sh
#
# * List of changes (generated via git log --oneline --decorate --pretty="format:%<(35)%ad %<(25)%an %s" ./recipes-connectivity/ppp/ppp-2.4.5/init)
#
# Timestamp                           Author                    Description
# --
# Tue Jan 2 14:52:37 2018 +0100       Steven Dorigotti          BSP-928 PLCM09: Handle Mobile Network Authentication
# Fri Jul 14 19:13:00 2017 +0200      Steven Dorigotti          BSP-726 UN6x: Basic PLCM09 plugin support and connectivity
#
#   /etc/init.d/ppp: start or stop PPP link.
#
# If you want PPP started on boot time (most dialup systems won't need it)
# rename the /etc/ppp/no_ppp_on_boot file to /etc/ppp/ppp_on_boot, and
# follow the instructions in the comments in that file.

test -x /usr/sbin/pppd -a -f /etc/ppp/ppp_on_boot || exit 0
if [ -x /etc/ppp/ppp_on_boot ]; then RUNFILE=1; fi

gen_config()
{
    # generate configuration based on System Parameters
    /etc/ppp/peers/provider.sh > /etc/ppp/peers/provider
    /etc/chatscripts/gprs.sh > /etc/chatscripts/gprs
    rm -f /etc/ppp/*-secrets
    AUTH_TYPE=`sys_params services/mobile/auth/type 2>/dev/null`
    case ${AUTH_TYPE} in
      1) /etc/ppp/secrets.sh > /etc/ppp/pap-secrets || exit 2 ;;
      2) /etc/ppp/secrets.sh > /etc/ppp/chap-secrets || exit 2 ;;
      *) ;;
    esac
    chmod 400 /etc/ppp/*-secrets
}

case "$1" in
  start)
      modprobe ppp_generic 2>/dev/null
      gen_config

      echo -n "Starting up PPP link: pppd"
      if [ "$RUNFILE" = "1" ]; then
        /etc/ppp/ppp_on_boot
      else
        pppd call provider
      fi
      echo "."
    ;;
  stop)
      echo -n "Shutting down PPP link: pppd"
      if [ "$RUNFILE" = "1" ]; then
        poff
      else
        poff provider
      fi
      echo "."
    ;;
  restart|force-reload)
    echo -n "Restarting PPP link: pppd"
    if [ "$RUNFILE" = "1" ]; then      
      poff
      sleep 5
      /etc/ppp/ppp_on_boot
    else                  
      poff provider
      sleep 5
      pppd call provider
    fi
    echo "."
    ;;
  *)
      echo "Usage: /etc/init.d/ppp {start|stop|restart|force-reload}"
      exit 1
    ;;
esac

exit 0
