#!/bin/sh

# This is free software, published under version 2 of the GNU General
# Public License.  You should have received a copy of the GNU General
# Public License with your Debian GNU/Linux system as
# /usr/doc/copyright/GPL.

# The author is John Hasler <jhasler@debian.org>. It is based on work by
# Richard G. Roberto and Bruce Perens.

version="pppconfig 1.1"
# set -x
umask 027
BACKTITLE="Debian GNU/Linux PPP Configuration Utility"
MINMEN=0

abortstring='ABORT BUSY
ABORT "NO CARRIER"
ABORT VOICE
ABORT "NO DIALTONE" 
ABORT "NO ANSWER"'

# Set defaults.
atdx=ATDT
route=defaultroute
ipdefault=noipdefault
modeminit=ATZ

ispport=/dev/ttyS1
ispspeed=115200
route=defaultroute
ipdefault=noipdefault
ispname=replace_with_your_username
ispauth=pap
modeminit=ATZ
ispnumber=replace_with_number
ispconnect=\'\'
isplogin="ogin:"
ispname=replace_with_name
ispprompt="ssword:"
isppwd=replace_with_password
ispothers=''

Exit () {
    local result=$1
    rm -rf ${TempFile}* $SedFile
    clear
    exit ${result:-0}
}

# include the user interface functions

# Global options 
# The variable $BACKTITLE specifies the back title.  
# The variable $DIALOG_OPTIONS, initialized here to --clear, provides 
# options you want on the command line of each dialog invocation.  
# The variable $DIALOG_TEST can be set to "echo" to see the calls 
# to dialog without executing them.

DIALOG_OPTIONS=""

# Make any dialogue box, with default settings and backtitle from
# $BACKTITLE in the environment.
#
# dialog --type arg arg ...
#
dialogBox () {
    local type="$1"
    shift
    local title=""
    local backtitle=""
    local text="$1"
    shift
    if [ $# -ge 1 ]
    then
        title="$1"
	shift
    fi
    if [ -n "$BACKTITLE" ]; then
	backtitle="$BACKTITLE"
    fi
    $DIALOG_TEST $UI $DIALOG_OPTIONS --title "$title" --backtitle \
    "$backtitle" "$type" "$text" 22 80 "$@" 2>&1 1>/dev/tty
    local result=$?
    return $result
}

# Display a file.
#
# fileBox filename [title]
#
fileBox () {
    dialogBox --textbox "$1" "$2"
}

# textBox takes presents its standard input in a dialog box. This
# is useful for "here documents" and pipes.
#
# textBox [title]
#
textBox () {
    cat > $TempFile
    if [ $? -ne 0 ]
    then
        echo "Can't make temporary file for dialog box." 1>&2
	return 255
    fi
    # Note that dialog needs stdin to be the terminal, so I redirect here.
    < /dev/tty dialogBox --textbox $TempFile "$1"
    local result=$?
    rm -f $TempFile
    return $result
}

msgBox () {
    dialogBox --msgbox "$1" "$2"
}

infoBox () {
    dialogBox --infobox "$1" "$2"
}

yesNoBox () {
    dialogBox --yesno "$1" "$2"
}

inputBox () {
    dialogBox --inputbox "$1" "$2" "$3"
}

# menu text title tag1 item1 ...
menu () {
    local text="$1"
    shift
    local title="$1"
    shift
    dialogBox --menu "$text" "$title" 0 "$@"
}

# menu text title tag1 item1 status1 ...
checklist () {
    local text="$1"
    shift
    local title="$1"
    shift
    dialogBox --checklist "$text" "$title" 0 "$@"
}

# menu text title tag1 item1 status1 ...
radiolist () {
    local text="$1"
    shift
    local title="$1"
    shift
    dialogBox --radiolist "$text" "$title" 2 "$@"
}
# end of shell interface to "dialog"


mkMenu ()
{
while true
    do
    # These local variables were used in the dinstall script I hijacked
    # this user interface concept from, so I use them as well.
    local next_action=true
    local previous_action=true
    local alternate_action=true
    echo "menu \\" > $TempFile
    [ $# -lt 1 ] && { # do this if mkMenu is called with no args
	# This section sets up the inital menu at first invocation. It will
	# usually just get skipped.
	next_action=newConnection
	cat >> $TempFile << EOF
"This is the PPP configuration utility.  It does not make a connection to
your ISP: it just configures your system so that you can connect with a
utility such as 'pon'. You will be asked for the username, password, and
phone number that your ISP gave you.  If your ISP uses PAP or CHAP, that is
all you need.  If you need to use a chat script to connect, you will need
to know how your ISP prompts for your username and password.  If you don't
know what your ISP uses, try PAP.

Use the up and down arrow keys to move around the menus.  Use the TAB key
to move from the menu to <OK> to <CANCEL> and back.  When you are ready to
move on to the next menu go to <OK> and hit enter.  To go back to the main
menu go to <CANCEL> and hit enter." \
"Main Menu" \\
	"Next"  "Create a connection" \\
EOF
    } # end of argumentless specifics	

    #
    # These are specific menu fragments called by the $1 arg to mkMenu
    #
    case $1 in 
	method) # menu of connection methods -- only uses
		# menu tail
	    cat >> $TempFile <<EOF
"Please select the authentication method for this connection.  PAP is the
method most often used in Windows 95, so if your ISP supports the NT or
Win95 dial-up client, try \\"PAP\\"." \
"$title" \\
	"PAP"	"Password Authentication Protocol" \\
	"Chat"	"Use \\"chat\\" for login:/password: authentication" \\
	"CHAP"	"Challenge Handshake Authentication Protocol" \\
	" " " "	\\
EOF
	    previous_action="break 2"
	    ;;
#
# end of method block
#
	properties)     # list of variables about a connection
                        # to be modified by the user
	    cat >> $TempFile <<EOF
