#!/bin/sh
#
# Changed by Claudio Roberto Cussuol
# to use on-the-fly configuration - 12/31/2003

# Clear additional packages file
echo > floppy/packages

echo
echo "Configuring system for PPP dialup."
echo
echo "NOTE: The PPP Dialup support is currently unmaintained and may require further"
echo "      configuration and/or tweaking to make it function properly for your ISP."
echo "      If you are interested in helping support the PPP dialup function of Coyote,"
echo "      please email jjackson@vortech.net for more information. You need to be"
echo "      familiar with Linux and BASH shell scripting to assist in this effort."
echo
# Allow for local network reconfig
config_local

# Start configuring options
echo
echo
echo "OPTIONS CONFIGURATION"
echo "Demand Dial:"
echo "Initiate the link only on demand, i.e. when data"
echo "traffic is present."
echo
YNMSG="Do you want to enable the demand dial option [y/n]: "
check_yn
if [ "$YN" = "Y" ]; then
	DEMANDDIAL=180
	echo
	echo -n "Enter number of seconds for idle disconnect [$DEMANDDIAL]: "
 	read DEMANDDIAL2
	[ -z "$DEMANDDIAL2" ] || DEMANDDIAL=$DEMANDDIAL2
else
	DEMANDDIAL=NO
fi
echo
echo

YNMSG="Did your ISP assign you a static IP ADDRESS? [y/n]: "
check_yn
if [ "$YN" = "Y" ]; then
    echo -n "Enter the ISP assigned IP Address: "
    read STATICIP
    echo
    STATIC="YES"
else
    echo "Setting up for dynamic PPP Address"
    echo
    LOCALREMOTE=192.168.0.3
    echo
    echo "Set the local PPP interface IP address. Should not be the"
    echo "same as $LOCAL_IPADDR, but on the same subnet."
    echo -n "Press enter for [$LOCALREMOTE]: "
    read LOCALREMOTE2
    [ -z "$LOCALREMOTE2" ] || LOCALREMOTE=$LOCALREMOTE2
    echo
    STATIC="NO"
fi

MODEMTTY=ttyS0
echo
echo -n "Enter tty device name for modem (ttyS0, ttyS1, etc)[$MODEMTTY]: "
read MODEMTTY2
[ -z "$MODEMTTY2" ] || MODEMTTY=$MODEMTTY2

PORTSPEED=115200
echo
echo -n "Enter $MODEMTTY's port speed (115200, 57600, etc)[$PORTSPEED]: "
read PORTSPEED2
[ -z "$PORTSPEED2" ] || PORTSPEED=$PORTSPEED2

INITSTR="ATZ"
echo
echo -n "Enter modem init string (Enter = ATZ): "
read INITSTR2
[ -z "$INITSTR2" ] || INITSTR=$INITSTR2

ISPNAME="ISP"
echo
echo -n "Enter name of ISP (no whitespace)[ISP]: "
read ISPNAME2
[ -z "$ISPNAME2" ] || ISPNAME=$ISPNAME2

echo
echo -n "Enter phone number to dial: "
read PHONENUM

echo
echo -n "Enter username: "
read PPPUSERNAME

echo
echo -n "Enter password: "
read PPPPASSWORD

#----------------------------------
# Allows for login before pppd runs.
echo
echo
echo "Does your provider need you to authenticate before running PPP?"
echo "ex. if your provider supports neither chap nor pap authentication."
echo
echo "If you enable this, your password will be sent in clear text over the"
echo "line. Say yes here only if despite having verified everything, you still"
echo "cannot connect to your ISP."
YNMSG="Login during chat? [y/n]: "
check_yn
if [ "$YN" = "Y" ]; then
	CHATLOGIN="YES"
else
	CHATLOGIN="NO"
fi

#---------------------------------

# Clear the additional packages file
# echo "ppp" > floppy/packages

config_dhcp
config_dmz

