#!/bin/bash
############################################
# runcmd
#
# Helper script to run an optional command in the DTB's wine environment
# Usage: ./runcmd <command with optional parameters>
# 
# Copyright (c) 2016 Unify GmbH & Co. KG
# Demeter Aron, Kemenes Robert 
# aron.demeter.ext@unify.com, robert.kemenes.ext@unify.com
#
# Last modification: 02.03.2017 in dtb-1.0-28.noarch.rpm
############################################

# This is a Helper script, it is not used anywhere, so don't fulfill the logs with it.

# 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 [ $# -lt 1 ]
then
    echo "Usage: $0 <command with optional parameters>"
    exit
fi;

# 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

# Finally try to call the user command
sudo -u $DTB_USER WINEDEBUG=fixme-all,err-ole,-rpc \
                  WINEARCH=win32 \
                  WINEPREFIX=$DTB_WORKINGDIR \
                  DISPLAY=:$XDISPLAYNR.0 \
                  $WINE_BIN $* 1>/dev/null 2>/dev/null
				  