#!/bin/sh
# ============================================================================
# FLIP 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.
#  2.10	Jun.18,1996	GK	
#  1.00	Apr.23,1996	GK	Initial version.
# ============================================================================

CFGDIR=${WANPIPE_HOME}/cfg
SUBSYS=flip
DEVNAME=flip
MODNAME=flip
CONFFILE=/etc/flip.conf

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

# ----------------------------------------------------------------------------
# Display error message and append it to the log file.
# ----------------------------------------------------------------------------
error() {
	echo -e "flipinit: $*!"
	[ -f "$WANPIPE_LOG" ] && echo -e "flipinit: $*!" >> $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 FLIP interface.
#	$1	interface number
#	$2	device node
#	$3	DLCI
# ----------------------------------------------------------------------------
conf_interface() {

	[ $# -lt 3 ] && {
		echo "Too few arguments!" >> $WANPIPE_LOG
		return 1
	}

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

	# Create new interface.
	flipcfg a $1 $DEVMINOR $3 2>> $WANPIPE_LOG || return 1

	return 0
}

# ----------------------------------------------------------------------------
# Configure FLIP 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}/flip.$DEVMINOR"
	[ -f "$FIRMCONF" ] || {
		echo "Firmware configuration file $FIRMCONF not found" >> $WANPIPE_LOG
		return 1
	}

	# Configure FLIP links.
	flipcfg c $DEVMINOR $FIRMCONF 2>> $WANPIPE_LOG || return 1

	return 0
}

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

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

	# Configure FLIP links.
	LINKNUM=0
	NODELIST=
	while read NODE TYPE PORT MEM IRQ CLK SFM BIND rest
	do [ -c "/dev/$NODE" -a "$BIND" = "$DEVNAME" ] && {
		message "Configuring frame relay link for SDLA /dev/$NODE... \c"
		if conf_link /dev/$NODE
		then
			message "O.k."
			LINKNUM=`expr $LINKNUM + 1`
			NODELIST="$NODELIST $NODE"
		else echo "Failed!"
		fi
	}
	done < $SDLACONF

	# If no links were configured, exit.
	[ "$LINKNUM" -eq 0 ] && return 0

	# Create FLIP interfaces.
	echo -n "Creating FLIP interfaces:"
	echo -e "Interfc\tSDLA\tDLCI\tResult" >> $WANPIPE_LOG
	echo -e "-------\t----\t----\t------" >> $WANPIPE_LOG
	while read NODE DLCI IFNO rest
	do [ -c /dev/$NODE ] && check_list $NODE $NODELIST && {
		echo -ne "${DEVNAME}${IFNO}\t${NODE}\t${DLCI}\t" >> $WANPIPE_LOG
		if conf_interface $IFNO $NODE $DLCI
		then
			echo "O.k." >> $WANPIPE_LOG
			echo -n " flip$IFNO"
		else echo -n "?"
		fi
	}
	done < $CONFFILE
	echo ""
}

# ----------------------------------------------------------------------------
# Shut down FLIP subsystem.
# ----------------------------------------------------------------------------
subsys_down() {

	echo -e "\n`date`: FLIP subsystem is going down..." >> $WANPIPE_LOG

	while read NODE TYPE PORT MEM IRQ CLK SFM BIND rest
	do [ -c "/dev/$NODE" -a "$BIND" = "$DEVNAME" ] && {
		message "Shutting down frame relay link for SDLA /dev/$NODE ..."

		# Get minor device number.
		DEVMINOR=`getdevminor /dev/$NODE`
		if [ -z "$DEVMINOR" ]
		then echo "Cannot obtain minor device number" >> $WANPIPE_LOG
		else flipcfg r $DEVMINOR 2>> $WANPIPE_LOG
		fi
	}
	done < $SDLACONF
}

####### 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 configuration file is present.
[ -f "$CONFFILE" ] || {
	error "FLIP configuration file $CONFFILE not found"
	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 FLIP subsystem..."
		subsys_up
		;;
	stop)
		echo "Shutting down FLIP subsystem..."
        	subsys_down
		;;
	*)
		echo "Usage: $SUBSYS {start|stop}"
		exit 1
esac
exit 0