"Please select the property you wish to modify, select \\"Cancel\\" to go
back to start over, or select \\"Finished\\" to write out the changed
files." \
"$title" \\
EOF
	    cat >> $TempFile <<EOF
	"Modeminit"     "Modem initialization string" \\
        "Number"        "Telephone number to call" \\
        "User"          "User name for authentication" \\
EOF
	    cat >> $TempFile <<EOF
        "Password"      "The password for this connection" \\
EOF
	    cat >> $TempFile <<EOF
        "Speed"         "Port speed for the modem" \\
        "Com"           "Serial Port the modem is on" \\
	"Defaultroute"  "Enable or disable default route" \\
	"Ipdefault"     "Set IP addresses" \\
        "Method"        "Authentication Method (e.g., PAP)" \\
EOF
	    [ $ispauth = "chat" ] && {
		cat >> $TempFile <<EOF
        "ISPChat"       "Manage Chat Settings" \\
EOF
	    }
	    cat >> $TempFile <<EOF
        " " " " \\
        "Finished"      "Write files and return to main menu." \\
EOF
	    previous_action="break 2" 
	;;
#
# end of properties block
#

        chatprops)      # configure the individual chat properties
	    cat >> $TempFile << EOF
"You will now have the opportunity to change your chat settings. Select the
setting you wish to change, and select \\"Previous\\" when you are done
changing chat settings." \
"Chat Settings" \\
        "ISPConnect"    "Change the Connect strings" \\
        "ISPLogin"      "Change the Login Prompt string" \\
        "ISPPrompt"     "Change the Password Prompt string" \\
        "ISPOthers"     "Change or add other strings" \\
        "Previous"      "Return to previous menu" \\
        " " " " \\
EOF
	    previous_action="break 2"
        ;;
#
# end of chatprops block
#
    esac

	
#
# START OF STANDARD MENU FRAGMENT
# this is included unless MINMEN=1
#
    [ $MINMEN -ne 1 ] && {
	cat >> $TempFile << EOF
	" " " " \\
	"Create" "Create a connection" \\
EOF
    }
