#!/bin/bash
############################################
# Central script for installing or uninstalling DTBMaster or DTBServer into wine.
# Usage: ./dtb-installer DTBMaster install|uninstall|status (lang=en|lang=de) sync=no|sync=hdms|sync=display_name
#						 DTBServer install|uninstall|status lang=en|lang=de|lang=fr
#
# rpm69 - removing old wine instructions
############################################

# 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 greater than 4 or less than 2, or it is not the standard than print out the help.
if [ $# -gt 4 ] || [ $# -lt 2 ]  || \
  ([ $1 != "DTBMaster" ] && [ $1 != "DTBServer" ]) || \
  ([ $2 != "install" ] && [ $2 != "uninstall" ] && [ $2 != "status" ])
then
    logger -t $CMD -p ERROR -s "Usage: $0 DTBMaster|DTBServer install|uninstall|status lang=en|lang=de|lang=fr sync=no|sync=hdms|sync=display_name"
    exit
fi

if [ $1 = "DTBMaster" ]; then
    PACKAGE=DTBMASTER
else 
    PACKAGE=DTBSERVER
fi

plog "Check the registry, if the DTB is installed or not"
REGENTRY=${PACKAGE}_REGKEY
# The check is based on wine version
plog "Executing: sudo -u $DTB_USER WINEDEBUG=fixme-all,err-ole,-rpc WINEARCH=win32 WINEPREFIX=$DTB_WORKINGDIR DISPLAY=:$XDISPLAYNR.0 $WINE_BIN reg QUERY \"${!REGENTRY}\""
sudo -u $DTB_USER WINEDEBUG=fixme-all,err-ole,-rpc \
				WINEARCH=win32 \
				WINEPREFIX=$DTB_WORKINGDIR \
				DISPLAY=:$XDISPLAYNR.0 \
				$WINE_BIN reg QUERY "${!REGENTRY}" /f 1>/dev/null 2>/dev/null
status=$?


# Check the installation status then exit
if [ $2 = "status" ]; then
    if [ $status == 0 ]; then
        echo "Status: $1 is installed."
        exit 0
    else
        echo "Status: $1 is not installed."
        exit 2
    fi
fi

# Is the desired operation really necessary?
if [ $2 = "install" ] && [ $status -eq 0 ]; then 
	plog "Trying to install $1, but it is already installed; Nothing to do."
	exit 0
elif [ $2 = "uninstall" ] && [ $status -eq 1 ]; then
    plog "Trying to uninstall $1, but it is not installed; Nothing to do."
    exit 0
fi

# If the desired operation is ready to go
INSTALLPATH=${PACKAGE}_INSTALLPATH

if [ $2 = "install" ]; then
	if [ $1 == "DTBMaster" ]; then
	    if [ $# -eq 3 ]; then # if there are 3 parameters, then setup a simple install (eng, hdms)
			ISS_FILE=${PACKAGE}_INSTALL_ISS
		elif [ $# -eq 4 ]; then # if there are 4 parameters, then check the fourth
		    if [ $4 = "sync=no" ]; then
			    ISS_FILE=${PACKAGE}_NOSYNC_SETUP_ISS
		    elif [ $4 = "sync=hdms" ]; then
			    ISS_FILE=${PACKAGE}_HDMS_SETUP_ISS
		    elif [ $4 = "sync=display_name" ]; then
				ISS_FILE=${PACKAGE}_DISPLAY_NAME_SETUP_ISS
			fi
		else # if there are 2 parameters, then setup a simple install (eng, hdms)
			ISS_FILE=${PACKAGE}_INSTALL_ISS
		fi
	elif [ $1 == "DTBServer" ]; then
	    if [ $# -eq 3 ]; then # if there are 3 parameters, then check the third
	        if [ $3 = "lang=en" ]; then
    	        ISS_FILE=${PACKAGE}_INSTALL_ISS
	        elif [ $3 = "lang=de" ]; then
	            ISS_FILE=${PACKAGE}_INSTALL_ISS_DE
	        elif [ $3 = "lang=fr" ]; then
	            ISS_FILE=${PACKAGE}_INSTALL_ISS_FR
	        fi
		else # if there are 2 parameters, then setup a simple install (eng)
			ISS_FILE=${PACKAGE}_INSTALL_ISS
		fi
	fi
    SETUP_PARAMETER="/s /sms"
else
    ISS_FILE=${PACKAGE}_UNINSTALL_ISS
    SETUP_PARAMETER="/uninst /s /sms"
fi

plog "$2 $PACKAGE with ${!INSTALLPATH}/${!ISS_FILE}"

# ensure, Xvnc server and lwm are running
	checkproc "$XVNC_BIN"
	if [ $? != 0 ]; then
		plog "$XVNC_BIN is not running." 
		$XSERVER_START
		if [ $? != 0 ]; then
			plog "ERROR: Xvnc server/lwm not ready; Exiting."
			exit 1
		fi
	fi

# Remove the SyncTrig.cfg file from DTBMaster - this will prevent installshield "access denied" errors
rm -rf $DTB_DRBD_SHARE/DTBMaster/winedir/SyncTrig.cfg

# Finally do the install/uninstall operation with the ISS files
cd ${!INSTALLPATH}

plog "Trying to start setup.exe using wine."
plog "Executing on ${!INSTALLPATH}: sudo -u $DTB_USER WINEDEBUG=fixme-all,err-ole,-rpc WINEARCH=win32 WINEPREFIX=$DTB_WORKINGDIR DISPLAY=:$XDISPLAYNR.0 $WINE_BIN setup.exe $SETUP_PARAMETER /f1"Z:${!INSTALLPATH////\\}\\${!ISS_FILE}""
sudo -u $DTB_USER WINEDEBUG=fixme-all,err-ole,-rpc \
                  WINEARCH=win32 \
                  WINEPREFIX=$DTB_WORKINGDIR \
                  DISPLAY=:$XDISPLAYNR.0 \
                  $WINE_BIN setup.exe $SETUP_PARAMETER /f1"Z:${!INSTALLPATH////\\}\\${!ISS_FILE}" #1>/dev/null 2>/dev/null

# Post action: if there was no DTB_Server.exe installed, then rename the DTB Server.exe into DTB_Server.exe
if [ $1 == "DTBServer" ] && [ $2 == "install" ]; then
	# if [ ! -f $DTB_WORKINGDIR/drive_c/DTBServer/DTB_Server.exe ] && [ -f $DTB_WORKINGDIR/drive_c/DTBServer/DTB\ Server.exe ]; then
        mv $DTB_WORKINGDIR/drive_c/DTBServer/DTB\ Server.exe $DTB_WORKINGDIR/drive_c/DTBServer/DTB_Server.exe
    # fi
fi

# Post action: Make sure that the registry is also cleared up.
if [ $1 == "DTBMaster" ] && [ $2 == "uninstall" ]; then
	plog "Executing: sudo -u $DTB_USER WINEDEBUG=fixme-all,err-ole,-rpc WINEARCH=win32 WINEPREFIX=$DTB_WORKINGDIR DISPLAY=:$XDISPLAYNR.0 $WINE_BIN reg delete $DTBMASTER_REGKEY /f"
	sudo -u $DTB_USER WINEDEBUG=fixme-all,err-ole,-rpc \
					  WINEARCH=win32 \
					  WINEPREFIX=$DTB_WORKINGDIR \
					  DISPLAY=:$XDISPLAYNR.0 \
					  $WINE_BIN reg delete "$DTBMASTER_REGKEY" /f
fi
					
# Post action: Make sure that the registry is also cleared up.
if [ $1 == "DTBServer" ] && [ $2 == "uninstall" ]; then
	plog "Executing: sudo -u $DTB_USER WINEDEBUG=fixme-all,err-ole,-rpc WINEARCH=win32 WINEPREFIX=$DTB_WORKINGDIR DISPLAY=:$XDISPLAYNR.0 $WINE_BIN reg delete $DTBSERVER_REGKEY /f"
	sudo -u $DTB_USER WINEDEBUG=fixme-all,err-ole,-rpc \
					  WINEARCH=win32 \
					  WINEPREFIX=$DTB_WORKINGDIR \
					  DISPLAY=:$XDISPLAYNR.0 \
					  $WINE_BIN reg delete "$DTBSERVER_REGKEY" /f
fi

# Post action: [OSFOURK-21150] - Certain directories need certain rights
chmod o+x /var/pgsql
chmod o+x /var/pgsql/dtb
chmod o+x /var/pgsql/dtb/DTBServer
chmod o+x /var/pgsql/dtb/DTBServer/config