#!/bin/bash

TDMCONF=/usr/conf/isgw.tdm

if [ $# == 1 ]
then
	TDMCONF=$1
fi

echo -n "generating $TDMCONF ... "

declare -a LI

# detect
LI[0]=$(cat /proc/li0 | grep "^type: " | cut -d" " -f2)
LI[1]=$(cat /proc/li1 | grep "^type: " | cut -d" " -f2)

function li_get_ports()
{
	local li=$1
	local ports=""
	case "$li" in
		xhfc-1su|hfc-e1)
			ports="1"
			;;
		xhfc-2su|hfc-2e1)
			ports="1 2"
			;;
		xhfc-2s4u|xhfc-4su)
			ports="1 2 3 4"
			;;
		si-4fxo|si-4fxs)
			ports="101 102 103 104"
			;;
		xhfcSi-2s02fxs)
			ports="1 2 103 104"
			;;
		telit-2GSM)
			ports="201 202"
			;;
	esac

	echo $ports
}

function li_get_channels_per_port()
{
	local xhfc=$1
	local channels=""

	case "$xhfc" in
		xhfc*)
			channels="1 2"
			;;
		hfc-e1|hfc-2e1)
			channels="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30"
			;;
		*)
		channels="1"
			;;
	esac

	echo $channels
}

function li_get_delay_transmitter_per_port()
{
	local xhfc=$1
	local dt=""
	case "$xhfc" in
		xhfc-*)
			dt=14
			;;
		hfc-e1|hfc-2e1)
			dt=15
			;;
		si-*)
			dt=13
			;;
		xhfcSi-2s02fxs)
			dt=15
			;;
		telit-2GSM)
			dt=0
			;;
		*)
			dt=14
		;;
	esac
	echo $dt
}

function li_get_delay_receiver_per_port()
{
	local xhfc=$1
	local rt=""
	case "$xhfc" in
		si-*)
			rt=13
			;;
		xhfcSi-2s02fxs)
			rt=15
			;;
		telit-2GSM)
			rt=1
			;;
		*)
			rt=14
			;;
	esac
	echo $rt
}

function li_get_clock_mode_select_transmitter()
{
	local li=$1
	local rt=""
	case "$li" in
		telit-2GSM)
			rt="single_clock"
			;;
		*)
			rt="double_clock"
			;;
	esac
	echo $rt
}

function li_get_clock_mode_select_receiver()
{
	local li=$1
	local rt=""
	case "$li" in
		telit-2GSM)
			rt="single_clock"
			;;
		*)
			rt="double_clock"
			;;
	esac
	echo $rt
}

function li_get_frame_edge()
{
	local li=$1
	local rt=""
	case "$li" in
		telit-2GSM)
			rt="on_rising"
			;;
		*)
			rt="on_falling"
			;;
	esac
	echo $rt
}

DT_PORT_1=$(li_get_delay_transmitter_per_port ${LI[0]})
DT_PORT_2=$(li_get_delay_transmitter_per_port ${LI[1]})

DR_PORT_1=$(li_get_delay_receiver_per_port ${LI[0]})
DR_PORT_2=$(li_get_delay_receiver_per_port ${LI[1]})

CMT_BUS_1=$(li_get_clock_mode_select_transmitter ${LI[0]})
CMT_BUS_2=$(li_get_clock_mode_select_transmitter ${LI[1]})

CMR_BUS_1=$(li_get_clock_mode_select_receiver ${LI[0]})
CMR_BUS_2=$(li_get_clock_mode_select_receiver ${LI[1]})

FED_BUS_1=$(li_get_frame_edge ${LI[0]})
FED_BUS_2=$(li_get_frame_edge ${LI[1]})

# write
echo "; isgw.tdm: autogenerated by $0 ($(/bin/date))

; tdm_delay: bus clocks_delay_transmitter clocks_delay_receiver
tdm_delay 1 $DT_PORT_1 $DR_PORT_1
tdm_delay 2 $DT_PORT_2 $DR_PORT_2

; clock_mode: bus mode_transmitter mode_receiver  (single_clock/double_clock)
clock_mode 1 $CMT_BUS_1 $CMR_BUS_1
clock_mode 2 $CMT_BUS_2 $CMR_BUS_2

; frame_edge: bus frame_edge_detect (on_rising/on_falling)
frame_edge 1 $FED_BUS_1
frame_edge 2 $FED_BUS_2


; timeslot: bus port channel timeslot" > $TDMCONF


IPORT=1
APORT=101
GPORT=201
OPORT=0

for i in 0 1
do
	BUS=$(($i + 1))
	TIMESLOT=0
	
	for p in $(li_get_ports ${LI[$i]})
	do
		if [ $p -gt 200 ];then
			OPORT=$GPORT
			let GPORT++
		elif [ $p -gt 100 ];then
			OPORT=$APORT
			let APORT++
		else
			OPORT=$IPORT
			let IPORT++
		fi

		CHANNELS=$(li_get_channels_per_port ${LI[$i]})
		
		#we have only one channel on analog or gsm ports
		if [ $OPORT -gt 100 ];then
			CHANNELS=1
		fi
		
		for CHANNEL in $CHANNELS
		do
			echo "timeslot $BUS $OPORT $CHANNEL $TIMESLOT" >> $TDMCONF
			TIMESLOT=$(($TIMESLOT + 1))
		done
	done
done

echo "done"