#
# END OF STANDARD MENU FRAGMENT
#

#
# START OF MENU TAIL
# this is always present
#

    cat >> $TempFile << EOF
	"Quit" "Exit this utility"
EOF
#
# END OF MENU TAIL
#
#cat $TempFile
# START OF STANDARD CASE PROCESSING 
local action 
    action="`. $TempFile`"
    result=$?
    if [ $result -ne 0 ]
	then 
	    mkMenu 
	fi
    if [ -z "$action" ]
	then 
	    continue
	fi 
    case $action in
	Next) 
	    $next_action
	    return 
	;;
	Alternate) 
	    $alternate_action
	    return 
	;;
	Previous|Finished) 
	    $previous_action
	    return
	;;
	Create) newConnection
	    return
	;;
	PAP) 
	    doOps PAP
	    return
	;;
	CHAP) 
	    doOps CHAP
	    return
	;;
	Chat) 
	    doOps Chat
	    return
	;;
	Method) 
	    mkMenu method
	    return
	;;
	ISPChat) 
	    chatProps
	    return
	;;
	ISPConnect) 
	    chatConfig connect
	    return
	;;
	ISPLogin) 
	    chatConfig login
	    return
	;;
	ISPPrompt) 
	    chatConfig prompt
	    return
	;;
	ISPOthers) 
	    chatConfig others
	    return
	;;
	Password) 
	    doOps Passwd
	    return
	;;
	Com) 
	    doOps Port
	    return
	;;
	Defaultroute) 
	    doOps Defaultroute
	    return
	;;
	Ipdefault) 
	    doOps Ipdefault
	    return
	;;
	Speed) 
	    doOps Speed
	    return
	;;
	User) 
	    doOps User
	    return
	;;
	Number) 
	    doOps Number
	    return
	;;
	Modeminit) 
	    doOps Modeminit
	    return
	;;
	Quit) Exit
	;;
    esac
#
END OF STANDARD CASE PROCESSING
#
    done	# end of while loop
}

# Action functions for PPPCONFIG


chatConfig () {

case "$1" in
    connect)	# the connection string sent by the modem
	ispconnect="`inputBox \
"Enter the text of connect acknowledgement, if any. This string will be
sent when the CONNECT string is received from the modem.  Unless you know
for sure that your ISP requires such an acknowledgement you should leave
this as a null string: that is, ''.  

A few ISP's require some sort of handshake before the actual login.  For
example, the ISP might send host: when you connect and expect you to
respond with ppp before proceeding with login.  To do this you would just
add host: and ppp to the ack string so that it would read '' host: ppp.
This would cause your system to expect CONNECT, send a return, expect
host:, and send ppp." \
"Ack String" "${ispconnect:=''}"`"
	if [ $? -ne 0 ]
	then
	    mkMenu
	fi
	return
	;;
    login)	# the login prompt string sent by the ISP
	isplogin="`inputBox \
"Enter the text of the login prompt.  Chat will send your username in
response.  The most common prompts are 'login:' and 'username:'. Sometimes
the first letter is capitalized and so we leave it off and match the rest of
the word. Sometimes the colon is omitted." \
"Login String" "$isplogin"`"
	if [ $? -ne 0 ]
	then
	    mkMenu
	fi
	return
	;;
    prompt)	# password prompt sent by the ISP
	ispprompt="`inputBox \
"Enter the text of the password prompt.  Chat will send your password in
response. The most common prompt is 'password:'. Sometimes the first letter
is capitalized and so we leave it off and match the rest of the
word. " \
"PWD String" "$ispprompt"`"
	if [ $? -ne 0 ]
	then
	    mkMenu
	fi
	return
	;;
    others)	# other possible chat requirements
	ispothers="`inputBox \
"You probably do not need to put anything here. Enter any additional input
your ISP requires.  This may be a program name like 'ppp' as a response to
a menu prompt. If you need to make an entry, make the first entry the
prompt you expect and the second the required response.  All entries must
be separated by white space.  Anything inside single quotes will be treated
as one word even if it contains white space." \
"Other Strings" "$ispothers"`"
	if [ $? -ne 0 ]
	then
	    mkMenu
	fi
	return
	;;
