#
# $Id$
#
# control tool for maintaining Kamailio
#
#===================================================================

##### ----------------------------------------------- #####
### FIFO specific variables and functions
#

##### ----------------------------------------------- #####
### load CTL base
#
if [ -f "$MYLIBDIR/kamctl.ctlbase" ]; then
	. "$MYLIBDIR/kamctl.ctlbase"
else
	mwarn "Cannot load CTL core functions '$MYLIBDIR/kamctl.ctlbase' ..."
	# exit -1
fi

#
##### ----------------------------------------------- #####
### parameters
#
if [ -z "$OSER_FIFO" ]; then
	OSER_FIFO=/tmp/kamailio_fifo
fi

#
##### ----------------------------------------------- #####
### functions
#
usage_fifo() {
	echo
	mecho " -- command 'fifo'"
	echo
cat <<EOF
 fifo ............................... send raw FIFO command
EOF
}
USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_fifo"


fifo_cmd()
{
	mdbg "entering fifo_cmd $*"
	
	if [ "$#" -lt 1 ]; then
		merr "fifo_cmd must take at least command name as parameter"
		exit 1
	fi
	name=openser_receiver_$$
	path=$CHROOT_DIR/tmp/$name
	if [ ! -w $OSER_FIFO ]; then
		merr "Error opening Kamailio's FIFO $OSER_FIFO"
		merr "Make sure you have the line 'modparam(\"mi_fifo\", \"fifo_name\", \"$OSER_FIFO\")' in your config"
		merr "and also have loaded the mi_fifo module."
		if [ ! -z $CHROOT_DIR ]; then
			merr "[chrooted environment] Check that $OSER_FIFO is symlinked to ${CHROOT_DIR}${OSER_FIFO}"
		fi
		exit 2
	fi
	if ! test -p $path; then
		mkfifo $path
		if [ $? -ne 0 ] ; then
			merr "error opening read fifo $path"
			exit 3
		fi
		chmod a+w $path
	fi

	# construct the command now
	CMD=":$1:$name\n";
	shift
	while [ -n "$1" ] ; do
		CMD="${CMD}${1}\n"
		shift
	done
	CMD="${CMD}\n"

	trap "rm -f $path; kill 0" 2

	# start reader now so that it is ready for replies
	# immediately after a request was sent out
	cat < $path | filter_fl &

	# issue FIFO request (printf taken to deal with \n)
	printf "$CMD" > $OSER_FIFO

	# wait for the reader to complete
	wait
	rm $path

	mdbg "FIFO command was:\n$CMD"
}


CTLCMD=fifo_cmd

fifo_openser_monitor() {
	name=openser_receiver_$$
	path=$CHROOT_DIR/tmp/$name
	if [ ! -w $OSER_FIFO ]; then
		merr "Error opening Kamailio's FIFO $OSER_FIFO"
		merr "Make sure you have the line 'modparam(\"mi_fifo\", \"fifo_name\", \"$OSER_FIFO\")' in your config"
		merr "and also have loaded the mi_fifo module."
		exit 1
	fi
	if ! test -p $path; then
		mkfifo $path
		if [ $? -ne 0 ] ; then
			merr "monitor - error opening read fifo $path"
			exit 1
		fi
		chmod a+w $path
	fi
	trap "rm $path;  clear; echo monitor ^C-ed; exit 1" 2
	attempt=0
	if [ "$2" = "" ]; then
	    loops=-1;
	else
	    loops=$2;
	fi
	clear
	while [ $loops -ne $attempt ] ; do
		attempt=`$EXPR $attempt + 1`
		#clear
		tput clear
		
		# print_stats $name $path $attempt
		mecho "[cycle #: $attempt; if constant make sure server lives]"

		cat < $path | filter_fl &
		cat > $OSER_FIFO <<EOF
:version:$name

EOF
		wait

		cat < $path | filter_fl &
		cat > $OSER_FIFO << EOF
:uptime:$name

EOF
		wait
		echo

		mecho "Transaction Statistics: "
		cat < $path | filter_fl &
		cat > $OSER_FIFO <<EOF
:get_statistics:$name
UAS_transactions
UAC_transactions
inuse_transactions

EOF
		wait
		echo

		mecho "Stateless Server Statistics: "
		cat < $path | filter_fl &
		cat > $OSER_FIFO <<EOF
:get_statistics:$name
sent_replies
sent_err_replies
received_ACKs

EOF
		wait
		echo

		mecho "UsrLoc Stats: "
		cat < $path | filter_fl &
		cat > $OSER_FIFO <<EOF
:get_statistics:$name
usrloc:

EOF
		wait
		
		if [ $loops -ne $attempt ] ; then
			sleep $WATCH_PERIOD
		fi
	done
	rm $path
	exit 0
}

OPENSER_MONITOR=fifo_openser_monitor