 #!/bin/bash
############################################
# Common definitions for dtb-wine solution
# Usage: ./_common_definitiones
#
# Changelog:
# rpm68 - adding french language
# rpm69 - created waitDTBstart()
# rpm71 - Using DTBconfigFile.xml
# rpm77 - moving from /var/tmp/dtb into /var/pgsql/dtb_config/
############################################

#Debugging (1=on; 0=off)
DTB_DEBUG=0

## Common definitions
DTB_INSTALLDIR=/var/opt/dtb/install
DTB_WORKINGDIR=/var/opt/dtb/wineroot
DTB_DRBD_SHARE=/var/pgsql/dtb
DTB_CFGDIR=/var/pgsql/dtb_config

WINE_BIN=/usr/bin/wine
DTB_USER=dtbuser
DTB_GROUP=users
XVNC_BIN=/usr/bin/Xvnc
LWM_BIN=/usr/bin/lwm
DUMMY_REGISTRY_FILE=/tmp/dtb.reg

XDISPLAYNR=10

DTB_WRAPPER=/opt/dtb/bin/dtb-wrapper
DTB_SERVER_EXE=DTB_Server.exe
DTBSERVER_REGKEY="HKEY_LOCAL_MACHINE\Software\Siemens\Display Telephonebook"
DTBSERVER_SHUTDOWN=/opt/dtb/utilities/gracefulShutdownServer.exe
DTB_MASTER_EXE=DTBNetzConfig.exe
DTBMASTER_REGKEY="HKEY_LOCAL_MACHINE\Software\Siemens\DTBNetzConfig"
DTBMASTER_SHUTDOWN=/opt/dtb/utilities/gracefulShutdownMaster.exe

DTBMASTER_INSTALLSRC=/opt/dtb/DTBMaster/install
DTBMASTER_CONFIGSRC=/opt/dtb/DTBMaster/config
DTBMASTER_INSTALLPKG=Disk1
DTBMASTER_INSTALLPATH=$DTB_INSTALLDIR/DTBMaster/Disk1
DTBMASTER_NOSYNC_SETUP_ISS=nosync-setup.iss
DTBMASTER_HDMS_SETUP_ISS=hdms-setup.iss
DTBMASTER_DISPLAY_NAME_SETUP_ISS=display_name-setup.iss
DTBMASTER_UM_SETUP_ISS=um-setup.iss
DTBMASTER_INSTALL_ISS=$DTBMASTER_NOSYNC_SETUP_ISS
DTBMASTER_UNINSTALL_ISS=uninstall.iss

DTBSERVER_INSTALLSRC=/opt/dtb/DTBServer/install
DTBSERVER_CONFIGSRC=/opt/dtb/DTBServer/config
DTBSERVER_INSTALLPKG=Disk1
DTBSERVER_INSTALLPATH=$DTB_INSTALLDIR/DTBServer/Disk1
DTBSERVER_INSTALL_ISS=setup.iss
DTBSERVER_INSTALL_ISS_DE=setup-de.iss
DTBSERVER_INSTALL_ISS_FR=setup-fr.iss
DTBSERVER_UNINSTALL_ISS=uninstall.iss

XSERVER_START="/opt/dtb/bin/dtb-xserver start"
SETCAPBINS="/usr/bin/wine-preloader /usr/bin/wine /usr/bin/wineserver /usr/bin/winedbg"
WINECAPS="cap_net_bind_service,cap_net_admin,cap_net_raw+eip"

DTB_SYNC_FLAG_FILE=/var/opt/dtb_sync
DTB_LANG_FLAG_FILE=/var/opt/dtb_lang
DTB_SYNCSTATUS=/var/pgsql/dtb_config/syncstatus
DTB_TESTRESULT=/var/pgsql/dtb_config/testresult
CONFIG_FILE=/var/pgsql/dtb_config/DTBconfigFile.xml
WSI_CFG=/var/pgsql/dtb_config/wsi.xml
LOGFILE=$DTB_DRBD_SHARE/DTBMaster/winedir/iDTB.log

