#!/bin/sh
#
# Many thanks to David Hinds and others (PCMCIA code) for the script
# skeleton
# Ralph Siemsen <ralphs@netwinder.org> for the module.conf portion
# PPP+DNS parsing/prompting configppp Mike Harris <mharris@beer.com>
#
#
PPPOED_DIR=`pwd`
PPPOE_MOD_DIR=$PPPOED_DIR/kernel-patches/module 
FWC=$PPPOED_DIR/contribs/mssclampfw-1.2 
PPPOED=$PPPOED_DIR/pppoed

fail ()
{
    echo ""
    echo "Configuration failed."
    echo ""
    exit 1
}


bye()
{
	echo ""
	echo installing init scripts .....
	echo ""
	cd $PPPOED
	cp init.d_pppoed $RC_INIT_DIR/pppoed
	chmod 755 $RC_INIT_DIR/pppoed
	#chkconfig --add pppoed
	#
	# start the service for the user the first time
	#
	PROMPT=y
	ask_str "would you like to start pppoed?(answer y/n only)" PROMPT
	echo ""
	if [ $PROMPT = "y" ] ; then
		/etc/rc.d/init.d/pppoed restart
	fi

	echo " "
	echo "=================================="
	echo "pppoed installation complete "
	echo "=================================="
	echo " "
}
#=======================================================================

PROMPT=y

arg () {
    VALUE="`echo X"$2" | sed -e 's/^X--[a-zA-Z_]*=//'`"
    eval $1=\"$VALUE\"
}


#=======================================================================

KERNEL_DIR=/usr/src/linux
MODVER=include/linux/modversions.h
RC_INIT_DIR=/etc/rc.d/init.d
CC=gcc


prompt () {
    eval $3=\"$2\"
    if [ "$PROMPT" = "y" ] ; then
	/bin/echo -e "$1 [$2]: \c"
	read tmp
	if [ ! -t 1 ] ; then echo $tmp ; fi
	if [ -n "$tmp" ] ; then eval $3=\"$tmp\" ; fi
    else
	/bin/echo "$1 [$2]"
    fi
}

ask_bool () {
    default=`eval echo '$'$2`
    if [ ! "$default" ] ; then default=n ; fi
    answer=""
    while [ "$answer" != "n" -a "$answer" != "y" ] ; do
	prompt "$1 (y/n)" "$default" answer
    done
    eval "$2=$answer"
}

ask_str () {
    default=`eval echo '$'$2`
    prompt "$1" "`echo $default`" answer
    eval $2=\"$answer\"
}

#=======================================================================

echo ""
echo "    -------- Linux 2.2 PPPOEd Configuration --------"
echo ""
echo "The default responses for each question are correct for most users."
echo "Consult the docs/INSTALL for additional info about each option."
echo ""

