#!/system/bin/busybox sh


# Paths to programs
#IFCONFIG=/system/bin/ifconfig
PPPD=/system/bin/pppd_pppoe
PPPOE=/system/bin/pppoe
ECHO="/system/bin/log -t pppoe_setup"
#ECHO=echo

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

CONFIG=/data/misc/ppp/pppoe.conf
RESOLV=/data/misc/ppp/resolv.conf
VARRUN=/data/misc/ppp
PPP_DIR=/data/misc/ppp
#LOG=/data/misc/ppp/log
#LOG=/dev/console
SETPROP=/system/bin/setprop


$ECHO "-------------------PPPOE--------------------"     

# Protect created files
umask 077

copy() {
    busybox cp $1 $2
    if [ "$?" != 0 ] ; then
        $ECHO "*** Error copying $1 to $2"     
        $ECHO "*** Quitting."    
        exit 1
    fi
}

# Prototype config file must exist
if [ ! -r "$CONFIG" ] ; then
    $ECHO "Oh, dear, I don't see the file '$CONFIG' anywhere."     
    exit 1
fi

# Must have pppd
if [ ! -x $PPPD ] ; then
    $ECHO "Oops, I can't execute the program '$PPPD'.  You"     
    $ECHO "must install the PPP software suite, version 2.3.10 or later."     
    exit 1
fi
export CONFIG
. $CONFIG

if [ "$DEMAND" = "" ] ; then
    DEMAND=no
fi

# pppoe must exist
if [ ! -x "$PPPOE" ] ; then
    $ECHO "Oh, dear, I can't execute the program '$PPPOE'." >> $LOG
    exit 1
fi
    USER=$1
    $ECHO "USER NAME: $USER"     

    ETH=$2
    $ECHO "INTERFACE: $ETH"     

#    $ECHO "Do you want the link to come up on demand, or stay up continuously?"
#    $ECHO "If you want it to come up on demand, enter the idle time in seconds"
#    $ECHO "after which the link should be dropped.  If you want the link to"
#    $ECHO "stay up permanently, enter 'no' (two letters, lower-case.)"
#    $ECHO "NOTE: Demand-activated links do not interact well with dynamic IP"
#    $ECHO "addresses.  You may have some problems with demand-activated links."
    DEMAND=$3
    $ECHO "DEMAND: $DEMAND"     

    DNS1=$4
    DNS2=$5
    $ECHO "DNS1: $DNS1"     
    $ECHO "DNS2: $DNS2"     

    PWD=$6
#    $ECHO "PASSWORD: $PWD"     
    $ECHO "PASSWORD:"     


    # Firewalling
#    $ECHO "The firewall choices are:"
#    $ECHO "NONE: This script will not set any firewall rules.  You are responsible"
#    $ECHO "          for ensuring the security of your machine.  You are STRONGLY"
#    $ECHO "          recommended to use some kind of firewall rules."
#    $ECHO "STANDALONE: Appropriate for a basic stand-alone web-surfing workstation"
#    $ECHO "MASQUERADE: Appropriate for a machine acting as an Internet gateway"
#    $ECHO "                for a LAN"
    FIREWALL=$7
    $ECHO "FIREWALL: $FIREWALL"     


# Adjust configuration files.  First to $CONFIG

$ECHO "Adjusting $CONFIG"     

copy $CONFIG $CONFIG-bak
if [ "$DNS1" = "server" ] ; then
    DNSTYPE=SERVER
    DNS1=""
    PEERDNS=yes
else
    PEERDNS=no
    if [ "$DNS1" = "" ] ; then
	DNSTYPE=NOCHANGE
    else
	DNSTYPE=SPECIFY
    fi
fi

# Some #$(*& ISP's use a slash in the user name...
busybox sed -e "s&^USER=.*&USER='$USER'&" \
    -e "s&^ETH=.*&ETH='$ETH'&" \
    -e "s&^PIDFILE=.*&PIDFILE=\"$VARRUN/\$CF_BASE-pppoe.pid\"&" \
    -e "s/^FIREWALL=.*/FIREWALL=$FIREWALL/" \
    -e "s/^DEMAND=.*/DEMAND=$DEMAND/" \
    -e "s/^DNSTYPE=.*/DNSTYPE=$DNSTYPE/" \
    -e "s/^DNS1=.*/DNS1=$DNS1/" \
    -e "s/^DNS2=.*/DNS2=$DNS2/" \
    -e "s/^PEERDNS=.*/PEERDNS=$PEERDNS/" \
    < $CONFIG-bak > $CONFIG

if [ $? != 0 ] ; then
    $ECHO "** Error modifying $CONFIG"     
    $ECHO "** Quitting"     
    exit 1
fi


if [ "$DNS1" != "" ] ; then
    if [ "$DNS1" != "server" ] ; then
	$ECHO "Adjusting $RESOLV"     
	if [ -r $RESOLV ] ; then
	    $ECHO "  (First backing it up to $RESOLV-bak)"     
	    busybox cp $RESOLV $RESOLV-bak
	    
	fi
	echo "nameserver $DNS1" >> $RESOLV
	if [ "$DNS2" != "" ] ; then
	    echo "nameserver $DNS2" >> $RESOLV
	fi
    fi
fi

$ECHO "Adjusting $PPP_DIR/pap-secrets and $PPP_DIR/chap-secrets"     
if [ -r $PPP_DIR/pap-secrets ] ; then
    $ECHO "  (But first backing it up to $PPP_DIR/pap-secrets-bak)"     
    copy $PPP_DIR/pap-secrets $PPP_DIR/pap-secrets-bak
else
    busybox cp /dev/null $PPP_DIR/pap-secrets-bak
fi
if [ -r $PPP_DIR/chap-secrets ] ; then
    $ECHO "  (But first backing it up to $PPP_DIR/chap-secrets-bak)"     
    copy $PPP_DIR/chap-secrets $PPP_DIR/chap-secrets-bak
else
    busybox cp /dev/null $PPP_DIR/chap-secrets-bak
fi

busybox egrep -v "^$USER|^\"$USER\"" $PPP_DIR/pap-secrets-bak > $PPP_DIR/pap-secrets
echo "\"$USER\"	*	\"$PWD\"" >> $PPP_DIR/pap-secrets
busybox egrep -v "^$U|^\"$U\"" $PPP_DIR/chap-secrets-bak > $PPP_DIR/chap-secrets
echo "\"$USER\"	*	\"$PWD\"" >> $PPP_DIR/chap-secrets

$ECHO "setprop net.pppoe.pppoe-start setuped"     
$SETPROP "net.pppoe.status" "setuped"
exit 0
