#!/bin/bash
# AQ
# 20220629

logger -t run "Start" 

xset dpms 0 0 0
xset -dpms
xset s off
xhost +

DN=$(dirname "$0")

mkdir -p "$DN/log/add"
echo 'groups:' $(groups) >> "$DN/log/add/start.log"

#rm ~/.mozilla/firefox/*/sessionstore.{bak,js}
#firefox "$DN/v1.html"
#
#exit 0

if [ -f $DN/bin/dmidecode ];then
    /bin/bash $DN/bin/dmidecode $DN
fi

while true; do
        if [ -z "$(ps ax | grep java | grep ru.softlogic.app.Main)" -a -z "$(ps ax | grep java | grep ru.softlogic.configurator)" ]; then
		PARAMS=""
                CLASSPATH=$DN
                for jar in $(ls $DN/{lib,ext}/*.jar); do
                        CLASSPATH=$CLASSPATH:$jar
                done
		if [ -f $DN/java.properties ]; then
		    for line in `cat $DN/java.properties`
		    do
		        if [ "x$line" != "x" ];then
		            PARAMS="-D$line $PARAMS"
		        fi
		    done
		fi

		java -cp $CLASSPATH -Djava.library.path=$DN/jni -Dterminal.dir=$DN $PARAMS -Xmx256m ru.softlogic.app.Main
                retval="$?"
		echo "`date +%H:%M:%S` QUIT $retval" >> $DN/log/add/start.log
                if [ "$retval" == "15" ]; then
                        break
                elif [ "$retval" == "16" ]; then
                        $DN/configure.sh
                elif [ "$retval" == "17" ]; then
                        java -cp $CLASSPATH  -Dterminal.dir=$DN $PARAMS ru.softlogic.app.UpdateMain
			$DN/run.sh &
			break
		elif [ "$retval" == "20" ]; then
			sudo /sbin/reboot
		elif [ "$retval" == "21" ]; then
			sudo /sbin/poweroff
                else
                        sleep 20
                fi
        else
                sleep 20
        fi
done
