#!/bin/sh

# Initialise the DSPs to provide interface to TDM channels
# and echo cancellation, DTMF detection and DTMF generation

# If "start" arg has not been passed in, assume
# emulator is being used

[ -f /SpliceCom/pots_level ] && eval POTS_LEVEL=`cat /SpliceCom/pots_level`

case "$1" in
	start )
		insmod dsp || exit
		
		case $POTS_LEVEL in
			6 )	insmod vinetic4c6 || exit ;;
			12 )	insmod vinetic4c12 || exit ;;
			* )	insmod vinetic4c || exit ;;
		esac
		insmod isdndrv || exit
		
		rm -f /SpliceCom/dsps
		for x in 0 1 2 3; do
			echo Z > /dev/dsp$x
			test $? -eq 0 && echo -n $x >> /SpliceCom/dsps
		done
			
		if [ `cat /SpliceCom/dsps` = "02" ]; then
			platform=NSG
			last_comp_dsp=2
			last_tdm_dsp=0
			hw_type=1
			if [ ! -f /etc/hostname ]; then
				MAC=`ifconfig eth0 | grep Ethernet | cut -d " " -f 11 | cut -c 1,2,4,5,7,8,10,11,13,14,16,17`                                                               
				if [ "$MAC" ]; then
					HOSTNAME="NSG5500-$MAC"
					echo "$HOSTNAME" > /etc/hostname
					hostname $HOSTNAME
				fi
			fi	
		else
			platform=CS5100
			last_comp_dsp=3
			last_tdm_dsp=1
			hw_type=0
		fi
		echo "Platform $platform detected"
		
		for x in 0 1; do
			echo "Loading DSP$x TDM/LEC/POTS/DTMF code"
			cat /SpliceCom/cs5100.sre > /dev/dsp${x}srec
			echo B$hw_type > /dev/dsp$x
			[ $x = $last_tdm_dsp ] && break
		done
		
		for x in 2 3; do
			echo "Loading DSP$x VCOMP code"
			cat /SpliceCom/vcomp.sre > /dev/dsp${x}srec
			echo B$hw_type > /dev/dsp$x
			[ $x = $last_comp_dsp ] && break
		done
		
		echo G > /dev/dsp0
		insmod dtmf || exit
		insmod rtp_ppp || exit
		ifconfig rtp0 192.168.78.7
		insmod tdmsync
		;;
		
	stop )
		rmmod tdmsync
		ifconfig rtp0 0.0.0.0
		ifconfig rtp0 down
		rmmod rtp_ppp
		rmmod dtmf
		rmmod isdndrv
		rmmod vinetic4c
		rmmod dsp
		;;
		
	* ) 	# Assume emulator in use
		x=0
		insmod dsp debug_level=1 || exit
		insmod vinetic4c trace_level=1 || exit
		insmod isdndrv || exit
		echo Z > /dev/dsp$x
		echo press return when emulator loaded and running
		read a
		echo b > /dev/dsp$x
		echo G > /dev/dsp0
		insmod dtmf || exit
		insmod rtp_ppp
		ifconfig rtp0 192.168.78.7
		;;
esac

