#! /bin/sh
#
# /usr/sbin/span_types
#
# This script can be used both from udev and
# from the command line to manage PRI spans
# type (E1/T1/J1).
#
# Span types can be set only *BEFORE* span are assigned.
#
# It reads a configuration file /etc/dahdi/span-types.conf
# (the format is documented inside that file)
#
# A mandatory first argument is:
#   list       - to show existing E1/T1/J1 types
#   dumpconfig - the same, but in a format (almost) suitable
#                for the configuration file
#   set        - actually write the setting to the driver
#
# Examples:
#    span_types list
#    span_types dumpconfig
#    span_types set	     # all devices
#    span_types set /sys/bus/dahdi_devices/devices/astribanks:xbus-00
#


devbase='/sys/bus/dahdi_devices/devices'
DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
spantypes_conf="$DAHDICONFDIR/span-types.conf"

usage() {
	echo >&2 "Usage: $0 {list|dumpconfig|set} [devpath ...]"
	exit 1
}

if [ "$#" -eq 0 ]; then
	usage
fi
action="$1"
shift

if [ ! -d "$devbase" ]; then
	echo >&2 "$0: Missing '$devbase' (DAHDI driver unloaded?)"
	exit 1
fi

# Use given devices or otherwise, all existing devices
if [ "$#" -gt 0 ]; then
	DEVICES="$@"
else
	DEVICES=`echo $devbase/*`
fi

show_spantypes() {
	echo "# PRI span types (E1/T1/J1)"
	for device in $DEVICES
	do
		hw_id=`cat "$device/hardware_id"`
		location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
		cat "$device/spantype" | while read st; do
			case "$st" in
			*:[ETJ]1)
				printf "%-10s %-20s %s\n" \
					"$st" "[$hw_id]" "$location"
				;;
			esac
		done | sort -n
	done
}

dump_config() {
	echo '#'
	echo "# Autogenerated by $0 on `date`"
	echo "# Map PRI DAHDI devices to span types for E1/T1/J1"
	echo ''
	fmt="%-65s %s\n"
	printf "$fmt" '# @location/hardware_id' 'span_type'
	for device in $DEVICES
	do
		hw_id=`cat "$device/hardware_id"`
		location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
		if [ -n "$hw_id" ]; then
			id="$hw_id"
		else
			id="$location"
		fi
		#echo "# Device: [$hw_id] $location"
		cat "$device/spantype" | while read st; do
			case "$st" in
			*:[ETJ]1)
				printf "$fmt" "$id" "$st"
				;;
			*)
				#echo "#    Skipped local span `echo $st | sed 's/:/ -- /'`"
				;;
			esac
		done | sort -n
		#echo ''
	done
}

# Allow comments and empty lines in config file
filter_conf() {
	sed -e 's/#.*//' -e '/^[ \t]*$/d' "$spantypes_conf"
}

conf_spans() {
	hw_id="$1"
	location="$2"
	filter_conf | (
		# Collect device spans
		# in a subshell, so $SPANS is not lost
		SPANS=''
		while read id spans; do
			# GLOBBING
			case "$location" in
			$id)
				#echo >&2 "match($id): $spans"
				SPANS="$SPANS $spans"
				;;
			esac
			case "$hw_id" in
			$id)
				#echo >&2 "match([$id]): $spans"
				SPANS="$SPANS $spans"
				;;
			esac
		done
		echo "$SPANS"
	)
}

# Beware of special characters in attributes
attr_clean() {
	cat "$1" | tr -d '\n' | tr '!' '/' | tr -c 'a-zA-Z0-9/:.-' '_'
}

device_set_spantype() {
	device="$1"
	attr_file="$device/spantype"
	hw_id=`attr_clean "$device/hardware_id"`
	location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
	spanspecs=`conf_spans "$hw_id" "$location"`
	#echo >&2 "MATCHED($device): $spanspecs"
	cut -d: -f1 "$attr_file" | while read spanno; do
			for sp in $spanspecs
			do
				s=`echo "$sp" | cut -d: -f1`
				v=`echo "$sp" | cut -d: -f2`
				case "$spanno" in
				$s)
					#echo >&2 "conf($attr_file): $spanno:$v"
					echo "$spanno:$v" > "$attr_file"
					;;
				esac
			done
		done
}

set_spantypes() {
	if [ ! -f "$spantypes_conf" ]; then
		echo >&2 "$0: Missing configuration '$spantypes_conf'"
		exit 1
	fi
	for device in $DEVICES
	do
		device_set_spantype "$device"
	done
}

case "$action" in
list)
	show_spantypes
	;;
dumpconfig)
	dump_config
	;;
set)
	set_spantypes
	;;
*)
	usage
	;;
esac
