#!/bin/sh
# Generated automatically from adsl-start.in by configure.
#***********************************************************************
#
# adsl-start
#
# Shell script to bring up an ADSL connection
#
# Copyright (C) 2000 Roaring Penguin Software Inc.
#
# $Id: adsl-start.in,v 1.7 2010-09-01 09:05:19 ykpeng Exp $
#
# This file may be distributed under the terms of the GNU General
# Public License.
#
# LIC: GPL
#
# Usage: adsl-start [config_file]
#        adsl-start interface user [config_file]
# Second form overrides USER and ETH from config file.
# If config_file is omitted, defaults to /etc/ppp/pppoe.conf
#
#***********************************************************************

. /etc.defaults/rc.subr

# Synology, modified for ipv6 PPPoE support
check_connection()
{
	TIME=0
	while [ true ] ; do
	    ${exec_prefix}/sbin/adsl-status $CONFIG > /dev/null 2>&1

		# Looks like the interface came up
	    if [ $? = 0 ] ; then
			# Print newline if standard input is a TTY
			tty -s && $ECHO " Connected!"
			# Synology SYNO for PPPoE ipv6 support
			local ifIpv6=$(lsmod | grep ipv6 )
			if [ -n "${ifIpv6}" ]; then
				local v6eth0=`/sbin/ifconfig eth0 | /bin/grep fe80 | /usr/bin/awk '/inet6/{print $3}'`
				if [ -n "${v6eth0}" ]; then
					local intfPPPoe=`${exec_prefix}/sbin/adsl-status $CONFIG | grep "Point-to-Point" | /usr/bin/awk '{print $1}'`
					if [ -n "$intfPPPoe" ]; then
						/sbin/ip -6 addr add ${v6eth0} dev ${intfPPPoe}
					fi
				fi
			fi
			exit 0
		fi

		if test -n "$FORCEPING" ; then
			$ECHO -n "$FORCEPING"
		else
			tty -s && $ECHO -n "$PING"
		fi
		/bin/sleep $CONNECT_POLL
		TIME=`expr $TIME + $CONNECT_POLL`
	    if [ $TIME -gt $CONNECT_TIMEOUT ] ; then
			break
		fi
	done
	return
}

KERNELMODULE="slhc ppp_generic ppp_async"

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

# Paths to programs
CONNECT=${exec_prefix}/sbin/adsl-connect
ECHO=/bin/echo
IFCONFIG=/sbin/ifconfig

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

# Defaults
CONFIG=/etc/ppp/pppoe.conf
USER=""
ETH=""
ME=`basename $0`
# Must be root
if [ -x "/usr/bin/id" -a "`/usr/bin/id -u`" != 0 ] ; then
    $ECHO "$ME: You must be root to run this script" >& 2
    exit 1
fi

# Debugging
if [ "$DEBUG" = "1" ] ; then
    $ECHO "*** Running in debug mode... please be patient..."
    DEBUG=/tmp/pppoe-debug-$$
    export DEBUG
    mkdir $DEBUG
    if [ "$?" != 0 ] ; then
	$ECHO "Could not create directory $DEBUG... exiting"
	exit 1
    fi
    DEBUG=$DEBUG/pppoe-debug.txt

    # Initial debug output
    $ECHO "---------------------------------------------" > $DEBUG
    $ECHO "* The following section contains information about your system" >> $DEBUG
    date >> $DEBUG
    $ECHO "Output of uname -a" >> $DEBUG
    uname -a >> $DEBUG
    $ECHO "---------------------------------------------" >> $DEBUG
    $ECHO "* The following section contains information about your network" >> $DEBUG
    $ECHO "* interfaces.  The one you chose for PPPoE should contain the words:" >> $DEBUG
    $ECHO "* 'UP' and 'RUNNING'.  If it does not, you probably have an Ethernet" >> $DEBUG
    $ECHO "* driver problem." >> $DEBUG
    $ECHO "Output of ifconfig -a" >> $DEBUG
    $IFCONFIG -a >> $DEBUG
    $ECHO "---------------------------------------------" >> $DEBUG
    if [ "`uname -s`" = "Linux" ] ; then
        $ECHO "* The following section contains information about kernel modules" >> $DEBUG
	$ECHO "* If the module for your Ethernet card is 'tulip', you might" >> $DEBUG
	$ECHO "* want to look for an updated version at http://www.scyld.com" >> $DEBUG
	$ECHO "Output of lsmod" >> $DEBUG
	lsmod >> $DEBUG
	$ECHO "---------------------------------------------" >> $DEBUG
    fi
    $ECHO "* The following section lists your routing table." >> $DEBUG
    $ECHO "* If you have an entry which starts with '0.0.0.0', you probably" >> $DEBUG
    $ECHO "* have defined a default route and gateway, and pppd will" >> $DEBUG
    $ECHO "* not create a default route using your ISP.  Try getting" >> $DEBUG
    $ECHO "* rid of this route." >> $DEBUG
    $ECHO "Output of netstat -n -r" >> $DEBUG
    netstat -n -r >> $DEBUG
    $ECHO "---------------------------------------------" >> $DEBUG
    $ECHO "Contents of /etc/resolv.conf" >> $DEBUG
    $ECHO "* The following section lists DNS setup." >> $DEBUG
    $ECHO "* If you can browse by IP address, but not name, suspect" >> $DEBUG
    $ECHO "* a DNS problem." >> $DEBUG
    cat /etc/resolv.conf >> $DEBUG
    $ECHO "---------------------------------------------" >> $DEBUG
    $ECHO "* The following section lists /etc/ppp/options." >> $DEBUG
    $ECHO "* You should have NOTHING in that file." >> $DEBUG
    $ECHO "Contents of /etc/ppp/options" >> $DEBUG
    cat /etc/ppp/options >> $DEBUG 2>/dev/null
    $ECHO "---------------------------------------------" >> $DEBUG