*)	# chatConfig with no options will display a message and configure
	# all chat options
	msgBox \
"Now you will be asked to configure your chat script. This is the series of
queries and responses used by your ISP to authenticate you. You will need to
know how your ISP prompts for your username and password." \
"Chat"
	if [ $? -ne 0 ]
	then
	    mkMenu
	fi
	chatConfig connect
	chatConfig login
	chatConfig prompt
	chatConfig others
	return
	;;
    esac
}

GetName () {
provider="`inputBox \
"Enter the name you wish to use to refer to this ISP.  You will probably
want to give the default name of 'provider' to your primary ISP.  That way,
you can dial it by just giving the command 'pon'.  Give each additional isp
a unique name.  For example, you might call your employer 'theoffice' and
your university 'theschool'.  Then you can connect to your ISP with 'pon',
your office with 'pon theoffice', and your university with 'pon theschool'." \
"Provider Name" "$provider"`"
	if [ $? -ne 0 ]
	then
	    mkMenu
	fi
	return
}


chatProps () {
# loop through the chatprops menu display until user
# chooses previous_action and does a "break 2"
    noswitch=0
    [ $MINMEN = 1 ] && noswitch=1
    [ $noswitch -eq 0 ] && MINMEN=1
    while true
    do
	mkMenu chatprops
    done
    [ $noswitch -eq 0 ] && MINMEN=0
    return
}

Previous ()
{
    return
}

finishConnection () {
# this is to tidy up things with the user and is in no way
# associated with any Finnish Connections ;)

    msgBox \
"Finished configuring connection and writing changed files." "Finished"
    doFiles put
}

newConnection () {
# this sets up new connections by calling other functions to:
# - initialize a clean state by zeroing state variables
# - query the user for information about a connection

    MINMEN=1

    title="Create Connection"
    Nameservers GET
    GetName             # Get the name of this provider.
    CHATFILE=$ETC/chatscripts/$provider
    OPTIONFILE=$ETC/ppp/peers/$provider
    doFiles get         # Get variables now that we know the name
    mkMenu method 	# calls doOps with appropriate method arg
			# this starts the process of collecting all other
			# connection related information
    doOps	        # calls all doOps args required
    doProps		# review properties
    MINMEN=1
    finishConnection	# finishing touches
    return
}

doProps () {
    msgBox \
"You will now have the chance to review or change connection properties." \
"Properties"
    if [ $? -ne 0 ]
    then 
        mkMenu
    fi
    while true
    do
	title="Select A Property To Manage"
  mkMenu properties	# calls doOps for each property to modify
    done
}

doFiles () {
    case $1 in
    get)
	if grep -qs "This file was generated by pppconfig" "$OPTIONFILE"
	# We put options in this file last time.
	then
	# Read it.
	   temp=`grep -s pppconfig_dev $OPTIONFILE | cut -f 1 -d ' ' -`
	   ispport=${temp:-$ispport}
	   temp=`grep -s pppconfig_speed $OPTIONFILE | cut -f 1 -d ' ' -`
	   ispspeed=${temp:-$ispspeed}
	   temp=`grep -s pppconfig_route $OPTIONFILE | cut -f 1 -d ' ' -`
	   route=${temp:-$route}
	   temp=`grep -s pppconfig_ipdefault $OPTIONFILE | cut -f 1 -d ' ' -`
	   ipdefault=${temp:-$ipdefault}
	   temp=`grep -s pppconfig_user $OPTIONFILE | cut -f 2 -d ' ' -`
	   ispname=${temp:-$ispname}
	fi
	if [ -f "${PPPCONFIG_DIR}/$provider" ]
	# We saved some chat stuff last time.
	then
	    # Read it.
	    read ispauth < "${PPPCONFIG_DIR}/$provider"
	    if [ "$ispauth" = chat ]
	    then
	        { read ispauth
	        read modeminit
	        read atdx
	        read ispnumber
	        read ispconnect
	        read isplogin
	        read ispname
	        read ispprompt
	        read isppwd
	        read ispothers; } < "${PPPCONFIG_DIR}/$provider"
	    else
		{ read ispauth
		read modeminit
		read atdx
 		read ispnumber; } < "${PPPCONFIG_DIR}/$provider"
	    fi
	    atdx=`echo $atdx | tr a-z A-Z`
	fi
	;;
    put)
	if grep -qs "This file was generated by pppconfig" "$OPTIONFILE"
	# Evidently we've been here before.
	then
	    # Edit the file.
	    sed -e "/#pppconfig_debug/c\\
