#!/bin/bash
############################################
# dtb-xserver
#
# Wrapper script to ensure that X server is running.
# Usage: ./dtb-xserver start|stop
#
# Copyright (c) 2017 Unify GmbH & Co. KG
# Demeter Aron, Kemenes Robert 
# aron.demeter.ext@unify.com, robert.kemenes.ext@unify.com
#
# rpm69: added -SecurityTypes=None for new wine
############################################

# Import definitions
CMD=$(basename $0)
if [ -f /opt/dtb/bin/_common_definitions ]; then
    . /opt/dtb/bin/_common_definitions
else
	logger -t $CMD -p ERROR -s "The /opt/dtb/bin/_common_definitions file does not exist, exiting the script..."
	exit 1
fi

# Evaluate parameters: if the number of parameters is less than 1, or it is not the standard than print out the help.
if [ $# -ne 1 ] || \
  ([ $1 != "start" ] && [ $1 != "stop" ])
then
    logger -t $CMD -p ERROR -s "Usage: $0 start|stop"
    exit
fi;

if [ $1 == "start" ]; then

	# Open VNC port for debugging, if it is not already done
	if [ $DTB_DEBUG == 1 ]; then
		iptables -S soco2_open_ports | grep " --dport 59$XDISPLAYNR -j ACCEPT" >/dev/null
		if [ $? != 0 ]; then
			plog "Opening VNC port 59${XDISPLAYNR} for DTB debugging..."
			iptables -A soco2_open_ports -m tcp -p tcp --dport 59$XDISPLAYNR -j ACCEPT >/dev/null
			plog "Port 5910 opened."
		else
			plog "The port 5910 is already open. Nothing to do."
		fi
	fi

	# Check if the Xvnc server is already running
	checkproc "$XVNC_BIN"
	if [ $? != 0 ]; then
		plog "Starting Xvnc server..."
		startproc -u $DTB_USER "$XVNC_BIN" :$XDISPLAYNR -SecurityTypes=None -geometry 1024x768 -depth 24 1>/dev/null 2>/dev/null
		sleep 1
		
		# If it still not starts, then clean the X sockets
		checkproc "$XVNC_BIN"
		if [ $? != 0 ]; then
			plog "Cleaning up the X-server sockets..."
			rm -rf /tmp/.X11-unix
		fi
		
		checkproc "$XVNC_BIN"
		if [ $? != 0 ]; then
			plog "ERROR: Could not start the Xvnc server."
			exit 1
		else
			plog "Xvnc server started."
		fi
		
    else
        plog "The Xvnc server is already running; Nothing to do."
	fi
	


	# Check if the LWM is already running
	checkproc "$LWM_BIN"
	if [ $? != 0 ]; then
		plog "Starting LWM..."
		export DISPLAY=:$XDISPLAYNR.0
		startproc -u $DTB_USER "$LWM_BIN" 1>/dev/null 2>/dev/null
		sleep 1
		
		checkproc "$LWM_BIN"
		if [ $? != 0 ]; then
			plog "ERROR: Could not start the LWM server."
			exit 1
		else
			plog "LWM started."
		fi
		
	else
        plog "The LWM is already running; Nothing to do."
	fi

else
	
	# Close the VNC port, if it is open
	# It doesn't matter if the debug flag is on 1 because without the Xvnc the debugging is not possible.
	# count the number of checks in the IP tabels to eliminate infinite loops and close also the manually opened ports
	COUNTER=1;
	iptables -S soco2_open_ports | grep " --dport 59${XDISPLAYNR} -j ACCEPT" >/dev/null
	retval=$?
	if [ $retval == 0 ]; then
		while [ $retval == 0 ] && [ $COUNTER -le 10 ]; do
			plog "($COUNTER): Closing the VNC port 59${XDISPLAYNR} for DTB debugging..."
			iptables -D soco2_open_ports -m tcp -p tcp --dport 59$XDISPLAYNR -j ACCEPT >/dev/null
			sleep 1
			iptables -S soco2_open_ports | grep " --dport 59${XDISPLAYNR} -j ACCEPT" >/dev/null
			retval=$?
			COUNTER=$(( $COUNTER + 1 ))
		done
		plog "Port 5910 closed."
	else
		plog "The port 5910 is already closed; Nothing to do."
	fi

    # Stop the LWM, if it is running
    checkproc "$LWM_BIN"
    if [ $? == 0 ]; then
        plog "Stopping the LWM..."
        killproc "$LWM_BIN"
        sleep 1
		checkproc "$LWM_BIN"
		if [ $? == 0 ]; then
			plog "ERROR: Could not stop the LWM server."
			exit 1
		else
			plog "LWM stopped."
		fi
	else
		plog "The LWM is not running; Nothing to do."
    fi

    # Stop the Xvnc server, if it is running
    checkproc "$XVNC_BIN"
    if [ $? == 0 ]; then
        plog "Stopping the Xvnc server..."
        killproc "$XVNC_BIN"
        sleep 1
		checkproc "$XVNC_BIN"
		if [ $? == 0 ]; then
			plog "ERROR: Could not stop the Xvnc server."
			exit 1
		else
			plog "Xvnc server stopped."
		fi
	else
		plog "The Xvnc server is not running; Nothing to do."
    fi
	
fi
