#!/usr/bin/env bash

set -e

#----------------------------------------------------------------------------
# User configuration
#----------------------------------------------------------------------------
GHOST_ACCOUNT_ID=""
GHOST_CLIENT_USER=""
GHOST_CLIENT_SECRET=""
GHOST_PARENT_GATEWAY_URL=""
GHOST_ENVIRONMENT_URL=https://gwp.ghostnodes.com

#----------------------------------------------------------------------------
# Internal setup code
#----------------------------------------------------------------------------
# --- Colors ---
RED="\033[0;31m"
GREEN="\033[0;32m"
CYAN="\033[0;36m"
YELLOW="\033[1;33m"
NC="\033[0m" # No Color / reset

info()    { echo -e "${CYAN}➤ $1${NC}"; }
success() { echo -e "${GREEN}✔ $1${NC}"; }
warn()    { echo -e "${YELLOW}⚠ $1${NC}"; }
error()   { echo -e "${RED}✖ $1${NC}"; }

#----------------------------------------------------------------------------
# Validate required variables
#----------------------------------------------------------------------------
if [ -z "$GHOST_ACCOUNT_ID" ] || [ -z "$GHOST_CLIENT_USER" ] || [ -z "$GHOST_CLIENT_SECRET" ]; then
    error "One or more required configuration variables are empty:"
    echo "  GHOST_ACCOUNT_ID=\"$GHOST_ACCOUNT_ID\""
    echo "  GHOST_CLIENT_USER=\"$GHOST_CLIENT_USER\""
    echo "  GHOST_CLIENT_SECRET=\"********\""
    echo
    error "Please fill in all required values at the top of the script."
    exit 1
fi

#----------------------------------------------------------------------------
# Install .NET
#----------------------------------------------------------------------------
info "Checking .NET installation..."

if dotnet --info >/dev/null 2>&1; then
    version=$(dotnet --info | grep 'Version:' | head -n 1 | awk '{print $2}')

    # Extract major version
    major=$(echo "$version" | cut -d'.' -f1)

    if [ "$major" -ge 8 ]; then
        success ".NET runtime installed (version $version)"
    else
        warn "Detected .NET version $version, but 8.0 or higher is required."
        info "Installing .NET 8 runtime and SDK..."

        apt-get update -y >/dev/null
        apt-get install -y dotnet-sdk-8.0 dotnet-runtime-8.0 aspnetcore-runtime-8.0 >/dev/null

        success ".NET installation completed."
    fi
else
    info "No .NET installation found. Installing .NET 8 runtime and SDK..."

    apt-get update -y >/dev/null
    apt-get install -y dotnet-sdk-8.0 dotnet-runtime-8.0 aspnetcore-runtime-8.0 >/dev/null

    success ".NET installation completed."
fi

#----------------------------------------------------------------------------
# Symlink to dotnet
#----------------------------------------------------------------------------
if [ -f /usr/share/dotnet/dotnet ]; then
    DOTNET_PATH=/usr/share/dotnet/dotnet
elif [ -f /usr/lib/dotnet/dotnet ]; then
    DOTNET_PATH=/usr/lib/dotnet/dotnet
else
    # Try environment PATH (Docker-friendly)
    DOTNET_PATH=$(command -v dotnet 2>/dev/null || true)
fi

# Create symlink if found
if [ -n "$DOTNET_PATH" ]; then
    ln -sf "$DOTNET_PATH" /usr/bin/dotnet
else
    warn "Dotnet executable not found. Symlink creation skipped."
fi

ln -sf /etc/machine-id /var/lib/dbus/machine-id

#----------------------------------------------------------------------------
# Download Ghost installer
#----------------------------------------------------------------------------
info "Downloading latest Ghost installer..."
mkdir -p /usr/share/GhostLabs/Downloads
cd /usr/share/GhostLabs/Downloads

wget -q -O GhostCoreSetup.dll \
  https://ghostnodes.blob.core.windows.net/ghost-app-download/GhostCoreSetup/.net8/GhostCoreSetup.dll

wget -q -O GhostCoreSetup.runtimeconfig.json \
  https://ghostnodes.blob.core.windows.net/ghost-app-download/GhostCoreSetup/.net8/GhostCoreSetup.runtimeconfig.json

success "Ghost installer updated."

#----------------------------------------------------------------------------
# Install Ghost components
#----------------------------------------------------------------------------
info "Installing Ghost Agent..."
if dotnet GhostCoreSetup.dll -a -c:$GHOST_ACCOUNT_ID -p:$GHOST_PARENT_GATEWAY_URL -ci:true -al:true -cid:$GHOST_CLIENT_USER -cs:$GHOST_CLIENT_SECRET -eu:$GHOST_ENVIRONMENT_URL >/dev/null 2>&1; then
    success "Agent installation completed."
else
    error "Agent installation failed."
    exit 1
fi

info "Installing Ghost Monitor..."
if dotnet GhostCoreSetup.dll -m -c:$GHOST_ACCOUNT_ID -p:$GHOST_PARENT_GATEWAY_URL -ci:true -al:true -cid:$GHOST_CLIENT_USER -cs:$GHOST_CLIENT_SECRET -eu:$GHOST_ENVIRONMENT_URL >/dev/null 2>&1; then
    success "Monitor installation completed."
else
    error "Monitor installation failed."
    exit 1
fi

# Create folder for logs
mkdir -p /usr/share/GhostLabs/tempfiles/logs 2>/dev/null

success "Installation completed."