debug          #pppconfig_debug
/#pppconfig_dev/c\\
$ispport       #pppconfig_dev
/#pppconfig_speed/c\\
$ispspeed      #pppconfig_speed
/#pppconfig_route/c\\
$route         #pppconfig_route
/#pppconfig_ipdefault/c\\
$ipdefault      #pppconfig_ipdefault
/#pppconfig_user/c\\
user $ispname #pppconfig_user
" $OPTIONFILE > $SedFile
cat $SedFile > $OPTIONFILE
 
       else
	    if [ -s $OPTIONFILE ]
	    # There is something here, but none of it is ours.
  	    then
		# Move it to a safe place.
		mv $OPTIONFILE "${OPTIONFILE}.old"
	    fi
	    # And create a new file.
	    touch $OPTIONFILE
	    chgrp dip $OPTIONFILE
	    cat >"$OPTIONFILE" <<EOF
# This file was generated by pppconfig.  You can edit the following lines
# but please do not delete lines or the change the comments or you will
# confuse pppconfig.
noauth         #pppconfig_noauth
connect "/usr/sbin/chat -v -f $CHATFILE"  #pppconfig_connect
debug          #pppconfig_debug     
$ispport       #pppconfig_dev
$ispspeed      #pppconfig_speed
$route         #pppconfig_route
$ipdefault     #pppconfig_ipdefault
user $ispname  #pppconfig_user
# End of pppconfig controlled lines.  You can add lines below here without
# confusing pppconfig.
EOF
	fi
	
	# If there is no CHATFILE, create it.
	if [ ! -f $CHATFILE ]
	then
	    touch $CHATFILE
	    chgrp dip $CHATFILE
        fi

	# If there is no pppconfig/provider, create it.
	if [ ! -f "${PPPCONFIG_DIR}/$provider" ]
	then
	    touch "${PPPCONFIG_DIR}/$provider"
	    chmod 600 "${PPPCONFIG_DIR}/$provider"
        fi

	# If pppconfig/provider is older than CHATFILE and CHATFILE is not empty
	# move CHATFILE to CHATFILE.old and create a new CHATFILE.
	if [ \( $CHATFILE -nt "${PPPCONFIG_DIR}/$provider" \) -a -s $CHATFILE ]
	then
	    mv $CHATFILE ${CHATFILE}.old
	    touch $CHATFILE
	    chgrp dip $CHATFILE
	fi

	if  [ $ispauth = "chat" ]
	then
	    cat > "$CHATFILE" <<EOF
$abortstring
"" $modeminit
OK $atdx$ispnumber
CONNECT $ispconnect
$isplogin \\d\\q$ispname
$ispprompt \\q$isppwd
$ispothers
"" \\d\\c
EOF
	cat > "${PPPCONFIG_DIR}/$provider" <<EOF
$ispauth
$modeminit
$atdx
$ispnumber
$ispconnect
$isplogin
$ispname
$ispprompt
$isppwd
$ispothers
EOF

	else
	    cat > $CHATFILE <<EOF
$abortstring
"" $modeminit
OK $atdx$ispnumber
CONNECT \\d\\c
EOF
	    cat > "${PPPCONFIG_DIR}/$provider" <<EOF
