#!/bin/bash
# IPC - MASTERSCRIPT (CCcam)
#

. /var/emu/script/functions.sh
readipccfg
[ -z $CCNAME ] && clear && echo -e "\033[1;31mVariablen Konfigurationsfehler!\033[0m" && exit 1
[ -z $CAMBINS ] && clear && echo -e "\033[1;31mVariablen Konfigurationsfehler!\033[0m" && exit 1
[ -z $CAMLOGS ] && clear && echo -e "\033[1;31mVariablen Konfigurationsfehler!\033[0m" && exit 1
[ -z $CCVERSION ] && clear && echo -e "\033[1;31mVariablen Konfigurationsfehler!\033[0m" && exit 1
[ -z $IPCVERSION ] && clear && echo -e "\033[1;31mVariablen Konfigurationsfehler!\033[0m" && exit 1
[ -z $CRON ] && clear && echo -e "\033[1;31mIPC - Paket Konfigurationsfehler!\033[0m" && exit 1

CAMNAME="$CCNAME"

if [ ! -f "$CCBIN" ]; then
	echo -e "\e[1;31m$CAMNAME wurde deaktiviert!"
	echo -e "Zum Re-aktivieren erst \e[0;32mi cams\e[1;31m nutzen!\e[0m"
	exit 1
fi

# CCcam - Start
start_cam () {
	timestamp=`date +"%Y%m%d_%H%M"`
	if [ ! -f $CAMLOGS/$CAMNAME.OK ]; then
		dummy="1"
		#cp /var/log/syslog $CAMLOGS/$CAMNAME-restart-$timestamp.log
	fi
	$CCBIN -v -t & #logged CCcam Messages in SYSLOG!
	if [ $? -ne "0" ]; then
		echo "Fehler beim starten von $CAMNAME!"
	else
		echo "$CAMNAME : gestartet"
	fi
	rm -f $CAMLOGS/$CAMNAME.OK
}

# CCcam - Stop
stop_cam () {
	echo "OK" >> $CAMLOGS/$CAMNAME.OK
	pkill $CCVERSION
	sleep 2
	PID="$(pgrep -x $CCVERSION)"
	if [[ ! -n $PID ]] ; then
		echo "$CAMNAME : beendet"
	else
		pkill -9 $CCVERSION
		echo "$CAMNAME : gekillt"
	fi
}