config_restart="y"
while [ "$config_restart" == "y" ] ; do
{
ask_str "Linux source directory[where kernel headers are found]" KERNEL_DIR

if [ ! -f $KERNEL_DIR/kernel/Makefile ] ; then
    echo "Linux source tree $KERNEL_DIR is incomplete or missing!"
    if [ -d $KERNEL_DIR/include/linux ] ; then
	echo "    The kernel header files are present, but not " \
	     "the full source code."
    fi
    echo "    See the HOWTO for a list of FTP sites for current" \
	 "kernel sources."
    fail
fi

# What kernel are we compiling for?

version () {
    expr $1 \* 65536 + $2 \* 256 + $3
}

echo ""
for TAG in VERSION PATCHLEVEL SUBLEVEL EXTRAVERSION ; do
    eval `sed -ne "/^$TAG/s/[ 	]//gp" $KERNEL_DIR/Makefile`
done
SRC_RELEASE=$VERSION.$PATCHLEVEL.$SUBLEVEL$EXTRAVERSION
SRC_BASE=$VERSION.$PATCHLEVEL.$SUBLEVEL
VERSION_CODE=`version $VERSION $PATCHLEVEL $SUBLEVEL`
echo "The kernel source tree is version $SRC_RELEASE."
if [ $VERSION_CODE -gt `version 2 2 0` -a \
	$VERSION_CODE -lt `version 2 0 0` ] ; then
    echo "This package requires a 2.2 series kernel."
    fail
fi

CUR_RELEASE=`uname -r`
CUR_VERSION=`uname -v`
CUR_BASE=`uname -r | sed -e "s/^\([0-9]\.[0-9]\.[0-9]*\).*/\1/"`
MATCH=0 # default: different releases and/or architectures
if [ $ARCH = $HOST_ARCH ] ; then
    if [ $CUR_RELEASE = $SRC_RELEASE ] ; then
	MATCH=1 # same release, perfect match
    elif [ $CUR_BASE = $SRC_BASE ] ; then
	MATCH=1 # well, maybe same source tree
	echo "  WARNING: the current kernel is sublevel $CUR_RELEASE."
    else
	echo "  WARNING: the current kernel is version $CUR_RELEASE."
    fi
fi

# Check for consistent kernel build dates

CUR_D=`uname -v | sed -e 's/^#[0-9]* //;s/SMP //'`
CUR_D=`echo $CUR_D | sed -e 's/\(:[0-9][0-9]\) .* \([12][90]\)/\1 \2/'`
echo "The current kernel build date is $CUR_D."
SRC_VERSION="unknown";
if [ -f $KERNEL_DIR/include/linux/compile.h ] ; then
    SRC_VERSION=`grep UTS_VERSION $KERNEL_DIR/include/linux/compile.h |
	sed -e 's/.*"\(.*\)"/\1/'`
    SRC_D=`echo $SRC_VERSION | sed -e 's/^#[0-9]* //;s/SMP //'`
    SRC_D=`echo $SRC_D | sed -e 's/\(:[0-9][0-9]\) .* \([12][90]\)/\1 \2/'`
    if [ $MATCH = 1 -a "$SRC_D" != "$CUR_D" ] ; then
	echo "  WARNING: the source tree has a build date of $SRC_D."
	if [ `date -d "$SRC_D" +%s` -gt `date -d "$CUR_D" +%s` ] ; then
	    echo "  Did you forget to install your new kernel?"
	fi
    elif [ $MATCH = 1 ] ; then
	MATCH=2 # same release & date
    fi
fi
echo ""

MODULE_DIR=/lib/modules/$SRC_RELEASE/net

ask_str "Module install dir" MODULE_DIR
ask_str "RH-like init directory" RC_INIT_DIR
ask_str "compiler for modules. If you have RH7.0, select kgcc instead of gcc" CC

echo "collected information"
echo ""
echo "Kernel source: $KERNEL_DIR"
echo "init_dir: $RC_INIT_DIR"
echo "module install directory: $MODULE_DIR"
echo "Compiler for modules : $CC"

ask_str "is this correct?(answer y/n only)" PROMPT

if [ $PROMPT = "n" ] ; then
	PROMPT=y
	ask_str "would you like to restart?(answer y/n only)" PROMPT
	echo ""
	if [ $PROMPT = "n" ] ; then
		fail
	fi
else
	break
fi

}
done

echo ""
echo installing pppoe module .....
echo ""
cd $PPPOE_MOD_DIR
make KERNEL_DIR=$KERNEL_DIR RC_INIT_DIR=$RC_INIT_DIR CC=$CC MODULE_DIR=$MODULE_DIR install
echo ""
echo installing firewall clamper module .....
echo ""
cd $FWC
make KERNEL_DIR=$KERNEL_DIR RC_INIT_DIR=$RC_INIT_DIR CC=$CC MODULE_DIR=$MODULE_DIR install
echo ""
echo installing pppoed ...
echo ""
cd $PPPOED
$PPPOED/configure
make install
echo ""
echo updating  the module configuration .....
# Add entry to modules.conf
FILE=/etc/modules.conf
if [ ! -e $FILE ]; then touch $FILE; fi
cp -ax $FILE $FILE~
grep -a -v pppox $FILE~ > $FILE
cp -ax $FILE $FILE~
echo "alias char-major-144 pppox" >> $FILE

rm $FILE~

echo ""
#get from user the PPP details

echo " PPPOED installation complete at this point "
echo " I can proceed to setup your ppp-options and DNS"
echo " I dont believe in setting up the firewall for you "
echo "If you insist on automated firewalling setup use lokit available at:
      ftp://ftp.linux.org.uk/pub/linux/alan/Lokkit/"
echo " "


###############################################
PROMPT=y
ask_str "Do you want me to setup PPP for you?(answer y/n only)" PROMPT

if [ $PROMPT = "n" ] ; then
	bye
