#!/bin/bash

set -e

MAX_MODULES=16
MODULE_CHANS=8

VERBOSE=
etc_path=/etc/dahdi/tdmox
sys_path=/sys/bus/dahdi_tdmox/devices
conf_file=
spanno=
span_dir=
modules=
channels=

die() { echo "$@" 1>&2 ; exit 1; }

comment_chan() {
cat << EOF >> $1/name
#Channel name. Up to 40 bytes.
EOF
cat << EOF >> $1/sig
#Channel signalling (Optional). One of sigcap.
EOF
cat << EOF >> $1/sigcap
#Capability for signalling. Can specify multiple, devided by space.
#FXSLS FXSKS FXSGS FXOLS FXOKS FXOGS E&M E&M-E1 Clear HDLCRAW HDLCFCS HDLCNET MTP2 Slave CAS DACS DACS+RBS
EOF
}

comment_span() {
cat << EOF >> $1/addr
#TDMoX address.
#for TDMoE: dev/addr/subaddr
EOF
cat << EOF >> $1/channels
#Number of the channels
EOF
cat << EOF >> $1/deflaw
# ALAW MULAW
EOF
cat << EOF >> $1/desc
#SPAN description. Up to 80 bytes. Up to 40 bytes recommended.
#Will override description generated by the driver.
EOF
cat << EOF >> $1/devicetype
#SPAN devicetype. Will override devicetype generated by the driver.
EOF
cat << EOF >> $1/driver
#channel driver. eth for TDMoE (TDM over Ethernet)
EOF
cat << EOF >> $1/flags
#RBS or not RBS for now.
EOF
cat << EOF >> $1/linecompat
#D4 ESF AMI B8ZS CCS HDB3 CRC4 NTTE NOTOPEN
EOF
cat << EOF >> $1/location
#SPAN location. Will override location generated by the driver.
EOF
cat << EOF >> $1/manufacturer
#SPAN manufacturer. Will override manufacturer generated by the driver.
EOF
cat << EOF >> $1/name
#SPAN name. Up to 40 bytes. Up to 20 recommended.
#Will override name generated by the driver.
EOF
cat << EOF >> $1/spantype
#FXS FXO ANALOG_MIXED E1 T1 J1 BRI_NT BRI_TE BRI_SOFT
EOF
cat << EOF >> $1/timing
# timing priority, like for a normal span.
# use "0" to not use this as a timing source, or prioritize them as
# primary, secondard, etc.
EOF
}

_getconf_asteroid_module() {
	local from_port=$1
	local from_chan=$2
	local total=$3
	local type=$4
	local channo			# SPAN's channel number
	local chan_dir
	local end=$(( from_chan + total ))
	local portno=$from_port  # Device's port number
	local mportno=1          # Module's port number
	local sig
	local name

	for (( channo=$from_chan; channo<$end; channo++ ))
	do
		chan_dir=$span_dir/chan-$channo
		[ -d "$chan_dir" ] || mkdir $chan_dir 2>/dev/null
    	
    	case $type in
			FXS)
				sig=FXOLS
				name="TDM_AS/$spanno/chan-$channo""_port-$portno"
			;;
			FXO)
				sig=FXSLS
				name="TDM_AO/$spanno/chan-$channo""_port-$portno"
			;;
			GSM)
				sig="E&M"
		    	case $mportno in
					3|7) name="TDM_AE/$spanno/chan-$channo""_port-$portno" ;;
					*)   name="---/$spanno/chan-$channo""_port-$portno" ;;
			esac
			;;
			EMPTY)
				sig=FXSLS
				name="---/$spanno/chan-$channo""_port-$portno"
			;;
		esac

		echo $name > $chan_dir/name
		echo $sig > $chan_dir/sig
		echo $sig > $chan_dir/sigcap
		portno=$(( portno + 1 ))
		mportno=$(( mportno + 1 ))
		comment_chan $chan_dir
	done
}

