#!/bin/sh
# This script belongs to RPPPPoEK 0.4

# You can modify it under the same terms as the main-application
# This is the GPL v2 or (at your option) any later version.
# For further details about the license read the file COPYING in
# in the source-package

# Modify the following lines according to your system.

# Path to ifconfig
IFCONFIG=/sbin/ifconfig

# Path to echo
ECHO=/usr/bin/echo

# Path to grep
GREP=/bin/grep

# Path to cut
CUT=/usr/bin/cut

# Path to sudo
SUDO=/usr/bin/sudo

# Path to pppoe-start (Roaring Penguin utils)
RPC=/usr/sbin/pppoe-start

# Path to pppoe-stop (Roaring Penguin utils)
RPD=/usr/sbin/pppoe-stop

#####################################################################
#  You shouldn't modify anything below this line, if you don't know #
#  what you're doing!                                               #
#####################################################################

# At first check if the tools are present and executable
if [ ! -x $IFCONFIG ]; then
	$ECHO "$IFCONFIG isn't executable. Aborting."
	exit -1
fi
if [ ! -x $GREP ]; then
	$ECHO "$GREP isn't executable. Aborting."
	exit -1
fi
if [ ! -x $CUT ]; then
	$ECHO "$CUT isn't executable. Aborting."
	exit -1
fi
if [ ! -x $SUDO ]; then
	$ECHO "$SUDO isn't executable. Aborting."
	exit -1
fi
if [ ! -x $RPC ]; then
	$ECHO "$RPC isn't executable. Aborting."
	exit -1
fi
if [ ! -x $RPD ]; then
	$ECHO "$RPD isn't executable. Aborting."
	exit -1
fi

ERROR=0

case "$1" in
	connect)
		$SUDO $RPC || ERROR=1
	;;
	disconnect)
		$SUDO $RPD || ERROR=1
	;;
	status)
		if [ -z $2 ]; then
			$ECHO "Usage: $0 status <device>"
			ERROR=-1
		else
			ADDR=`$IFCONFIG $2 2>/dev/null | $GREP inet | $CUT -f 2 -d: | $CUT -f 1 -d" "`
			if [ -z $ADDR ]; then
				ERROR=1
			else
				$ECHO $ADDR
			fi
		fi
	;;
	*)
	$ECHO "Usage: $0 {connect|disconnect|status <device>}"
	;;
esac

exit $ERROR
