#
#
# 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 "$FIFOPATH" ]; then
	if [ -z "$OSER_FIFO" ]; then
		FIFOPATH=/var/run/kamailio/kamailio_fifo
	else
		FIFOPATH=$OSER_FIFO
	fi
fi

#
##### ----------------------------------------------- #####
### functions
#
usage_fifo() {
	echo
	mecho " -- command 'mi' - send raw MI commands"
	echo
cat <<EOF
 mi ................................. send raw MI command
 fifo ............................... send raw FIFO (MI) 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=kamailio_receiver_$$
	path=$CHROOT_DIR/tmp/$name
	# delete existing fifo file with same name
	if test -p $path; then
		rm -f $path
	fi
	if [ ! -w $FIFOPATH ]; then
		merr "Error opening Kamailio's FIFO $FIFOPATH"
		merr "Make sure you have the line 'modparam(\"mi_fifo\", \"fifo_name\", \"$FIFOPATH\")' in your config"
		merr "and also have loaded the mi_fifo module."
		if [ ! -z $CHROOT_DIR ]; then
			merr "[chrooted environment] Check that $FIFOPATH is symlinked to ${CHROOT_DIR}${FIFOPATH}"
		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" > $FIFOPATH

	# wait for the reader to complete
	wait
	rm $path

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


CTLCMD=fifo_cmd

fifo_kamailio_monitor() {
	name=kamailio_receiver_$$
	path=$CHROOT_DIR/tmp/$name
	# delete existing fifo file with same name
	if test -p $path; then
		rm -f $path
	fi
	if [ ! -w $FIFOPATH ]; then
		merr "Error opening Kamailio's FIFO $FIFOPATH"
		merr "Make sure you have the line 'modparam(\"mi_fifo\", \"fifo_name\", \"$FIFOPATH\")' 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 > $FIFOPATH <<EOF
:version:$name

EOF
		wait

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

EOF
		wait
		echo

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

EOF
		wait
		echo

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

EOF
		wait
		echo

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

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

KAMAILIO_MONITOR=fifo_kamailio_monitor