# Befehlsparameter
case "$1" in
	start)
		PID="$(pgrep -x $CCVERSION)"
		if [[ ! -z $PID ]] ; then
			echo "$CAMNAME : ist bereits gestartet"
		else
			start_cam
			if [[ $CRON == 1 ]]; then
				PID="$(pgrep -x cron)"
				if [[ ! -z $PID ]] ; then
					dummy="1"
				else
					echo "Cron  : gestartet"
					/etc/init.d/cron start > /dev/null
				fi
			fi
		fi
	;;
	stop)
		PID="$(pgrep -x $CCVERSION)"
		if [[ ! -z $PID ]] ; then
			stop_cam
			if [[ $CRON == 1 ]]; then
				PID="$(pgrep -x cron)"
				if [[ ! -z $PID ]] ; then
					echo "Cron  : beendet"
					/etc/init.d/cron stop > /dev/null
				else
					echo "Cron  : ist bereits beendet"
				fi
			fi
		else
			echo "$CAMNAME : ist bereits beendet"
		fi
	;;
	restart)
		echo "$CAMNAME : restart"
		stop_cam
		sleep 3
		start_cam
	;;
	help)
		clear
		less $CAMBINS/script/cccam-help.txt
	;;
	version)
		$CAMBINS/script/cccam-version.sh
	;;
	channelinfo)
		$CAMBINS/script/cccam-channelinfo.sh
	;;
	providers)
		$CAMBINS/script/cccam-providers.sh
	;;
	i)
		$CAMBINS/script/cccam-i.sh
	;;
	is)
		$CAMBINS/script/cccam-is.sh
	;;
	ic)
		$CAMBINS/script/cccam-ic.sh
	;;
	iac)
		$CAMBINS/script/cccam-iac.sh
	;;
	ir)
		$CAMBINS/script/cccam-ir.sh
	;;
	tcp)
		$CAMBINS/script/cccam-tcp.sh
	;;
	cfg)
		$CAMBINS/script/cccam-cfg.sh
	;;
	cfgh)
		$CAMBINS/script/cccam-cfgh.sh
	;;
	cfgn)
		$CAMBINS/script/cccam-cfgn.sh
	;;
	cfgnd)
		$CAMBINS/script/cccam-cfgnd.sh
	;;
	cfgnl)
		$CAMBINS/script/cccam-cfgnl.sh
	;;
	log)
		clear
		echo -e "$CAMNAME-Server: \033[1;37mLog\033[0m - Abbrechen mit [Strg]+[C]                  $(date +"%d.%m.%Y %H:%M")"
		echo "==============================================================================="
		echo ""
		if [ -f  /var/log/syslog ] ; then
			tail -f /var/log/syslog | grep -i $CAMNAME:
		else
			echo " Keine Logdatei vorhanden." && echo ""
		fi
	;;
	iu)
		clear
		echo -e "$CAMNAME-Server: \033[1;37mIllegal Users\033[0m - Abbrechen mit [Strg]+[C]        $(date +"%d.%m.%Y %H:%M")"
		echo "==============================================================================="
		echo ""
		if [ -z `cat $CAMLOGS/$CAMNAME-iusers.log` ]; then
			echo " Keine Logeintraege vorhanden." && echo ""
		elif [ -f  $CAMLOGS/$CAMNAME-iusers.log ] ; then
			tail -f $CAMLOGS/$CAMNAME-iusers.log
		else
			echo " Keine Logdatei vorhanden." && echo ""
		fi
	;;
	bc)
		clear
		echo -e "$CAMNAME-Server: \033[1;37mBad Commands\033[0m - Abbrechen mit [Strg]+[C]         $(date +"%d.%m.%Y %H:%M")"
		echo "==============================================================================="
		echo ""
		if [ -z `cat $CAMLOGS/$CAMNAME-bcommands.log` ]; then
			echo " Keine Logeintraege vorhanden." && echo ""
		elif [ -f  $CAMLOGS/$CAMNAME-bcommands.log ] ; then
			tail -f $CAMLOGS/$CAMNAME-bcommands.log
		else
			echo " Keine Logdatei vorhanden." && echo ""
		fi
	;;
	crash)
		$CAMBINS/script/cccam-crash.sh
	;;
	changes)
		$CAMBINS/script/cccam-changes.sh
	;;
	[fF][aA][iI][lL]2*)
		$CAMBINS/script/cccam-fail2ban.sh
	;;
	[sS][oO][fF][tT]*)
		$CAMBINS/script/softkey.sh
	;;
	*)
		clear
		echo -e "\033[1;37m$CAMNAME-Server IPC $IPCVERSION                                         $(date +"%d.%m.%Y %H:%M")\033[0m"
		echo "==============================================================================="
		echo ""
		echo -e "Befehlsliste: \033[1;31mcccam\033[0m"
		echo ""
		echo -e "\033[1;37m  Hilfe zu diesen Befehlen\033[0m                    \033[1;31mhelp\033[0m"
		echo -e "\033[1;37m  Version anzeigen/installieren\033[0m               \033[0;37mversion\033[0m"
		echo -e "\033[1;37m  Start, Stop, Restart\033[0m                        \033[0;37mstart | stop | restart\033[0m"
		echo -e "\033[1;37m  Konfiguration\033[0m                               \033[0;37mcfg | cfgn | cfgnl | cfgnd\033[0m"
		echo -e "\033[1;37m  Informationen\033[0m                               \033[0;37mi | is | ic | iac | ir | tcp\033[0m"
		echo -e "\033[1;37m  Channelinfo, Providers aktualisieren\033[0m        \033[0;37mchannelinfo | providers\033[0m"
		echo -e "\033[1;37m  Log: Was laeuft, Crash, Changes\033[0m             \033[0;37mlog | crash | changes\033[0m"
		echo -e "\033[1;37m  Log: Illegal Users, Bad Commands\033[0m            \033[0;37miu | bc\033[0m"
		echo -e "\033[1;37m  fail2ban: installieren / konfigurieren\033[0m      \033[0;37mfail2ban\033[0m"
		echo -e "\033[1;37m  Softcam Keys aktualisieren\033[0m                  \033[0;37msoftkey\033[0m"
		echo ""
		echo "==============================================================================="
		echo ""
		exit 1
	;;
esac

exit 0
