#!/bin/sh
# start.dip: the main control script for talking SLIP or PPP to DIS.
# Copyright 1994-6 John A. Phillips - john@linux.demon.co.uk
# usage: start.dip [slip|ppp]

# Initialization.
# ===============

# Assign the protocol if specified, or use the default.
protocol=${1:-slip}

# Check for a valid protocol.
if [ $protocol != "ppp" -a $protocol != "slip" ]; then
    echo "usage: start.dip [ppp|slip]"
    exit 1
fi

# If the script is interrupted, kill dip and exit.
trap '/sbin/dip -k 2>/dev/null; echo ""; exit 1' \
    SIGHUP SIGINT SIGQUIT SIGTERM

# Start up the main dialup IP connection.
# =======================================

# Use this section for SLIP.
# --------------------------
if [ $protocol = "slip" ]; then
    /sbin/dip /usr/local/etc/demon_slip

# Did dip succeed?
    dip_result=$?
    if [ $dip_result -ne 0 ]; then
        echo dip failed with exit code $dip_result
        exit 1
    fi

# SLIP sometimes seems to need time to settle.
    sleep 5

# Connected.
    echo "SLIP connected."
fi

# Use this section for PPP.
# -------------------------
if [ $protocol = "ppp" ]; then
    /sbin/dip /usr/local/etc/demon_ppp

# Did dip succeed?
    dip_result=$?
    if [ $dip_result -ne 0 ]; then
        echo dip failed with exit code $dip_result
        exit 1
    fi

# Wait for the signal that IP has come up via PPP.
    echo "Waiting for PPP to come up ..."
    wait_time=21
    trap "break" SIGCONT
    while [ $wait_time -gt 0 ]; do
        sleep 1
        wait_time=`expr $wait_time - 1`
        echo -n -e "\\r$wait_time "
    done
    trap SIGCONT
    echo ""

# Did a signal arrive, or was there a time out?
    if [ $wait_time -le 0 ]; then
        echo "PPP timed out."
        /bin/killall pppd 2>/dev/null
        /sbin/dip -k 2>/dev/null
        exit 2
    fi

# Connected.
    echo "PPP connected."
fi

# Start up IP applications.
# =========================

# Check to see whether we have any waiting mail.
/usr/local/sbin/querypost &

# Set the system and CMOS date and time from the network.
/usr/local/sbin/setclock &

# Run the mail queue.
/usr/sbin/sendmail -q &

# Now process incoming and outgoing news.
# /usr/local/sbin/procnews &

# Print out Demon status information.
/usr/local/sbin/querystatus &

# Start up any auto-logout procedures (if you have them).
# =======================================================

# if [ $protocol = "slip" ]; then
#     /usr/local/sbin/slip_idle
# fi
# if [ $protocol = "ppp" ]; then
#     /usr/local/sbin/pppd_idle
# fi
