#!/bin/bash

# Currently this script is used to configure xinetd service only for Remote Logging service (via Telnet) 
#

if [ -f /etc/fiad-conf/loglogin.conf ] ; then
	. /etc/fiad-conf/loglogin.conf
else
    exit 0
fi

# Start xinetd (telnet) service only when Remote Logging is enabled
#
if [ "$LOGGER_ON" != "1" ] ; then
    /bin/killall logshell > /dev/null 2>&1
    /bin/rm -f /tmp/logshell
    /bin/killall xinetd > /dev/null 2>&1
    /bin/rm -f /tmp/xinetd.conf
    exit 0
fi

#
#Generate "/tmp/xinetd.conf"
#
if [ -f /tmp/featurestatus.conf ] ; then
	. /tmp/featurestatus.conf
fi

echo "defaults" > /tmp/xinetd.conf
echo "{" >> /tmp/xinetd.conf
echo "	instances          = 60" >> /tmp/xinetd.conf
echo "	log_type           = SYSLOG authpriv" >> /tmp/xinetd.conf
echo "	log_on_success     = HOST PID" >> /tmp/xinetd.conf
echo "	log_on_failure     = HOST" >> /tmp/xinetd.conf
echo "	cps                = 25 30" >> /tmp/xinetd.conf
echo "}" >> /tmp/xinetd.conf
echo "" >> /tmp/xinetd.conf

echo "service telnet-logs" >> /tmp/xinetd.conf
echo "{" >> /tmp/xinetd.conf
echo "	flags              = REUSE" >> /tmp/xinetd.conf
echo "	socket_type        = stream" >> /tmp/xinetd.conf
echo "	wait               = no" >> /tmp/xinetd.conf
echo "	user               = logger" >> /tmp/xinetd.conf
echo "	server             = /bin/telnetd" >> /tmp/xinetd.conf
echo "	server_args        = -E /bin/logshell" >> /tmp/xinetd.conf
echo "	log_on_failure     += USERID" >> /tmp/xinetd.conf
echo "}" >> /tmp/xinetd.conf
echo "" >> /tmp/xinetd.conf

#
#Generate "/tmp/logshell"
#

STR=""
#MARKER="\"|><>MARKER<><|\""

if [ "$LOGGER_ON" = "1" ] ; then
	if [ "$LOG_CM" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/cm.log | sed 's/^\(.\)/CM:     \1/I'"
	fi

	if [ "$LOG_SIP_UA" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/sip_ua.log | sed 's/^\(.\)/SIPUA:  \1/I'"
	fi

	if [ "$LOG_MG" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/mg.log | sed 's/^\(.\)/MG:     \1/I'"
	fi

	if [ "$LOG_MESSAGES" = "1" ] ; then
		STR=$STR" & cat /var/log/messages | sed 's/^\(.\)/MESS:   \1/I'"
	fi

	if [ "$LOG_DSP" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/dsp.log | sed 's/^\(.\)/DSP:    \1/I'"
	fi

	if [ "$LOG_VMS" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/vms.log | sed 's/^\(.\)/VMS:    \1/I'"
	fi

	if [ "$LOG_SIP_REG" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/sip_reg.log | sed 's/^\(.\)/SIPREG: \1/I'"
	fi

	if [ "$LOG_FXO_AGENT" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/fxoagent.log | sed 's/^\(.\)/FXO:    \1/I'"
	fi

	if [ "$LOG_CAS_USER" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/casuser.log | sed 's/^\(.\)/CASUSR: \1/I'"
	fi

	if [ "$LOG_CAS_NET" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/casnet.log | sed 's/^\(.\)/CASNET: \1/I'"
	fi

	if [ "$LOG_ISDN_L2" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/isdnl2.log | sed 's/^\(.\)/ISDNL2: \1/I'"
	fi

	if [ "$LOG_ISDN_AGENT" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/isdnagent.log | sed 's/^\(.\)/ISDNAG: \1/I'"
	fi

	if [ "$LOG_SPMG_AGENT" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/spmgagent.log | sed 's/^\(.\)/SPMG:   \1/I'"
	fi

	if [ "$LOG_FOIP" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/foip.log | sed 's/^\(.\)/FOIP:   \1/I'"
	fi

	if [ "$LOG_SIP_SUB" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/sip_sub.log | sed 's/^\(.\)/SIPSUB: \1/I'"
	fi

	if [ "$LOG_PRESENCE" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/presence.log | sed 's/^\(.\)/PUA:    \1/I'"
	fi

	if [ "$LOG_ACD" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/acd.log | sed 's/^\(.\)/ACD:    \1/I'"
	fi

	if [ "$LOG_PMS" = "1" ] ; then
		STR=$STR" & cat /tmp/logs/pms.log | sed 's/^\(.\)/PMS:    \1/I'"
	fi
fi

if [ "$STR" != "" ] ; then
	STR=${STR:3}
fi

echo "#!/bin/sh" >  /tmp/logshell
echo "$STR"      >> /tmp/logshell
chmod 700 /tmp/logshell

#
#Restart inetd
#
/bin/killall xinetd > /dev/null 2>&1
/sbin/xinetd -f /tmp/xinetd.conf > /dev/null 2>&1

exit 0