else
    DEBUG=""
fi

SYNOLoadModules $KERNELMODULE

# Sort out command-line arguments
case "$#" in
    1)
	CONFIG="$1"
	;;
    3)
	CONFIG="$3"
	;;
esac

if [ ! -f "$CONFIG" -o ! -r "$CONFIG" ] ; then
    $ECHO "$ME: Cannot read configuration file '$CONFIG'" >& 2
    exit 1
fi

. $CONFIG

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

# Check for pidfile
if [ -r "$PIDFILE" ] ; then
    PID=`cat "$PIDFILE"`
    # Check if still running
    kill -0 $PID > /dev/null 2>&1
    if [ $? = 0 ] ; then
	$ECHO "$ME: There already seems to be an ADSL connection up (PID $PID)" >& 2
	exit 1
    fi
    # Delete bogus PIDFILE
    rm -f "$PIDFILE" "$PIDFILE.pppd" "$PIDFILE.pppoe" "$PIDFILE.start"
fi

echo $$ > $PIDFILE.start

# Start the connection in the background unless we're debugging
if [ "$DEBUG" != "" ] ; then
    $CONNECT "$@"
    exit 0
fi

# Synology SYNO
# Backup default gateway
#
# If DHCP is enabled, we don't backup gateway at boot time.
# Because the dhcpcd will write its config to backup files.
if [ "$BOOT_TIME" = "yes" ]; then
	BOOTPROTO=`grep -s ^BOOTPROTO /etc/sysconfig/network-scripts/ifcfg-eth0 | awk -F = '{print $2}' | sed 's/ //g'`
	# We use BOOTPROTO != static rather than "=dhcp" because the /etc/rc use
	# such discriminant
	if [ "${BOOTPROTO}" != "static" ];then
		BackupConfig="no"
	fi
fi
if [ "$BackupConfig" != "no" ]; then
	echo "Backup old gateway"
	OldGateway=`netstat -nr|grep ^0.0.0.0 | awk -F 0.0.0.0 '{print $2}' | sed 's/ //g'`
	if [ "$OldGateway" != "" ]; then
		echo "old_gateway=$OldGateway" > /etc/ppp/old_gateway.bak
		/bin/ip route delete default
	fi
fi

$CONNECT "$@" > /dev/null 2>&1 &
CONNECT_PID=$!

if [ "$CONNECT_TIMEOUT" = "" -o "$CONNECT_TIMEOUT" = 0 ] ; then
		exit 0
fi

# Don't monitor connection if dial-on-demand
if [ "$DEMAND" != "" -a "$DEMAND" != "no" ] ; then
    exit 0
fi

if [ "$BOOT_TIME" = "yes" ]; then
	check_connection > /dev/null 2>&1 &
	exit 0
fi
# Monitor connection
check_connection

$ECHO "TIMED OUT" >& 2
# Timed out!  Kill the adsl-connect process and quit
kill $CONNECT_PID > /dev/null 2>&1

# SYNO, Synology, restore old gateway
if [ "$OldGateway" != "" ]; then
	echo "Restore old default gateway [$OldGateway]"
	/bin/ip route add default via $OldGateway
	rm -rf /etc/ppp/old_gateway.bak
fi

# Clean up PIDFILE(s)
rm -f "$PIDFILE" "$PIDFILE.pppd" "$PIDFILE.pppoe" "$PIDFILE.start"

# SYNO, Synology
if [ -f "/tmp/adsl-connect.${CONNECT_PID}.error" ]; then
	error=`cat /tmp/adsl-connect.${CONNECT_PID}.error`
	rm /tmp/adsl-connect.${CONNECT_PID}.error
	echo "errno: $error"
	exit ${error:=1}
fi

killall -9 pppd > /dev/null 2>&1
killall -9 pppoe > /dev/null 2>&1

SYNOUnloadModules $KERNELMODULE

exit 1

