#!/bin/bash

get_osflavor(){
    if [[ -f "/etc/redhat-release" ]]
        then
            echo "rpm"
        elif [[ -f "/etc/lsb-release" ]] && [[ ! -d "/etc/susehelp.d" ]]
        then
            echo "ubuntu"
        elif [[ -f "/etc/debian_version" ]]
        then
            echo "debian"
	elif [[ -f "/etc/SuSE-release" ]] || [[ -d "/etc/susehelp.d" ]]
        then
            echo "suse"
        else
            echo "ERROR: Cannot get the system type. Exiting."
            exit 1
    fi
}

here="${0%/*}"  # or you can use `dirname "$0"`

argument="-agent";

LD_LIBRARY_PATH="$here"/lib-sc-agent:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
export QT_QPA_PLATFORM_PLUGIN_PATH="$here"/lib-sc-agent/plugins/platforms
# on uhnntu/debian style os the agent suppose to be called before the x-server is on, so we need to wait to the x-server to be running before the agent starts
if [[ $(get_osflavor) != "rpm" ]] && [[ $(get_osflavor) != "suse" ]]
then
	"$here"/wait_to_xserver.sh
else
	"$here"/wait_to_xorg.sh
fi

exec "$here"/ForeScoutSecureConnector.bin "$argument" &>/dev/null
