#!/bin/sh

. /lib/rcscripts/sh/rc-std2parse.sh

. /usr/html/axis-cgi/lib/functions.sh
log="logger -s -t${0##*/}[$$]"
LOG_E="$log -perr"
LOG_I="$log -pinfo"

CONF_FILE=/etc/dynamic/mcast-always.conf
DESCRIPTION=
DBUS="dbus-send --system --dest=com.axis.Streamer --print-reply --type=method_call"
DOBJ_PATH=/com/axis/Streamer/RTP
METHOD_PREFIX=com.axis.Streamer.RTP

log_and_exit() {
	[ $# -ge 1 ] && [ "$1" ] || {
		$LOG_E "log_and_exit: Missing or empty argument"
		__cgi_errhd 500
		exit 1
	}
	$LOG_E $1
	__cgi_errhd ${2:-500} $1
	exit 1
}

log_service_unavailable_and_exit() {
	[ $# -eq 1 ] && [ "$1" ] || {
		$LOG_E "log_service_unavailable_and_exit: Missing or empty argument"
		__cgi_errhd 503
		exit 1
	}
	$LOG_I $1
	__cgi_errhd 503
	exit 1
}


get_session_id() {

	local f=get_session_id id

	[ $# -eq 3 ] && [ "$1" ] && [ "$2" ] && [ "$3" ] ||
		log_and_exit "invalid arguments could not get session id"

	[ -e $CONF_FILE ] ||
		log_service_unavailable_and_exit "$CONF_FILE doesn't exist, can't provide sdp description."

	[ -r $CONF_FILE ] ||
		log_service_unavailable_and_exit "$CONF_FILE is not readable, can't provide sdp description."

	# If the $CONF_FILE has a zero size it means that the content is
	# not flushed yet
	[ -s $CONF_FILE ] ||
		log_service_unavailable_and_exit "$CONF_FILE have zero length, can't provide sdp description."
	id=$(sed -rne "s|^$3[[:blank:]]+$2[[:blank:]]+id(.*)$|\1|p" $CONF_FILE) || {
		LOG_E "$f: could not retrieve session id."
		return 1
	}
	case $id in
		''|*[!0-9]*)
			log_and_exit "$f: invalid session id."
			;
	esac

	eval $1=\$id
}

add_description() {

	local f=add_description pipeline_desc media_desc

	case $1 in
		audio|video)
			;
		*)
			$LOG_E "$f: invalid argument."
			return 1
			;
	esac

	pipeline_desc=$($DBUS $DOBJ_PATH/Pipeline/"$2" \
		$METHOD_PREFIX.Pipeline.Describe |
		sed -r  -e '/^.*method[[:blank:]]+return/d' \
			-e '/^"$/d' \
			-e '/^$/d' \
			-e 's/^[[:blank:]]+string[[:blank:]]+"(.*)$/\1/') &&
	[ "$pipeline_desc" ] || return 1


	if [ -z "$DESCRIPTION" ]; then
		DESCRIPTION=$pipeline_desc
	else
		media_desc="${pipeline_desc##*
m=}"
		DESCRIPTION="$DESCRIPTION
m=$media_desc"
	fi
}

CHANNEL=$(__qs_getparam camera) && [ "$CHANNEL" ] ||
	log_and_exit "failed to retrieve the camera parameter from URL."

std2parse /etc/sysconfig/rtp.conf

[ $CHANNEL -gt 0 ] && [ $CHANNEL -le $STD2_NETWORK_RTP_NBROFRTPGROUPS ] ||
	log_and_exit "the 'camera' parameter should be at least 1 and maximum \
$STD2_NETWORK_RTP_NBROFRTPGROUPS." 400

CHANNEL=$(($CHANNEL - 1))

eval VIDEO_ENABLED=\$STD2_NETWORK_RTP_R${CHANNEL}_ALWAYSMULTICASTVIDEO
eval AUDIO_ENABLED=\$STD2_NETWORK_RTP_R${CHANNEL}_ALWAYSMULTICASTAUDIO

[ "$VIDEO_ENABLED" = yes ] || [ "$AUDIO_ENABLED" = yes ] ||
	log_and_exit "no sdp description to provide; \
Always Multicast is not started on this channel." 400

if [ "$VIDEO_ENABLED" = yes ]; then
	get_session_id video_id video "$CHANNEL" ||
		log_and_exit "failed to read video session id from $CONF_FILE"
	add_description video "$video_id" ||
		log_and_exit "failed to retrieve video sdp description."
fi

if [ "$AUDIO_ENABLED" = yes ]; then
	get_session_id audio_id audio "$CHANNEL" ||
		log_and_exit "failed to read audio session id from $CONF_FILE"
	add_description audio "$audio_id" ||
		log_and_exit "failed to retrieve audio sdp description."
fi

__cgi_hdgen yes application/sdp && echo "$DESCRIPTION" || {
	$LOG_E "failed to send HTTP packet."
	exit 1
}