#!/bin/sh
#***********************************************************************
#
# pppoe-setup
#
# All-purpose slicing/dicing shell script to configure rp-pppoe.
#
# LIC: GPL
#
# Copyright (C) 2000 Roaring Penguin Software Inc.
#
# $Id: pppoe-setup.in,v 1.2 2005/08/10 00:25:18 dfs Exp $
#***********************************************************************

#BK april 2008 modified for Puppy Linux v4.00.
#100803 bugfix posted by fyujj.
#110126 rodin.s: adding gettext
export TEXTDOMAIN=pppoe
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo \"$myMESSAGE\"
}
export LANGORG=$LANG

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

# Paths to programs
IFCONFIG=/sbin/ifconfig
PPPD=/usr/sbin/pppd
PPPOE=${exec_prefix}/sbin/pppoe
ECHO=/bin/echo
LOGGER="/usr/bin/logger -t `basename $0`"

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

CONFIG=/etc/ppp/pppoe.conf

# Protect created files
umask 077

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

$ECHO "$(gettext 'Welcome to the Roaring Penguin PPPoE client setup.  First, I will run')"
$ECHO "$(gettext 'some checks on your system to make sure the PPPoE client is installed')"
$ECHO "$(gettext 'properly...')"
$ECHO ""

# Must be root
if [ "`/usr/bin/id -u`" != 0 ] ; then
    $ECHO "$0: $(gettext 'Sorry, you must be root to run this script')"
    exit 1
fi

# Prototype config file must exist
if [ ! -r "$CONFIG" ] ; then
    $ECHO "`eval_gettext \"Oh, dear, I don't see the file '\\\$CONFIG' anywhere.  Please\"`"
    $ECHO "$(gettext 're-install the PPPoE client.')"
    exit 1
fi

# Must have pppd
if [ ! -x $PPPD ] ; then
    $ECHO "`eval_gettext \"Oops, I can't execute the program '\\\$PPPD'.  You\"`"
    $ECHO "$(gettext '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 "`eval_gettext \"Oh, dear, I can't execute the program '\\\$PPPOE'.  Please\"`"
    $ECHO "$(gettext 're-install the rp-pppoe client.')"
    exit 1
fi

$ECHO "$(gettext 'Looks good!  Now, please enter some information:')"

