#!/bin/bash

. ./terminal.config


if [ `xrandr -q | grep ' connected' | wc -l` -lt 2 ]; then
	echo "This system has less than two monitors. Running of topscreen is disabled"
exit
fi

#export SAS_FORCE_ENABLED=1

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

export FPS=30
export VSYNC=1
if [ `xrandr -q | grep ' connected' | wc -l` -gt 2 ]; then
	VSYNC=0 #izklopimo vsync, ce so trije monitorji, ker utripa rdece!
fi
export CPU_FPS=200



#rabimo to mapo za load-e in export-e iz usb kjluca
mkdir /mnt/tmp1 

#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 ] && echo "RUNTERMINAL.SH: This is mainboard KINO_KBN_i2"

#mehanizem za rotiranje log-file-ov
TERMINAL_NUMBER=$1
if [ -z "$TERMINAL_NUMBER" ];then
	TERMINAL_NUMBER=1
fi
OLD_GAMES_DIR=$OLD_GAMES_DIR"/10.5.10."$TERMINAL_NUMBER
mkdir -p $OLD_GAMES_DIR
#log_index=`cat $OLD_GAMES_DIR/log_index_topscreen`
#RESULT=$?
#if [ $RESULT -gt 0 ]; then
#    log_index=0
#fi;
#let ++log_index
#if [ $log_index -ge 70 ]; then
#    log_index=0
#fi;
#echo $log_index > $OLD_GAMES_DIR/log_index_topscreen
#declare -x LOG_FILENAME=$OLD_GAMES_DIR"/TOPSCREEN_"$log_index".log"
#rm $LOG_FILENAME -rf
declare -x LOG_FILENAME=$OLD_GAMES_DIR"/TOPSCREEN.log"


#zazenemo binary
echo "RUNTOPSCREEN.SH: Loading libraries from $LIBS_PATH"

#kill eventual zombies
killall -9 gamearttopscreen

export 1>/dev/null 2>/dev/null
ps -A|grep -e unity -e gdm3 &>/dev/null
if [ $? -ne 0  ]; then
#zagon na diskless terminalih
    echo "RUNTOPSCREEN.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

    export EXPORT_USB_KEY_DEV_NAME=$EXPORT_USB_KEY_DEV_NAME_ONTERMINAL
    cp -rf /home/ROULETTE/terminal/xorg_terminal.conf /etc/X11/xorg.conf
    xinit LD_LIBRARY_PATH=$LIBS_PATH $BIN_FILENAME -- $X_DISPLAY $VIRTUAL_TERMINAL
else
#zagon lokalno, na masterju
    echo "RUNTOPSCREEN.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
    export EXPORT_USB_KEY_DEV_NAME=$EXPORT_USB_KEY_DEV_NAME_ONSLOT

    LD_LIBRARY_PATH=$LIBS_PATH $BIN_FILENAME 127.0.0.1:9011

fi

exit 0;