$ispauth
$modeminit
$atdx
$ispnumber
EOF
	    secretsFile
	fi

	Nameservers PUT
	return

# End of "put".
	;;
    *)
	return
	;;
    esac
}

secretsFile () {
# This writes the secrets to pap-secrets or chap-secrets.
    if grep -qs "pppconfig for $provider" "${SECRETS_DIR}/${ispauth}-secrets"
	then
sed -e "/pppconfig for $provider/{
n
c\\
$ispname * $isppwd
}
" "${SECRETS_DIR}/${ispauth}-secrets" > $SedFile
cat $SedFile > ${SECRETS_DIR}/${ispauth}-secrets

    else
	echo "
# The next line was added by pppconfig for $provider.">>${SECRETS_DIR}/${ispauth}-secrets
	echo "$ispname * $isppwd">>"${SECRETS_DIR}/${ispauth}-secrets"
    fi
    return
}


doOps () {
    # this function uses the following global variables:
    # ispname - ISP user name for each connection (reset in manage function)
    # ispspeed - connection port speed (reset in Manage)
    # ispport - modem port (reset in Manage)
    # ispnumber - telephone number to call
    # modeminit - modem initialization string
    # atdx - ATDT or ATDP
    # route - defaultroute or -defaultroute
    # ipdefault - noipdefault or an ip number
    # isppwd - password to store
    # ispauth - pap, chap, or chat

    title="Manage $1 Configuration"
    case $1 in
	User) # input the user name for the ppp account
	    ispname="`inputBox \
"Backspace over the placeholder string and enter the username given to you
by your ISP. If your username includes any space or punctuation marks
enclose it in single quotes like this: 'a !@# weird name' " \
"$title" "${ispname:-replace_with_name}"`"
	    if [ $? -ne 0 ]
	    then 
		mkMenu
	    fi
	    return
	;;
    Port) # identify the port the modem is on
	ispport="`inputBox \
"Enter the port your modem is on.
/dev/ttyS0 is COM1 in DOS
/dev/ttyS1 is COM2 in DOS
/dev/ttyS2 is COM3 in DOS
/dev/ttyS3 is COM4 in DOS
/dev/ttyS1 is the most common.  Note that this must be typed exactly as
shown.  Capitalization is important: ttyS1 is not the same as ttys1." \
"$title" "${ispport:-"/dev/ttyS1"}"`"
	if [ $? -ne 0 ]
	then
	    mkMenu
	fi
	return
	;;
    Defaultroute)
	[ $route = "defaultroute" ] ; nodefaultroute=$?
	[ $route = "-defaultroute" ] ; ddefaultroute=$?
	route=`radiolist \
"Enabling default routing tells your system that the way to reach hosts to
which it is not directly connected is via your ISP. This is almost certainly
what you want.  Use the up and down arrow keys to move among the selections,
and press  the spacebar to select one.  When you are finished, use TAB to
select <OK> and ENTER to move on to the next item." \
"Default Route" \
"defaultroute" "Enable default route" $ddefaultroute \
\"-defaultroute\" "Disable default route" $nodefaultroute \
`
	if [ $? -ne 0 ]
	then
	    mkMenu
	fi
	if [ route = \"-defaultroute\" ]
	then
	    route=-defaultroute
	fi
	return
	;;
    Ipdefault)
	ipdefault="`inputBox \
"You almost certainly do not want to change this from the default value of
noipdefault.  This not the place for your nameserver ip numbers.  It is the
place for your ip number if and only if your ISP has assigned you a static
one.  If you have been given only a local static ip, enter it with a colon
at the end, like this: 192.168.1.2: . If you have been given both a local
and a remote ip, enter the local ip, a colon, and the remote ip, like this:
192.168.1.2:10.203.1.2 ." \
"Ip numbers" "$ipdefault"`"
	if [ $? -ne 0 ]
	then
	    mkMenu
	fi
	return
	;;
    Speed) # get the port speed
	ispspeed="`inputBox \
"Enter your modem port speed (e.g. 9600, 19200, 38400, 115200).  I
suggest that you leave it at 115200." \
"Set Speed" "${ispspeed:-115200}"`"
	if [ $? -ne 0 ]
	then
	    mkMenu
	fi
	return
	;;
    Modeminit)
	modeminit="`inputBox \
"Enter modem intialization string.  The default value is ATZ, which tells
the modem to use its default settings.  As most modems are shipped from
the factory with default settings that are appropriate for ppp, I suggest
you not change this." \
"Modem Intialization" "$modeminit"`"
	if [ $? -ne 0 ]
	then
	    mkMenu
	fi
	return
	;;
    Number) # get the number to call or DIRECT for a direct connection
	[ $atdx = ATDT ] ; pulse=$?
	[ $atdx = ATDP ] ; tone=$?
	ispnumber="`inputBox \
"Backspace over the placeholder string and enter the number to dial.  Don\'t
insert any dashes or spaces.  See your modem manual if you need to do
anything unusual like dialing through a PBX." \
"Number to dial" \
"${ispnumber:-replace_with_number}"`"
	if [ $? -ne 0 ]
	then
	    mkMenu
	fi
	atdx=`radiolist \
"Select method of dialing. Since almost everyone has touch-tone, you should
leave the dialing method set to tone unless you are sure you need
pulse. Use the up and down arrow keys to move among the selections, and
press the spacebar to select one. When you are finished, use TAB to
select <OK> and ENTER to move on to the next item." \
"Tone or Pulse" \
"ATDT" "tone" $tone \
"ATDP" "pulse" $pulse \
` 
	if [ $? -ne 0 ]
	then
	    mkMenu
	fi
	return
	;;
    Passwd) 
	isppwd="`inputBox \
"Backspace over the placeholder string and enter the password your ISP gave
you.  If your password includes any space or punctuation marks enclose it
in single quotes like this: 'a !@# weird password'." \
"Enter Password" \
"${isppwd:-replace_with_password}"`"
	if [ $? -ne 0 ]
	then
	    mkMenu
	fi
	return
	;;
    PAP) # PAP specific section
	ispauth="pap"
	doOps Passwd
	;;
    CHAP) # CHAP specific section
	ispauth="chap"
	doOps Passwd
	;;
    Chat) # Builds a chat file
	ispauth="chat"
	doOps Passwd
	chatConfig
	;;
    *)    # Does do everything but auth,passwd
	msgBox \
