#!/bin/sh
#
# Initialize the modem including enabling PPP and auto-answer

. /etc/defaults/rc.conf
source_rc_confs

POTENTIAL_MODEMS="cuaa2 cual0"

# Enable dialin on a specified device
enable_dialin()
{
	sh /root/bin/enable_dialin.sh $1 >/dev/null 2>&1
	return $?
}

# Determine the modem device and create a /dev/modem symlink
setup_link()
{
	local modem

	for modem in $POTENTIAL_MODEMS; do
		if ! [ -r /dev/${modem} ]; then
			continue
		fi
		if enable_dialin /dev/${modem}; then
			( cd /dev ; ln -s ${modem} modem )
			echo -n ' /dev/modem'
			return
		fi
	done

	logger -t 'setup_modem' "Could not determine the modem device"
}

# Only for startup.
case $1 in
start)
	;;
*)
	exit 0
	;;
esac

# If we already have a modem symlink, try to enable auto-answer.  If that
# fails remove the symlink.
if [ -r /dev/modem ]; then
	if enable_dialin /dev/modem; then
		echo -n ' dial-in'
	else
		rm /dev/modem
	fi
fi

# If we don't already have the symlink or we just removed it, try to create
# the symlink and enable auto-answer in the process.
if ! [ -r /dev/modem ]; then
	setup_link
fi

# If we don't have a symlink by now, there is no working modem installed,
# so give up and don't try to setup PPP.
if ! [ -r /dev/modem ]; then
	exit 0
fi

case ${twc_ppp_enable} in
[Yy][Ee][Ss])
	# Establish ppp mode.
	#
	/usr/sbin/ppp -quiet -auto backchannel && echo -n ' ppp backchannel'
	;;
esac
