#!/bin/sh

[ "$METHOD" == "ppp" ] || exit 0
[ "$IF_USE_TEMPLATE" ] || exit 0

[ -d /var/lock ] || mkdir /var/lock

[ -f "/etc/ppp/templates/$IF_USE_TEMPLATE" ] || {
	logger "$0: requested template '/etc/ppp/templates/$IF_USE_TEMPLATE' not found."
	exit 1
}

IDLEOPTIONS="persist\nmaxfail 0"
[ "$IF_PPP_IDLETIME" ] && {
  if [ "$IF_PPP_IDLETIME" -gt 0 ]; then
	IDLEOTPIONS="idletime $IF_PPP_IDLETIME"
  fi
}

[ "$IF_PPP_MTU" ] || IF_PPP_MTU=1412
if [ "$IF_PPP_MTU" -gt 0 ] ; then
	NETWORKOPTIONS="mtu $IF_PPP_MTU\nmru $IF_PPP_MTU"
fi

[ "$IF_PPP_VPI" ] || IF_PPP_VPI=1
[ "$IF_PPP_VCI" ] || IF_PPP_VCI=32

REPLACE="
	s,#IDLEOPTIONS#,$IDLEOPTIONS,
	s,#ENCAPS#,$IF_PPP_ENCAPS,
	s,#VPI#,$IF_PPP_VPI,
	s,#VCI#,$IF_PPP_VCI,
	s,#USERNAME#,$IF_PPP_USERNAME,
	s,#PASSWORD#,$IF_PPP_PASSWORD,
	s,#DEVICE#,$IF_PPP_DEVICE,
	s,#MODEMIP#,$IF_PPP_MODEMIP,
	s,#NETWORKOPTIONS#,$NETWORKOPTIONS,
"

sed "$REPLACE" < /etc/ppp/templates/$IF_USE_TEMPLATE > /etc/ppp/peers/$IF_PROVIDER

case "$IF_PPP_DEVICE" in
eth*)
	ip link set up dev $IF_PPP_DEVICE
	;;
nas*)
	[ "$IF_PPP_ENCAPS" ] || encap=0
	[ "$IF_PPP_PAYLOAD" ] || payload=1

	case "$IF_PPP_PAYLOAD" in
		bridged)
			payload=1
			;;
		routed)
			payload=0
			;;
		*)
			echo "Wrong payload, use either bridged or routed"
			exit 1
			;;
	esac

	case "$IF_PPP_ENCAPS" in
		llc)
			encap=0
			;;
		vc)
			encap=1
			;;
		*)
			echo "Wrong Encapsulation use either llc or vc"
			exit 1
			;;
	esac

	count=$(echo $IF_PPP_DEVICE|sed -e "s/nas//")
	br2684ctl -b -c $count -e $encap -p $payload -a $IF_PPP_VPI.$IF_PPP_VCI
	ip link set up dev $IF_PPP_DEVICE
	;;
esac

case "$IF_USE_TEMPLATE" in
umts)
	# wait for card to initialize
	COUNT=0
	while [ $((COUNT++)) -lt 20 ]; do
		[ -c "$IF_PPP_DEVICE" ] && break
		[ $COUNT -eq 20 ] && {
			logger "$0: '$IF_PPP_DEVICE' not found"
			exit 1
		}
		sleep 1
	done

	# wait till the SIM is ready
	comgt -s /etc/comgt/waitready.comgt -d $IF_PPP_DEVICE || exit 1

	# set pincode
	if [ "$IF_PIN" ]; then
		COMGTPIN="$IF_PIN" comgt PIN -d $IF_PPP_DEVICE || exit 1
	fi

	# set APN
	COMGTAPN="$IF_APN" comgt APN -d $IF_PPP_DEVICE || exit 1

	# set umts/gprs mode if available (Option cards only)
	case "$IF_MODE" in
		gprs_only)	comgt 2G -d $IF_PPP_DEVICE ;;
		umts_only)	comgt 3G -d $IF_PPP_DEVICE ;;
		umts_first)	comgt 3G2G -d $IF_PPP_DEVICE ;;
		"")		;;
		*)		echo "unknown mode, exiting"
				exit 1
				;;
	esac
	;;
esac