#!/bin/bash
# Haven Blueshore — Zabbix agent 6.4.21 install on macOS (amd64 tarball;
# runs on Apple Silicon via Rosetta 2 — no arm64 tarball is published).
# Pre-configured for PSK auto-registration onto mon.havenblueshore.cloud.
# Hostname is NOT set: the agent reports system.hostname.
# Run as root (sudo) from the folder containing this script and the tarball.
set -euo pipefail
cd "$(dirname "$0")"
[ "$(id -u)" = 0 ] || { echo "Run with sudo"; exit 1; }

set_conf() { # file key value
  local f="$1" k="$2" v="$3"
  if grep -qE "^[# ]*$k=" "$f"; then
    sed -i '' -E "s|^[# ]*$k=.*|$k=$v|" "$f"
  else
    printf '%s=%s\n' "$k" "$v" >> "$f"
  fi
}

mkdir -p /tmp/zbx-extract /usr/local/zabbix/sbin /usr/local/zabbix/bin /usr/local/zabbix/etc
tar -xzf zabbix_agent-6.4.21-macos-amd64-openssl.tar.gz -C /tmp/zbx-extract
AGENTD="$(find /tmp/zbx-extract -name zabbix_agentd -type f | head -1)"
[ -n "$AGENTD" ] || { echo "zabbix_agentd not found in tarball"; exit 1; }
cp "$AGENTD" /usr/local/zabbix/sbin/
find /tmp/zbx-extract -name 'zabbix_get'    -type f -exec cp {} /usr/local/zabbix/bin/ \; 2>/dev/null || true
find /tmp/zbx-extract -name 'zabbix_sender' -type f -exec cp {} /usr/local/zabbix/bin/ \; 2>/dev/null || true
find /tmp/zbx-extract -name 'zabbix_agentd.conf' | head -1 | xargs -I{} cp {} /usr/local/zabbix/etc/zabbix_agentd.conf

CONF=/usr/local/zabbix/etc/zabbix_agentd.conf
PSKFILE=/usr/local/zabbix/etc/zabbix_agentd.psk
printf '%s' 'cd1a785ee4257e33de073c4cfe6b1dde555d2ed9e22d80215313b3fcf49ed742' > "$PSKFILE"
chmod 600 "$PSKFILE"
set_conf "$CONF" Server            "mon.havenblueshore.cloud"
set_conf "$CONF" ServerActive      "mon.havenblueshore.cloud:10051"
set_conf "$CONF" HostMetadata      "macOS"
set_conf "$CONF" TLSConnect        "psk"
set_conf "$CONF" TLSAccept         "psk"
set_conf "$CONF" TLSPSKIdentity    "havenblueshore-autoreg"
set_conf "$CONF" TLSPSKFile        "$PSKFILE"
set_conf "$CONF" PidFile           "/tmp/zabbix_agentd.pid"
set_conf "$CONF" LogFile           "/tmp/zabbix_agentd.log"

cat > /Library/LaunchDaemons/com.zabbix.zabbix_agentd.plist <<'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key><string>com.zabbix.zabbix_agentd</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/zabbix/sbin/zabbix_agentd</string>
    <string>-c</string>
    <string>/usr/local/zabbix/etc/zabbix_agentd.conf</string>
    <string>-f</string>
  </array>
  <key>RunAtLoad</key><true/>
  <key>KeepAlive</key><true/>
  <key>StandardOutPath</key><string>/tmp/zabbix_agentd.log</string>
  <key>StandardErrorPath</key><string>/tmp/zabbix_agentd.log</string>
</dict>
</plist>
PLIST
launchctl bootout system /Library/LaunchDaemons/com.zabbix.zabbix_agentd.plist 2>/dev/null || true
launchctl bootstrap system /Library/LaunchDaemons/com.zabbix.zabbix_agentd.plist
sleep 2
pgrep -x zabbix_agentd >/dev/null && echo "Zabbix Agent 6.4.21 installed and running (PSK auto-registration to mon.havenblueshore.cloud:10051)."
