#!/bin/sh
# ============================================================================
# SPPP Subsystem Initialization Script.
# ----------------------------------------------------------------------------
# Copyright (c) 1996, Sangoma Technologies Inc.  All rights reserved.
# ============================================================================
# Rev.#	Date		Who	Details
# -----	---------------	-------	----------------------------------------------
#  2.30	Oct.16,1996	GK	Uses meta-configuration file wanpipe.rc.
#  1.00	Jun.18,1996	GK	Initial version.
# ============================================================================

CFGDIR=${WANPIPE_HOME}/cfg
SUBSYS=sppp
DEVNAME=sppp
MODNAME=sppp

####### FUNCTION DEFINITIONS #################################################

# ----------------------------------------------------------------------------
# Display error message and append it to the log file.
# ----------------------------------------------------------------------------
error() {
	echo -e "spppinit: $*!"
	[ -f "$WANPIPE_LOG" ] && echo -e "spppinit: $*!" >> $WANPIPE_LOG
	return 0
}

# ----------------------------------------------------------------------------
# Display a message and append it to the log file.
# ----------------------------------------------------------------------------
message() {
	echo -e $*
	[ -f "$WANPIPE_LOG" ] && echo -e $* >> $WANPIPE_LOG
	return 0
}

# ----------------------------------------------------------------------------
# Check to see if a value belongs to the list.
#	$1	value
#	$2...	list
# Return:
#	0 - yes
#	1 - no
# ----------------------------------------------------------------------------
check_list() {

	[ $# -lt 2 ] && return 1

	VAL=$1
	shift
	for i in $*
	do [ $VAL = $i ] && return 0
	done
	return 1
}

# ----------------------------------------------------------------------------
# Print minor device number for the specified node on the standard output.
#	$1	device
# ----------------------------------------------------------------------------
getdevminor() {
	[ -b $1 -o -c $1 ] || return 1
	ls -l $1 | while read \
		DEVPERM DEVLINKS DEVOWNER DEVGROUP DEVMAJOR DEVMINOR rest
	do
		echo $DEVMINOR
		return 0
	done
}

# ----------------------------------------------------------------------------
# Configure SPPP link.
#	$1	device node
# ----------------------------------------------------------------------------
conf_link() {

	# Get minor device number.
	DEVMINOR=`getdevminor $1`
	[ -z "$DEVMINOR" ] && {
		echo "Cannot obtain minor device number" >> $WANPIPE_LOG
		return 1
	}

	# See if firmware configuration file is present.
	FIRMCONF="${CFGDIR}/sppp.$DEVMINOR"
	[ -f "$FIRMCONF" ] || {
		echo "Firmware configuration file $FIRMCONF not found" >> $WANPIPE_LOG
		return 1
	}

	# Configure SPPP links.
	spppcfg sppp$DEVMINOR $FIRMCONF 2>> $WANPIPE_LOG || return 1

	return 0
}

# ----------------------------------------------------------------------------
# Configure data links and create interfaces.
# ----------------------------------------------------------------------------
subsys_up() {

echo -e "\n`date`: SPPP subsystem is comming up..." >> $WANPIPE_LOG

	# Configure SPPP links.
	while read NODE TYPE PORT MEM IRQ CLK SFM BIND rest
	do [ -c "/dev/$NODE" -a "$BIND" = "$DEVNAME" ] && {
		message "Configuring synchronous PPP link for /dev/$NODE... \c"
		if conf_link /dev/$NODE
		then message "O.k."
		else echo "Failed!"
		fi
	}
	done < $SDLACONF
}

# ----------------------------------------------------------------------------
# Shut down SPPP subsystem.
# ----------------------------------------------------------------------------
subsys_down() {
	echo -e "\n`date`: SPPP subsystem is going down..." >> $WANPIPE_LOG
	return 0
}

####### MAIN #################################################################
# set -x

# Create log file.
[ -f "$WANPIPE_LOG" ] || {
	echo "Sangoma WANPIPE (tm) Log File created on `date`" > $WANPIPE_LOG
}

# See if protocol module is loaded.
lsmod | grep -wq $MODNAME || {
	error "Protocol module not loaded"
	exit 1
}

# See if SDLA configuration file is present.
[ -f "$SDLACONF" ] || {
	error "WANPIPE configuration file $SDLACONF not found"
	exit 1
}

# See how we were called.
case "$1" in
	start)
		echo "Bringing up SPPP subsystem..."
		subsys_up
		;;
	stop)
		echo "Shutting down SPPP subsystem..."
        	subsys_down
		;;
	*)
		echo "Usage: $SUBSYS {start|stop}"
		exit 1
esac
exit 0