getconf_asteroid_module() {
	local module=$1
	local type=$2

	if [ "asteroid2" = "$device" ]
	then
	    case $module in
			1) _getconf_asteroid_module 1  1  7 $type ;;
			2) _getconf_asteroid_module 8  8  8 $type ;;
			3) _getconf_asteroid_module 17 16 7 $type ;;
			4) _getconf_asteroid_module 24 23 8 $type ;;
		esac
	else
		local offset=$(( module * MODULE_CHANS - MODULE_CHANS + 1 ))
		_getconf_asteroid_module $offset $offset $MODULE_CHANS $type
	fi
}

genconf_asteroid() {
	echo RBS > $span_dir/flags
	echo > $span_dir/linecompat
	echo ANALOG_MIXED > $span_dir/spantype
#	dahdi_tdmox will give a name (like TDM_A/0)

    case $device in
    	asteroid2)
			have_break=1
			modules=4
			channels=30
			echo "Asteroid2 @ $addr" > $span_dir/desc
		;;
		asteroid2-mt16)
			modules=16
			channels=128
			echo "Asteroid2-mt16 @ $addr" > $span_dir/desc
		;;
	esac

	echo $channels > $span_dir/channels

	local i
	for (( i=1; i<=$modules; i++ ))
	do
		varname="module$i"
		local type=${!varname}
		if [ -z $type ]
		then
			getconf_asteroid_module $i "EMPTY"
		else
			getconf_asteroid_module $i $type
		fi
	done
}

genconf_elf() {
	channels=31

	echo 					> $span_dir/flags
	echo "HDB3 CCS CRC4"	> $span_dir/linecompat
	echo "E1" 				> $span_dir/spantype
	echo "ELF2-AE @ $addr" 	> $span_dir/desc
	echo $channels 			> $span_dir/channels
#	dahdi_tdmox will give a name (like TDM_A/0)
}

genconf() {
	. $conf_file || exit 0

	[ -z $spanno ] && die "Missing required parameter 'spanno' !"
	[ -z $device ] && die "Missing required parameter 'device' !"
	[ -z $addr ] && die "Missing required parameter 'addr' !"
	[ -z $timing ] && die "Missing required parameter 'timing' !"

    span_dir="`dirname $conf_file`/span-$spanno"
	[ -d "$span_dir" ] || mkdir $span_dir 2>/dev/null

	echo $addr > $span_dir/addr
	echo ALAW > $span_dir/deflaw
	echo eth > $span_dir/driver
	echo Parabel ltd > $span_dir/manufacturer
	echo $timing > $span_dir/timing

	echo > $span_dir/devicetype
	echo > $span_dir/location
	echo > $span_dir/name

    case $device in
    	asteroid2)
			echo "Generating configuration files for Asteroid2"
			nots16=1
			genconf_asteroid
		;;
		asteroid2-mt16)
			echo "Generating configuration files for Asteroid2-mt16"
			genconf_asteroid
		;;
		elf2-ae)
			echo "Generating configuration files for elf2-ae"
			genconf_elf
		;;
	esac

	comment_span $span_dir
}


##########################################################################

usage()
{
cat << EOF
usage: $0 config_file options

This script generates configuration files for TDMoX devices, produced by Parabel ltd.
dahdi_tdmox must be called to configure dahdi_tdmox - it will register SPAN\'s.
SPAN configuration should be done via dahdi_cfg & system.conf (as for any other SPAN).

OPTIONS:
   -h      Show this message
   -c      Configuration dirrectory path
   -v      Verbose
EOF
}

conf_file=$1
shift

while getopts “hvc:” OPTION
do
     case $OPTION in
         h)
             usage
             exit 1
             ;;
         c)
             etc_path=${OPTARG}
             ;;
         v)
             VERBOSE=1
             ;;
         ?)
             usage
             exit
             ;;
     esac
done

genconf

echo "dahdi_tdmox: Configuration generation finished."