while [ true ] ; do
    $ECHO ""
    $ECHO "$(gettext 'USER NAME')"
    $ECHO ""
    printf "%s" ">>> `eval_gettext \"Enter your PPPoE user name (default \\\$USER):\"` "
    read U

    if [ "$U" = "" ] ; then
	U="$USER"
    fi

    # Under Linux, "fix" the default interface if eth1 is not available
    if test `uname -s` = "Linux" ; then
	$IFCONFIG $ETH > /dev/null 2>&1 || ETH=eth0
    fi
    $ECHO ""
    $ECHO "$(gettext 'INTERFACE')"
    $ECHO ""
    $ECHO ">>> $(gettext 'Enter the Ethernet interface connected to the DSL modem')"
    $ECHO "$(gettext 'For Solaris, this is likely to be something like /dev/hme0.')"
    $ECHO "`gettext \"For Linux, it will be ethn, where 'n' is a number.\"`"
    printf "%s" "($(gettext 'default') $ETH): "
    read E

    if [ "$E" = "" ] ; then
	E="$ETH"
    fi

    $ECHO ""
    $ECHO "$(gettext 'Do you want the link to come up on demand, or stay up continuously?')"
    $ECHO "$(gettext 'If you want it to come up on demand, enter the idle time in seconds')"
    $ECHO "$(gettext 'after which the link should be dropped.  If you want the link to')"
    $ECHO "`gettext \"stay up permanently, enter 'no' (two letters, lower-case.)\"`"
    $ECHO "$(gettext 'NOTE: Demand-activated links do not interact well with dynamic IP')"
    $ECHO "$(gettext 'addresses.  You may have some problems with demand-activated links.')"
    printf "%s" ">>> `eval_gettext \"Enter the demand value (default \\\$DEMAND)\"`: "
    read D
    if [ "$D" = "" ] ; then
	D=$DEMAND
    fi


    $ECHO ""
    $ECHO "DNS"
    $ECHO ""
    $ECHO "`gettext \"Please enter the IP address of your ISP's primary DNS server.\"`"
    $ECHO "`gettext \"If your ISP claims that 'the server will provide DNS addresses'\"`,"
    $ECHO "$(gettext 'just hit enter.')"
    $ECHO "$(gettext 'If you just press enter, I will assume you know what you are')"
    $ECHO "$(gettext 'doing and not modify your DNS setup. Recommended most cases.')"
    $ECHO ""
    printf "%s" ">>> $(gettext 'Enter the DNS information here:') "

    read DNS1
    
    #v4.00 BK if enter 'server', somehow /etc/resolv.conf gets changed to a
    #symlink to /etc/ppp/resolv.conf (which is a symlink to /etc/resolv.conf)
    #so it's a circle. This script does not do that, something else does if it
    #sees /etc/resolv.conf is empty. A workaround is to put something into it,
    #what I have done is put in a free name server:
    if [ "$DNS1" = "server" ];then
     [ "`grep '^nameserver' /etc/resolv.conf`" = "" ] && echo 'nameserver 208.67.222.222' >> /etc/resolv.conf
    fi

    if [ "$DNS1" != "" ] ; then
        if [ "$DNS1" != "server" ] ; then
	    $ECHO "`gettext \"Please enter the IP address of your ISP's secondary DNS server.\"`"
	    $ECHO "$(gettext 'If you just press enter, I will assume there is only one DNS server.')"
	    printf "%s" ">>> $(gettext 'Enter the secondary DNS server address here:') "
	    read DNS2
	fi
    fi

    while [ true ] ; do
	$ECHO ""
	$ECHO "PASSWORD"
	$ECHO ""
	stty -echo
	printf "%s" ">>> $(gettext 'Please enter your PPPoE password:')    "
	read PWD1
	$ECHO ""
	printf "%s" ">>> $(gettext 'Please re-enter your PPPoE password:') "
	read PWD2
	$ECHO ""
	stty echo
	if [ "$PWD1" = "$PWD2" ] ; then
	    break
	fi

	printf "%s" ">>> $(gettext 'Sorry, the passwords do not match.  Try again? (y/n)')"
	read ANS
	case "$ANS" in
	    N|No|NO|Non|n|no|non)
		$ECHO "$(gettext 'OK, quitting.  Bye.')"
		exit 1
	esac
    done

    # Firewalling
    $ECHO ""
    $ECHO "$(gettext 'FIREWALLING')"
    $ECHO ""
    if test `uname -s` != "Linux" ; then
	$ECHO "$(gettext 'Sorry, firewalling is only supported under Linux.  Consult')"
	$ECHO "$(gettext 'your operating system manuals for details on setting up')"
	$ECHO "$(gettext 'packet filters for your system.')"
	FIREWALL=NONE
    else
	$ECHO "$(gettext 'Please choose the firewall rules to use.  Note that these rules are')"
	$ECHO "$(gettext 'very basic.  You are strongly encouraged to use a more sophisticated')"
	$ECHO "$(gettext 'firewall setup; however, these will provide basic security.  If you')"
	$ECHO "`gettext \"are running any servers on your machine, you must choose 'NONE' and\"`"
	$ECHO "$(gettext 'set up firewalling yourself.  Otherwise, the firewall rules will deny')"
	$ECHO "$(gettext 'access to all standard servers like Web, e-mail, ftp, etc.  If you')"
	$ECHO "$(gettext 'are using SSH, the rules will block outgoing SSH connections which')"
	$ECHO "$(gettext 'allocate a privileged source port.')"
	$ECHO ""
	while [ true ] ; do
	    $ECHO "$(gettext 'The firewall choices are:')"
	    $ECHO "$(gettext '0 - NONE: This script will not set any firewall rules.  You are responsible')"
	    $ECHO "          $(gettext 'for ensuring the security of your machine.  You are STRONGLY')"
	    $ECHO "          $(gettext 'recommended to use some kind of firewall rules.')"
	    $ECHO "1 - $(gettext 'STANDALONE: Appropriate for a basic stand-alone web-surfing workstation')"
	    $ECHO "2 - $(gettext 'MASQUERADE: Appropriate for a machine acting as an Internet gateway')"
	    $ECHO "                $(gettext 'for a LAN')"
	    printf "%s" ">>> $(gettext 'Choose a type of firewall (0-2):') "
	    read a
	    if [ "$a" = 0 -o "$a" = 1 -o "$a" = 2 ] ; then
		break
	    fi
	    $ECHO "$(gettext 'Please enter a number from 0 to 2')"
	done

	case "$a" in
	    0)
		FIREWALL=NONE
		;;
	    1)
		FIREWALL=STANDALONE
		;;
	    2)
		FIREWALL=MASQUERADE
		;;
	esac
    fi

    $ECHO ""
    $ECHO "** $(gettext 'Summary of what you entered') **"
    $ECHO ""
    $ECHO "$(gettext 'Ethernet Interface:') $E"
    $ECHO "$(gettext 'User name:')          $U"
    if [ "$D" = "no" ] ; then
	$ECHO "$(gettext 'Activate-on-demand:') No"
    else
	$ECHO "`eval_gettext \"Activate-on-demand: Yes; idle timeout = \\\$D seconds\"`"
    fi

    if [ "$DNS1" != "" ] ; then
        if [ "$DNS1" = "server" ] ; then
	    $ECHO "`eval_gettext \"DNS addresses:      Supplied by ISP's server\"`"
        else
	    $ECHO "$(gettext 'Primary DNS:')        $DNS1"
	    if [ "$DNS2" != "" ] ; then
		$ECHO "$(gettext 'Secondary DNS:')      $DNS2"
	    fi
        fi
    else
	$ECHO "DNS:                $(gettext 'Do not adjust')"
    fi
    $ECHO "$(gettext 'Firewalling:')        $FIREWALL"
    $ECHO ""
    while [ true ] ; do
        printf "%s" '>>> '$(gettext 'Accept these settings and adjust configuration files (y/n)?')' '
        read ANS
	case "ANS" in
	    Y|y|yes|Yes|oui|Oui)
		ANS=y
		;;
            N|n|no|No|non|Non)
		ANS=n
		;;
	esac
	if [ "$ANS" = "y" -o "$ANS" = "n" ] ; then
	    break
        fi
    done
    if [ "$ANS" = "y" ] ; then
	break
    fi