"Now you will configure some basic connection properties." \
"Configuration"
	if [ $? -ne 0 ]
	then
	    mkMenu
	fi
	doOps User
	doOps Port
	doOps Defaultroute
	doOps Ipdefault
	doOps Speed
	doOps Modeminit
	doOps Number
	return
	;;
    esac
    return
}

# Get the nameservers, if resolv.conf doesn't have any.
# RESOLV_CONF is initialized to "OK".
Nameservers () {
    case $1 in
  CHECK) # Check out resolv.conf.  Does it exist?  Are there nameservers in it?
	if [ ! -f $ETC/resolv.conf ]
        then
	    RESOLV_CONF=NONE
	elif ! grep -q -s ^nameserver $ETC/resolv.conf
	then
	    RESOLV_CONF=EMPTY
	fi
	return
        ;;
    GET) # Get some nameservers from the user.
	if [ $RESOLV_CONF = "OK" ] 
	then 
	    return 
	fi 
	if yesNoBox \
"There are no nameservers (DNS servers) listed in your resolv.conf file.
You almost certainly need at least one.  Your ISP should have given you the
number(s) for his.  Do you want to install them now?"
	then
	    NAMESERVER1="`inputBox \
"Enter the IP number for your primary nameserver." \
"IP number" ""`"
	    if [ $? -ne 0 ]
	    then
		mkMenu
	    fi

	    NAMESERVER2="`inputBox \
"Enter the IP number for your secondary nameserver (if any)." \
"IP number" ""`"
	    if [ $? -ne 0 ]
	    then
		mkMenu
	    fi
	fi
	if [ ! $NAMESERVER1 ] && [ ! $NAMESERVER2 ]
	then
	    # If she didn't enter anything, she doesn't want us to mess with
	    # resolv.conf.
	    RESOLV_CONF="OK"
	fi
	return
        ;;
	
    PUT) 
	# Fix up resolv.conf if needed.
	case $RESOLV_CONF in
	OK) # Leave it alone.
	    return
	    ;;
      NONE) # It doesn't exist: create it.
	    touch $ETC/resolv.conf
	    chmod a+r $ETC/resolv.conf
	    ;;
	esac
	# Put some nameservers in it (but not if they are null).
	if [ $NAMESERVER1 ]
	then
	    echo "nameserver $NAMESERVER1" >> $ETC/resolv.conf
	fi
	if [ $NAMESERVER2 ]
	then
	    echo "nameserver $NAMESERVER2" >> $ETC/resolv.conf
	fi
	RESOLV_CONF="OK"
	return
	;;
    esac
}

