#!/bin/bash

. ./../common.config
. ./terminal.config

export LD_LIBRARY_PATH=$HOME_DIR/lib_v7

#export LADY_FOR_WEB=1

#test yserver running, if not, start it
ps -A|grep yserver 1>/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
	$HOME_DIR/RUN_YSERVER.sh & 1>/dev/null 2>/dev/null
fi

export VSYNC=1
export FPS=25
export CPU_FPS=500
export DISABLE_HW_ACCELERATION=1

#binary name, ki ga bomo zagnali
export BIN_FILENAME=/$HOME_DIR/ladyroulette

sleep 1 #mora biti zaradi loader-ja

#preverimo ali je ruleta aktivirana
#if  [ -z `cat $HOME_DIR/flavor_prefix` ]; then
#	echo ""
#	echo "Roulette has not been activated yet."
#	echo "Please complete the steps in 'POPE->Activate New Roulette' widzard."
#	exit 0;
#fi

# board hardware identification
export CPU_VENDOR_ID=`cat /proc/cpuinfo | grep "vendor_id" |head -1| cut -c 13-26`
export CPU_FAMILY="F"`cat /proc/cpuinfo | grep "cpu family" |head -1| cut -c 14-16`
export CPU_MODEL="M"`cat /proc/cpuinfo | grep -P "model\t" |head -1| cut -c 10-12`
export CPU_STEPPING="S"`cat /proc/cpuinfo | grep -P "stepping\t" |head -1| cut -c 12-14`
[ "AuthenticAMD_F22_M0_S1" == $CPU_VENDOR_ID"_"$CPU_FAMILY"_"$CPU_MODEL"_"$CPU_STEPPING ] && export AUDIODEV=hw:1,0 && echo "RUNLADYROULETTE.SH: This is mainboard KINO_KBN_i2"

declare -x LOG_FILENAME=$OLD_GAMES_DIR"/LADY.log"
export 1>/dev/null 2>/dev/null

#zazenemo binary
echo "RUNLADYROULETTE.SH: Loading libraries from $LD_LIBRARY_PATH"
ps -A|grep -e unity -e gdm &>/dev/null
if [ $? -ne 0  ]; then
#zagon na diskless terminalih
    echo "RUNLADYROULETTE.SH: No X server at \$DISPLAY [$DISPLAY], starting with xinit" >&2

    #nastavitev x serverja in virtual terminal Ctrl+Alt+Fn 
    #mora biti :1, ker na :0 ze tece loader!
    export X_DISPLAY=:1
    export VIRTUAL_TERMINAL=vt6
    xinit $BIN_FILENAME -- $X_DISPLAY $VIRTUAL_TERMINAL 2>/dev/null
else
#zagon lokalno, na masterju
    echo "RUNLADYROULETTE.SH: X server running at \$DISPLAY [$DISPLAY]" >&2

        #before starting program, we check if a _update file is pending as program update - usefull to update at next reboot
        if [ -e $BIN_FILENAME"_update" ]; then
            echo
            echo "    "Updating $BIN_FILENAME with file $BIN_FILENAME"_update"
            echo "    "Old version will be renamed to $BIN_FILENAME"_old"
            echo

            mv -f $BIN_FILENAME $BIN_FILENAME"_old" > /dev/null
            mv -f $BIN_FILENAME"_update" $BIN_FILENAME > /dev/null
            chmod ugo+x $BIN_FILENAME
        fi;

    export DISABLE_REBOOT_ON_EXIT=1
    $BIN_FILENAME &
    PID1=$!
#    if [ `xrandr --query|grep +0|wc -l` -ge 2 ]; then
#      	#gremo v fullscreen, ker sta dva monitorja
#       	echo Adding fullscreen and above
#	sleep 4
#    	wmctrl -r "Lady Roulette" -e 0,0,0,-1,-1 & #uncomment for VideoScreen on UPPER MONITOR
#    	#wmctrl -r "Lady Roulette" -e 0,0,1080,-1,-1 & #uncomment for VideoScreen on BOTTOM MONITOR
#    	wmctrl -r "Lady Roulette" -b add,fullscreen &
#    	sleep 1
#	wmctrl -r "Lady Roulette" -b add,above & #allways on top
#   else #ni potrebno, ker ze Lady-ka sama upravlja s tem
#	#ne gremo v fullscreen, samo en monitor
#	sleep 4
#	if [[ $PRODUCT_NAME == *"SinglePlayer"* ]]; then
#		wmctrl -r "Lady Roulette" -b add,below & #single-monitor, singleplayer
#	else
##lady-ka je lahko vedno above
#		wmctrl -r "Lady Roulette" -b add,above & #single-monitor, multiplayer
#	fi
#    fi

#    wait $PID1
#    $HOME_DIR/manage_unity_plugin.sh show
fi


exit 0;
