#!/bin/bash

[ "$WRP_CFG" ]  || . /etc/wrp.cfg
[ -d /etc/ppp ] || mkdir /etc/ppp

pppif=""

for i in "$LOCAL_INTERFACES" "$EXTERN_INTERFACES"; do
    if [ `. /etc/wrp.cfg; $i; echo \$MODEM` ]; then
	pppif="$i"
	break
    fi
done

echo -n "PPP device "
if [ -z "$pppif" ]; then
    echo "not found."
    exit
fi

$pppif
echo "$DEVICE configured on $MODEM."

# Configure /etc/ppp/options.

if [ ! -r /etc/ppp/options ]; then
    [ -z "$DEFAULT_ROUTE" ] || DEFAULT_ROUTE="defaultroute" && USE_PEER_DNS="usepeerdns"
    [ -z "$USE_PEER_DNS" ]  || USE_PEER_DNS="usepeerdns"
    [ -z "$DEMAND_DIAL" ]   || DEMAND_DIAL="demand"
    [ -z "$IDLE_TIMEOUT" ]  || IDLE_TIMEOUT="idle $IDLE_TIMEOUT"
    [ -z "$RETRY_TIMEOUT" ] || RETRY_TIMEOUT="holdoff $RETRY_TIMEOUT"
    [ -z "$INIT_CMD" ]	    || INIT_CMD="ATZ"
    [ -z "$DIAL_CMD" ]	    || DIAL_CMD="ATDT" 
    [ -z "$CHAT_SCRIPT" ]   && CHAT_SCRIPT="/etc/ppp/chat"
    cat > /etc/ppp/options <<End
0.0.0.0:
$MODEM
57600
crtscts		# h/w flow control
persist         # Don't exit after a connection is terminated; try to reconnect.
noauth		# don't require the dialin server to authenticate itself
nodetach	# don't fork off, so respawn can keep an eye on things.
$DEFAULT_ROUTE	# set default route & use dialup server's DNS
$USE_PEER_DNS	# Use the dialup server's DNS.
$RETRY_TIMEOUT	# Always wait 5 seconds before trying to reconnect.
$DEMAND_DIAL    # Dial out only on demand.
$IDLE_TIMEOUT   # Hang up after so many seconds of inactivity.
connect '/sbin/chat -v -f $CHAT_SCRIPT'
End
fi 

# Configure /etc/ppp/chat
if [ ! -r /etc/ppp/chat ]; then
    cat > /etc/ppp/chat <<End
ABORT "NO CARRIER"
ABORT "NO DIALTONE"
ABORT "ERROR"
ABORT "NO ANSWER"
ABORT "BUSY"
ABORT "Invalid"
REPORT "CONNECT"
REPORT "CARRIER"
"" "$INIT_CMD"
OK "$DIAL_CMD$POP_NUMBER"
CONNECT ""
"ogin:" "$PPP_USER"
"word:" "\q$PPP_PASSWD"
PPP ""
End
fi

ifup
