#!/usr/bin/sh

#*********************************************************************************************************
#*   __     __               __     ______                __   __                      _______ _______   *
#*  |  |--.|  |.---.-..----.|  |--.|   __ \.---.-..-----.|  |_|  |--..-----..----.    |       |     __|  *
#*  |  _  ||  ||  _  ||  __||    < |    __/|  _  ||     ||   _|     ||  -__||   _|    |   -   |__     |  *
#*  |_____||__||___._||____||__|__||___|   |___._||__|__||____|__|__||_____||__|      |_______|_______|  *
#* http://www.blackpantheros.eu | http://www.blackpanther.hu - kbarcza[]blackpanther.hu * Charles Barcza *
#*************************************************************************************(c)2002-2019********
##   blackPanther Default Apps Collection
base=$(basename $0)
. /etc/blackPanther-default-apps.conf

if [ -n "$(pidof Agent 2>/dev/null)" ];then
    echo "Agent pid found... server already running..."
    PID=$(pidof Agent 2>/dev/null)
else
    PID=$(pidof dotnet 2>/dev/null)
fi

if [ ! -f $HOME/.AgentDVR/.installed ]&&[ -x /usr/bin/agentdvr-server ];then
    exec /usr/bin/agentdvr-server &
    echo "First run! Preparing...."
    bubblemsg "AgentDVR Server" "First run! Preparing.. please Wait!"
    sleep 10
fi

if [ ! -n "$PID" ];then
    echo "Not running server! Try start now...."
    bubblemsg "AgentDVR Server" "Not running! Start from client now..."
    until pids=$(pidof dotnet)
    do   
	echo "Wait for $PID 5sec"
	systemctl --user start agentdvr
        #exec /usr/bin/agentdvr-server &
        bubblemsg "AgentDVR Wait" "Wait to server is started..."
	sleep 5
    done
	sleep 1
else
    echo "Agent server running ! Try connect on 8090 port.."

fi

/usr/lib64/agentdvr-viewer/AgentDVR

