#!/bin/sh
# ../scripts/pppoe-connect.  Generated from pppoe-connect.in by configure.
#***********************************************************************
#
# pppoe-connect
#
# Shell script to connect to a PPPoE provider
#
# Copyright (C) 2000 Roaring Penguin Software Inc.
#
# $Id$
#
# This file may be distributed under the terms of the GNU General
# Public License.
#
# LIC: GPL
#
# Usage: pppoe-connect [config_file]
#        pppoe-connect interface user [config_file]
# Second form overrides USER and ETH from config file.
# If config_file is omitted, defaults to /etc//ppp/pppoe.conf
#
#***********************************************************************

# From AUTOCONF
prefix=/usr
exec_prefix=${prefix}
localstatedir=/var

# Paths to programs
IFCONFIG=/sbin/ifconfig
PPPD=/usr/sbin/pppd
SETSID=/usr/bin/setsid
LOGGER="/usr/bin/logger -t `basename $0`"
SYNOGETKEYVALUE="/usr/syno/bin/synogetkeyvalue"
CONFIG_GENERATOR="/etc/ppp/pppoe-generate-config.py"

# Set to "C" locale so we can parse messages from commands
LANG=C
export LANG

# Must be root
if [ -x "/usr/bin/id" -a "`/usr/bin/id -u`" != 0 ] ; then
    echo "$0: You must be root to run this script" >& 2
    exit 1
fi

if test "$SETSID" != "" -a ! -x "$SETSID"; then
    SETSID=""
fi

# Sort out command-line arguments
CONFIG=/etc//ppp/pppoe.conf
case "$#" in
    1)
    CONFIG="$1"
    ;;
    3)
    CONFIG="$3"
    ;;
esac

if test ! -f "$CONFIG" -o ! -r "$CONFIG" ; then
    echo "$0: Cannot read configuration file '$CONFIG'" >& 2
    exit 1
fi

# Check for command-line overriding of ETH and USER
case "$#" in
    2|3)
    ETH="$1"
    USER="$2"
    ;;
esac

OVERRIDE=""
# Check that config file is sane
if test -n "$USER"; then
	OVERRIDE="${OVERRIDE} -r USER \"$USER\""
fi

if test -n "$ETH"; then
	OVERRIDE="${OVERRIDE} -r ETH \"$ETH\""
fi

GENEREATED_CONFIG="$CONFIG.run"
if ! $CONFIG_GENERATOR -i $CONFIG -o $GENEREATED_CONFIG $OVERRIDE; then
    echo "Failed to generate config for $CONFIG" >& 2
    exit 1
fi

# fixed value. it can't be changed by user.
eval CF_BASE="$($SYNOGETKEYVALUE $CONFIG CF_BASE)"
eval PPPD_PIDFILE="$($SYNOGETKEYVALUE $CONFIG PIDFILE).pppd"
RETRY_ON_FAILURE="$($SYNOGETKEYVALUE $CONFIG RETRY_ON_FAILURE)"

# MTU of Ethernet card attached to modem MUST be 1500.  This apparently
# fails on some *BSD's, so we'll only do it under Linux

if test `uname -s` = Linux ; then
    $IFCONFIG $ETH up
    EXISTING_MTU=`$IFCONFIG $ETH 2> /dev/null | grep MTU: | sed -e 's/.*MTU://' | sed -e 's/[^0-9].*//'`
    if test "$EXISTING_MTU" = "" -o "$EXISTING_MTU" -lt 1500 ; then
	$IFCONFIG $ETH mtu 1500
    fi
fi

SERVICEID=$(echo ${CONFIG#"/etc/ppp/pppoe_"} | sed 's/.conf//')
AUTHENTICATE_FAILED_RETRY_TIMES=0
AUTH_FAILED_NOT_RETRY_FILES="/run/pppoe_${SERVICEID}_auth_fail_not_retry_flag"
AUTH_FAILED_NOT_RETRY_FLAGS=0
if [ -f "${AUTH_FAILED_NOT_RETRY_FILES}" ]; then
	AUTH_FAILED_NOT_RETRY_FLAGS=1
	rm "${AUTH_FAILED_NOT_RETRY_FILES}"
fi

while [ true ] ; do
	# Not to retry when WAN port is non-plugged, so we don't need to waste time waiting for LCP timeout
	if test `/bin/get_key_value /etc/synoinfo.conf os_name` = "SRM" ; then
		SEC_MAIN_IFNAME=`/bin/get_key_value /usr/syno/etc/secondarywan.conf ifname`
		echo ${ETH} | /bin/grep "^${SEC_MAIN_IFNAME}\."
		if [ $? -eq 0 ]; then
			WAN_PORT=2
		else
			WAN_PORT=1
		fi
		wan_speed=`/bin/cat /proc/synobios/syno_switch_speed | /usr/bin/cut -d, -f ${WAN_PORT}`

		if test ${wan_speed} = 0 ; then
			sleep 5
			continue
		fi
	fi

	$SETSID $PPPD file "$GENEREATED_CONFIG" &
	echo "$!" > "$PPPD_PIDFILE"
    wait $!
		echo $? > /tmp/pppoe-connect.$$.error

    if test "$RETRY_ON_FAILURE" = "no" ; then
	exit
    fi

    # Run /etc/ppp/pppoe-lost if it exists
    test -x /etc/ppp/pppoe-lost && /etc/ppp/pppoe-lost

    # Re-establish the connection
    $LOGGER -p daemon.notice \
        "PPPoE connection lost; attempting re-connection."

    # Wait a bit in case a problem causes tons of log messages :-)
    sleep 5

	# Break retry loop when authentication failed
	if [ "19" = "$(cat /tmp/pppoe-connect.$$.error)" ]; then
		if [ "0" = "${AUTH_FAILED_NOT_RETRY_FLAGS}" ]; then
			AUTHENTICATE_FAILED_RETRY_TIMES=$((AUTHENTICATE_FAILED_RETRY_TIMES+1))
			if [ ${AUTHENTICATE_FAILED_RETRY_TIMES} -lt 60 ]; then
				sleep 5
				continue
			fi
		fi

		touch "/run/pppoe_${SERVICEID}_auth_fail_flag"
		exit
	fi
done