usage () {
    echo "$version"
    echo "Usage: pppconfig [--version] | [--help]
'--version' prints the version. '--help' print a help message."
    exit $1
}

help () {
    echo "
pppconfig is an interactive, menu driven utility to help automate setting
up a dial-up ppp connection.  It currently supports PAP, CHAP, and chat
authentication.  It uses the standard pppd configuration files.  It does
not make a connection to your ISP, it just configures your system so that
you can do so with a utility such as 'pon'.

Before running pppconfig you should know what sort of authentication your
isp requires, the username and password that they want you to use, and
the phone number.  If they require you to use chat authentication, you will
also need to know the login and password prompts and any other prompts and
responses required for login.  If you can't get this information from your
ISP you could try dialing in with 'minicom' and working through the procedure
until you get the garbage that indicates that ppp has started on the other
end.

Since pppconfig makes changes in system configuration files, you must be
logged in as root or use 'sudo' to run it.
"
    usage 0
}

UI=whiptail
provider=provider
ETC=/etc
CHATFILE=$ETC/chatscripts/$provider
OPTIONFILE=$ETC/ppp/peers/$provider
SECRETS_DIR=$ETC/ppp
PPPCONFIG_DIR=/var/lib/pppconfig
RESOLV_CONF=OK

if [ $# -eq 1 ]
then
    case $1 in
	--version)
	    echo $version
	    exit 0
	;;
	--help)
	    help
	;;
	dialog)
	    UI=dialog
	    DIALOG_OPTIONS=""
	;;
	debug)
	    set -x
	;;
	*)
#	    provider=$1
	    usage 1
	;;
    esac
fi

if [ $# -gt 1 ]
then
    usage 1
fi

if [ `id -u` -ne 0 ]
then
    echo "You must be root to run this program."
    exit 1
fi

if [ ! -d $ETC/chatscripts ]
then
    echo "$ETC/chatscripts does not exist."
    exit 1
fi

if [ ! -d $ETC/ppp/peers ]
then
    echo "$ETC/ppp/peers does not exist."
    exit 1
fi

if [ ! -d /var/lib/pppconfig ]
then
    # Create a directory to store chat config files in.
    mkdir /var/lib/pppconfig
    chmod 700 /var/lib/pppconfig
fi

readonly TempFile=`mktemp -q /tmp/pppconfig.XXXXXXXX`
if [ $? -ne 0 ]
    then
    echo "$0: Can't create temp file, exiting."
    exit 1
fi

readonly SedFile=`mktemp -q /tmp/sedfile.XXXXXXXX`
if [ $? -ne 0 ]
    then
    echo "$0: Can't create temp file, exiting."
    exit 1
fi

chmod 0600 $SedFile $TempFile

Nameservers CHECK

clear
while true 
do
    mkMenu
done
