#!/usr/bin/env bash

run_as_root()
{
    if [ "$(id -u)" -eq 0 ]; then
        "$@"
    else
        sudo "$@"
    fi
}

isOtelCollectorServiceInstalled()
{
    local name_prefix=""
    if [[ -z "$INSTALLATION_NAME" && -f "${AM_HOME}/config/installation.name" ]]; then
        INSTALLATION_NAME="$(cat "${AM_HOME}/config/installation.name" 2>/dev/null | tr -d '[:space:]')"
    fi
    if [[ -n "$INSTALLATION_NAME" ]]; then
        name_prefix="${INSTALLATION_NAME}-"
    fi
    local unit="/lib/systemd/system/${name_prefix}am-otel-collector.service"
    [[ -e "$unit" ]]
    return $?
}

isDirectoryWatcherServiceInstalled()
{
    local name_prefix=""
    if [[ -z "$INSTALLATION_NAME" && -f "${AM_HOME}/config/installation.name" ]]; then
        INSTALLATION_NAME="$(cat "${AM_HOME}/config/installation.name" 2>/dev/null | tr -d '[:space:]')"
    fi
    if [[ -n "$INSTALLATION_NAME" ]]; then
        name_prefix="${INSTALLATION_NAME}-"
    fi
    local unit="/lib/systemd/system/${name_prefix}am-directory-watcher.service"
    [[ -e "$unit" ]]
    return $?
}