done

# Adjust configuration files.  First to $CONFIG

$ECHO "$(gettext '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

# Where is pppd likely to put its pid?
if [ -d /var/run ] ; then
    VARRUN=/var/run
else
    VARRUN=/etc/ppp
fi

# Some #$(*& ISP's use a slash in the user name...
sed -e "s&^USER=.*&USER='$U'&" \
    -e "s&^ETH=.*&ETH='$E'&" \
    -e "s&^PIDFILE=.*&PIDFILE=\"$VARRUN/\$CF_BASE-pppoe.pid\"&" \
    -e "s/^FIREWALL=.*/FIREWALL=$FIREWALL/" \
    -e "s/^DEMAND=.*/DEMAND=$D/" \
    -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 "** $(gettext 'Error modifying') $CONFIG"
    $ECHO "** $(gettext 'Quitting')"
    exit 1
fi

if [ "$DNS1" != "" ] ; then
    if [ "$DNS1" != "server" ] ; then
	 $ECHO "$(gettext 'Adjusting') /etc/resolv.conf"
	 if [ -r /etc/resolv.conf ] ; then
	    grep -s "MADE-BY-RP-PPPOE" /etc/resolv.conf > /dev/null 2>&1
	    if [ "$?" != 0 ] ; then
		 $ECHO "  ($(gettext 'But first backing it up to') /etc/resolv.conf-bak)"
		 copy /etc/resolv.conf /etc/resolv.conf-bak
	    fi
	 fi
	 $ECHO "# MADE-BY-RP-PPPOE" > /etc/resolv.conf
	 $ECHO "nameserver $DNS1" >> /etc/resolv.conf
	 if [ "$DNS2" != "" ] ; then
	    $ECHO "nameserver $DNS2" >> /etc/resolv.conf
	 fi
    fi
fi

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

egrep -v "^$U|^\"$U\"" /etc/ppp/pap-secrets-bak > /etc/ppp/pap-secrets
$ECHO "\"$U\"	*	\"$PWD1\"" >> /etc/ppp/pap-secrets
egrep -v "^$U|^\"$U\"" /etc/ppp/chap-secrets-bak > /etc/ppp/chap-secrets
$ECHO "\"$U\"	*	\"$PWD1\"" >> /etc/ppp/chap-secrets

$ECHO ""
$ECHO ""
$ECHO ""
$ECHO "$(gettext 'Congratulations, it should be all set up!')"
$ECHO ""
$ECHO "`gettext \"Type 'pppoe-start' to bring up your PPPoE link and 'pppoe-stop' to bring\"`"
$ECHO "`gettext \"it down.  Type 'pppoe-status' to see the link status.\"`"
exit 0