echo
echo -n "Enter Domain Name: "
read DOMAINNAME

echo
echo -n "Enter DNS Server 1: "
read DNS1

echo
echo -n "Enter DNS Server 2 (optional): "
read DNS2

# Install any misc packages
. ./scripts/mkflp-misc


# Reset the modules definition file
cp data/modules-ppp pkgsrc/modules/etc/modules
# Reset the modules directory
rm -f pkgsrc/modules/lib/modules/*

for REQMODS in `cat data/modules-ppp`; do
	cp drivers/$REQMODS.o pkgsrc/modules/lib/modules/
done

echo -n "Enter the module name for your local network card: "
read CARD1
echo -n "Enter IO address (Leave blank for PCI cards): "
read IOADDR
echo -n "Enter IRQ (Leave blank for PCI cards): "
read IRQ
if [ -n "$IOADDR" ] ; then CARD1="$CARD1 io=0x$IOADDR" ; fi
if [ -n "$IRQ" ] ;    then CARD1="$CARD1 irq=$IRQ" ; fi

# Copy the needed modules into place
NETMOD1=`echo $CARD1 | cut -d " " -f 1`
# Set any module dependencies
set_deps
# Install necessary dep modules
if ! [ -z "$MODDEP1" ]; then
	if ! [ -f drivers/$MODDEP1.o ]; then
		echo "Unable to copy dep modules for card1 ($MODDEP1)"
		exit 1
	fi
	cp drivers/$MODDEP1.o pkgsrc/modules/lib/modules/
	echo "$MODDEP1" >> pkgsrc/modules/etc/modules
fi
# Install the Internet card driver
if ! [ -f drivers/$NETMOD1.o ]; then
	echo "Unable to copy driver module for card1 ($NETMOD1)"
	exit 1
fi
cp drivers/$NETMOD1.o pkgsrc/modules/lib/modules/
echo "$CARD1" >> pkgsrc/modules/etc/modules

config_dmz_card

config_language

# Create the Coyote configuration file
save_local_config
save_dmz_config
save_dhcp_config
echo "INETTYPE=PPP" >> $CONFIG_FILE
echo "DNS1=$DNS1" >>  $CONFIG_FILE
echo "DNS2=$DNS2" >>  $CONFIG_FILE
echo "DOMAINNAME=$DOMAINNAME" >>  $CONFIG_FILE
echo "IF_INET=ppp0" >> $CONFIG_FILE
echo "IF_LOCAL=eth0" >> $CONFIG_FILE
echo "PPP_ISP=$ISPNAME" >> $CONFIG_FILE
echo "PPP_CONFIG_OTF=YES" >> $CONFIG_FILE
echo "PPP_DEMANDDIAL=$DEMANDDIAL" >> $CONFIG_FILE
echo "PPP_MODEMTTY=$MODEMTTY" >> $CONFIG_FILE
echo "PPP_PORTSPEED=$PORTSPEED" >> $CONFIG_FILE
echo "PPP_INITSTR=$INITSTR" >> $CONFIG_FILE
echo "PPP_PHONENUM=$PHONENUM" >> $CONFIG_FILE
echo "PPP_USERNAME=$PPPUSERNAME" >> $CONFIG_FILE
echo "PPP_PASSWORD=$PPPPASSWORD" >> $CONFIG_FILE
echo "PPP_CHATLOGIN=$CHATLOGIN" >> $CONFIG_FILE
if [ "$STATIC" = "NO" ] ; then
        echo "PPP_STATICIP=NO" >> $CONFIG_FILE
	echo "PPP_LOCALREMOTE=$LOCALREMOTE" >> $CONFIG_FILE
else
	echo "PPP_STATICIP=$STATICIP" >> $CONFIG_FILE
fi

if ! [ -z "$LOGGING_HOST" ]; then
        echo "LOGGING_HOST=$LOGGING_HOST" >> $CONFIG_FILE
fi

buildall
write_floppy