fi
config_restart="y"
while [ "$config_restart" == "y" ] ; do
{
echo ""
ask_str " Please enter your Username for your ISP" userName
#no point hiding, because we need to verify this
ask_str "Please enter your Password for your ISP" passWord

echo""
echo "Please enter your domain name"
echo "This is often trailing the @ in your email address"
echo "If your email was John@Doe.com, then most often, your"
echo "domain name would be: Doe.com"
ask_str "domain name :" domainName
echo " "
echo " "
echo "collected information"
echo ""
echo "User Name : $userName"
echo "passWord: $passWord"
echo "domainName: $domainName"
echo " "
ask_str "is this correct?(answer y/n only)" PROMPT

if [ $PROMPT = "n" ] ; then
	PROMPT=y
	ask_str "would you like to restart?(answer y/n only)" PROMPT
	echo ""
	if [ $PROMPT = "n" ] ; then
		bye
	fi
else
	break
fi

}
done

echo "setting ppp-options ...."
if [ ! -d /etc/ppp ]; then
	mkdir /etc/ppp
fi

if [ -f /etc/ppp/options ] ; then
	cp /etc/ppp/options /etc/ppp/options.`date +%m%d%Y`
fi

echo "lock
local
nocrtscts
noauth
#be careful with mtu/mru if you are masquerading.
# look at Kal Lin's page at http://www.cs.toronto.edu/~kal/hse/resource.html
mru 1490
mtu 1490
#please make sure you have noaccomp for now
noaccomp
#the construct below is needed by sympatico
name \"$userName@$domainName\"
#you might want to change defaultroute if you have more
#than one pppoe session
defaultroute
hide-password
sync
#it might be a good idea to uncoment the debug below
#debug
#kdebug 7
#if you use the -R option to make it persistent
#then uncomment the next two lines below
#lcp-echo-interval 240
#lcp-echo-failure 3
#nodetach
" > /etc/ppp/options

if [ -f /etc/ppp/pap-secrets ] ; then
	cp /etc/ppp/pap-secrets /etc/ppp/pap-secrets.`date +%m%d%Y`
	rm /etc/ppp/pap-secrets
fi

touch /etc/ppp/pap-secrets
echo "# Secrets for authentication using PAP 
# client        server  secret                  IP addresses
$userName@$domainName * $passWord" > /etc/ppp/pap-secrets
echo "Setting /etc/ppp/pap-secrets* perms to 600"
chmod 600 /etc/ppp/pap-secrets.`date +%m%d%Y`
chmod 600 /etc/ppp/pap-secrets  
echo " "
###############################################
PROMPT=y
ask_str "Do you want me to setup DNS for you?(answer y/n only)" PROMPT

if [ $PROMPT = "n" ] ; then
	bye
fi
echo " "
config_restart="y"
nameServer2=""
while [ "$config_restart" == "y" ] ; do
{
echo "Please enter your Primary DNS server"
echo "It should be in the form ###.###.###.###"
echo "Where the \"#\" is either a number or blank space"
ask_str " Primary DNS server: "  nameServer1
echo " "
echo "If you have a Secondary DNS server please enter it"
echo "If none, just press enter"
ask_str " Secondary DNS server: "  nameServer2
echo " "

echo " "
echo " "
echo "collected information"
echo ""
echo "nameServer1 Name : $nameServer1"
if [ $nameServer2 ] ; then
echo "second nameserver:  $nameServer2"
fi
echo " "
ask_str "is this correct?(answer y/n only)" PROMPT
if [ $PROMPT = "n" ] ; then
	PROMPT=y
	ask_str "would you like to restart?(answer y/n only)" PROMPT
	echo ""
	if [ $PROMPT = "n" ] ; then
		bye
	fi
else
	break
fi

}
done

echo "setting DNS .... domainName: $domainName"

if [ -f /etc/resolv.conf ] ; then
	cp /etc/resolv.conf /etc/resolv.conf.`date +%m%d%Y`
	rm /etc/resolv.conf
fi
touch /etc/resolv.conf
echo "search $domainName" > /etc/resolv.conf
echo "nameserver $nameServer1" >> /etc/resolv.conf

if [ $nameServer2 ] ; then
	echo "nameserver $nameServer2" >> /etc/resolv.conf
fi
echo "Setting /etc/resolv.conf perms to 644"
chmod 644 /etc/resolv.conf

depmod -a
mknod /dev/pppox0 c 144 0
bye
