#!/bin/bash
# Install NetXMS agent on macOS — connects to monitor.softsol.co.za via tunnel.
# Matches Windows silent install: /SERVER=monitor.softsol.co.za /TUNNEL
# Run as administrator: sudo ./install_nxagent_macos.sh
set -euo pipefail

INSTALL_PREFIX="/opt/netxms"
CONF_DIR="${INSTALL_PREFIX}/etc"
CONF_FILE="${CONF_DIR}/nxagentd.conf"
PLIST_LABEL="org.netxms.nxagentd"
PLIST_PATH="/Library/LaunchDaemons/${PLIST_LABEL}.plist"

# SoftSol defaults (override with env if needed)
SOFTSOL_SERVER="${SOFTSOL_SERVER:-monitor.softsol.co.za}"
SOFTSOL_TUNNEL="${SOFTSOL_TUNNEL:-yes}"

usage() {
  cat <<EOF
Usage: sudo ./install_nxagent_macos.sh [path-to-extracted-package-dir]

Installs nxagentd and configures it for SoftSol monitoring automatically:
  Server: ${SOFTSOL_SERVER}
  Tunnel: ${SOFTSOL_TUNNEL}

Apple Silicon: nxagent-*-macos-arm64.tar.gz
Intel Mac:     nxagent-*-macos-x64.tar.gz
EOF
}

write_agent_config() {
  mkdir -p "${CONF_DIR}" "${INSTALL_PREFIX}/var"
  cat >"${CONF_FILE}" <<EOF
# NetXMS agent — SoftSol monitoring (auto-generated by install_nxagent_macos.sh)
LogFile = /var/log/nxagentd.log
FileStore = ${INSTALL_PREFIX}/var
MasterServers = ${SOFTSOL_SERVER}
EOF
  if [[ "${SOFTSOL_TUNNEL}" == "yes" ]]; then
    echo "ServerConnection = ${SOFTSOL_SERVER}" >>"${CONF_FILE}"
  fi
  cat >>"${CONF_FILE}" <<'EOF'
SubAgent = darwin
SubAgent = ping
SubAgent = ssh
EOF
  chmod 644 "${CONF_FILE}"
}

if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
  echo "Run as root: sudo $0" >&2
  exit 1
fi

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
SOURCE_DIR="${1:-${SCRIPT_DIR}}"

if [[ ! -d "${SOURCE_DIR}/netxms-macos-agent/bin" ]]; then
  echo "Cannot find netxms-macos-agent/bin under: ${SOURCE_DIR}" >&2
  usage
  exit 1
fi

echo "Installing NetXMS agent to ${INSTALL_PREFIX} ..."
mkdir -p "${INSTALL_PREFIX}"
if command -v rsync >/dev/null 2>&1; then
  rsync -a "${SOURCE_DIR}/netxms-macos-agent/" "${INSTALL_PREFIX}/"
else
  cp -R "${SOURCE_DIR}/netxms-macos-agent/." "${INSTALL_PREFIX}/"
fi

# Always write SoftSol server/tunnel config (same as Windows silent install)
write_agent_config
echo "Configured agent for server: ${SOFTSOL_SERVER} (tunnel: ${SOFTSOL_TUNNEL})"

chmod 755 "${INSTALL_PREFIX}/bin/nxagentd" 2>/dev/null || true
mkdir -p /var/log
touch /var/log/nxagentd.log /var/log/nxagentd.stdout.log /var/log/nxagentd.stderr.log
chmod 644 /var/log/nxagentd.log /var/log/nxagentd.stdout.log /var/log/nxagentd.stderr.log

cp "${SOURCE_DIR}/org.netxms.nxagentd.plist" "${PLIST_PATH}"
chmod 644 "${PLIST_PATH}"

launchctl bootout system "${PLIST_PATH}" 2>/dev/null || true
launchctl bootstrap system "${PLIST_PATH}"
launchctl enable "system/${PLIST_LABEL}" 2>/dev/null || true
launchctl kickstart -k "system/${PLIST_LABEL}" 2>/dev/null || launchctl start "${PLIST_LABEL}" 2>/dev/null || true

echo ""
echo "Install complete."
echo "  Server:  ${SOFTSOL_SERVER}"
echo "  Tunnel:  ${SOFTSOL_TUNNEL}"
echo "  Config:  ${CONF_FILE}"
echo "  Binary:  ${INSTALL_PREFIX}/bin/nxagentd"
echo "  Logs:    /var/log/nxagentd.log"
if [[ "${SOFTSOL_TUNNEL}" == "yes" ]]; then
  echo ""
  echo "Next: bind this Mac in NetXMS → Configuration → Agent Tunnel Manager."
fi