function plog()
{
  LVAR=$* 										 	  # the input message parameter
  CMD=$(basename $0) 						     	  # The script name where is running from
  d=$(date +%Y-%m-%d)" "$(date +%H:%M:%S) 		 	  # current date
  if [ -f $LOGFILE ]; then
	if [[ $(stat -c%s $LOGFILE) -ge 50000000 ]]; then # if the logfile is greater than 50MB then create bak
		mv $LOGFILE  $LOGFILE.bak
		logger -t $CMD -p INFO -s "iDTB.log.bak created."
		LOGLINE="$d - $CMD: $LVAR"
		echo $LOGLINE >> $LOGFILE.bak
		echo "$CMD: $LVAR"
		echo "$d - PLOG: iDTB.log.bak created." >> $LOGFILE.bak
	else
		LOGLINE="$d - $CMD: $LVAR"
		echo $LOGLINE >> $LOGFILE
		echo "$CMD: $LVAR"
	fi
  else
	if [ -d $DTB_DRBD_SHARE ]; then
		touch $LOGFILE
		chmod 600 $LOGFILE
		chown -R $DTB_USER:$DTB_GROUP $LOGFILE
		logger -t $CMD -p INFO -s "iDTB.log created."
		echo "$d - PLOG: iDTB.log created." >> $LOGFILE
		LOGLINE="$d - $CMD: $LVAR"
		echo $LOGLINE >> $LOGFILE
		echo "$CMD: $LVAR"
	fi
  fi
}

# Function for waiting the DTB until completely stops
waitDTBstop()
{
	status=0
	status_server=0
	status_master=0
	
	plog "Waiting for DTB to stop..."
	trap "plog signal received" SIGTERM SIGABRT 
	# In V10 platform the dtb resource crashes when it is stopped.

	# wait for 10 second, and in every second verify the DTB status. If stopped then skip waiting.
	for i in {1..10}; do
		$DTB_WRAPPER DTBServer status 1>/dev/null 2>/dev/null
		status_server=$?
		$DTB_WRAPPER DTBMaster status 1>/dev/null 2>/dev/null
		status_master=$?
		if ([ $status_server -eq 0 ] || [ $status_master -eq 0 ]); then
			echo "DTB is still running" 1>/dev/null
		else
			status=1
			break;
		fi
		sleep 1
	done
	
	# wait for 10 second, and in every second verify the resource status. If stopped then skip waiting.
	for i in {1..10}; do
		isDTBrunning=$(crm resource status rsc4k_dtb 2>&1)
		if [[ $isDTBrunning == "resource rsc4k_dtb is NOT running" ]]; then
			status=1
			break;
		else
			echo "rsc4k_dtb is still running" 1>/dev/null
		fi
		sleep 1
	done

	if [ $status -eq 1 ]; then
		plog "DTB has stopped"
	fi
}

# Function for waiting the DTB until completely starts
waitDTBstart()
{
	status=0
	status_server=0
	status_master=0
	
	plog "Waiting for DTB to start..."
	trap "plog signal received" SIGTERM SIGABRT 
	# In V10 platform the dtb resource crashes when it is stopped.

	# wait for 10 second, and in every second verify the DTB status. If stopped then skip waiting.
	for i in {1..10}; do
		$DTB_WRAPPER DTBServer status 1>/dev/null 2>/dev/null
		status_server=$?
		$DTB_WRAPPER DTBMaster status 1>/dev/null 2>/dev/null
		status_master=$?
		if ([ $status_server -eq 2 ] || [ $status_master -eq 2 ]); then
			echo "DTB is still stopped" 1>/dev/null
		else
			status=1
			break;
		fi
		sleep 1
	done
	
	# wait for 10 second, and in every second verify the resource status. If stopped then skip waiting.
	for i in {1..10}; do
		isDTBrunning=$(crm resource status rsc4k_dtb 2>&1)
		if [[ $isDTBrunning == "resource rsc4k_dtb is running"* ]]; then
			status=1
			break;
		else
			echo "rsc4k_dtb is still stopped" 1>/dev/null
		fi
		sleep 1
	done

	if [ $status -eq 1 ]; then
		plog "DTB has started"
	fi
}

function readConfigXML(){
	lookingFor=$1;
	result=$(grep $lookingFor $CONFIG_FILE);
	result=$(echo $result | sed "s/<$lookingFor>//g; s/<\/$lookingFor>//g")
	echo $result
}
