#!/bin/sh
#
# Build OPNsense RMM Agent Plugin Package
#
# Usage: ./build.sh [version]
# Example: ./build.sh 1.0.0
#
# Prerequisites:
# - Go 1.24+ (for cross-compilation)
# - FreeBSD pkg tools (runs on the target OPNsense, or use pkg-create on FreeBSD)
#
# This script:
# 1. Cross-compiles the agent binary (freebsd/amd64)
# 2. Assembles the staging directory
# 3. Creates a +MANIFEST for pkg
# 4. Outputs a .tar that can be installed with `pkg add`

set -e

VERSION="${1:-1.0.0}"
NAME="os-rmm-agent"
STAGEDIR="$(pwd)/stage"
BUILDDIR="$(pwd)/../build"
PKGFILE="$(pwd)/${NAME}-${VERSION}.pkg"

echo "==> Building RMM Agent Plugin v${VERSION}"

# 1. Build agent binary
echo "==> Compiling agent (freebsd/amd64)..."
cd ../agent
GOOS=freebsd GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-s -w" -o "${BUILDDIR}/rmm-agent" .
cd ../opnsense-plugin
echo "    Binary: ${BUILDDIR}/rmm-agent ($(ls -lh ${BUILDDIR}/rmm-agent | awk '{print $5}'))"

# 2. Assemble staging directory
echo "==> Assembling staging directory..."
rm -rf "${STAGEDIR}"
mkdir -p "${STAGEDIR}/usr/local"

# Plugin files
cp -R src/etc "${STAGEDIR}/usr/local/"
cp -R src/opnsense "${STAGEDIR}/usr/local/"
cp -R src/usr/local/etc "${STAGEDIR}/usr/local/"
mkdir -p "${STAGEDIR}/usr/local/rmm"
cp "${BUILDDIR}/rmm-agent" "${STAGEDIR}/usr/local/rmm/rmm-agent"

# Set permissions
chmod +x "${STAGEDIR}/usr/local/rmm/rmm-agent"
chmod +x "${STAGEDIR}/usr/local/etc/rc.d/rmm_agent"
chmod +x "${STAGEDIR}/usr/local/opnsense/scripts/rmmagent/setup.sh"
chmod +x "${STAGEDIR}/usr/local/opnsense/scripts/OPNsense/RMMAgent/generate_config.php"

# 3. Create +COMPACT_MANIFEST and +MANIFEST (UCL format)
echo "==> Creating package manifest..."
cat > "${STAGEDIR}/+COMPACT_MANIFEST" << EOF
{
  "name": "${NAME}",
  "version": "${VERSION}",
  "origin": "sysutils/${NAME}",
  "comment": "RMM Agent for OPNsense",
  "maintainer": "cm@cynfo.com",
  "www": "https://git.cynfo.net/christian/rmm",
  "abi": "FreeBSD:14:amd64",
  "arch": "freebsd:14:x86:64",
  "prefix": "/usr/local",
  "desc": "RMM Agent for OPNsense - Remote Monitoring and Management. Configurable via WebGUI under Services > RMM Agent.",
  "categories": ["sysutils"]
}
EOF

# +MANIFEST = +COMPACT_MANIFEST plus files/scripts
cp "${STAGEDIR}/+COMPACT_MANIFEST" "${STAGEDIR}/+MANIFEST"

# Post-install script
cat > "${STAGEDIR}/+POST_INSTALL" << 'SCRIPT'
#!/bin/sh
/usr/local/etc/rc.d/opnsense-configd restart 2>/dev/null
echo ""
echo "====================================="
echo " RMM Agent Plugin installed."
echo " Configure: Services > RMM Agent"
echo "====================================="
SCRIPT

# Pre-deinstall script
cat > "${STAGEDIR}/+PRE_DEINSTALL" << 'SCRIPT'
#!/bin/sh
/usr/local/etc/rc.d/rmm_agent stop 2>/dev/null || true
/usr/sbin/sysrc -x rmm_agent_enable 2>/dev/null || true
SCRIPT

# Post-deinstall script
cat > "${STAGEDIR}/+POST_DEINSTALL" << 'SCRIPT'
#!/bin/sh
/usr/local/etc/rc.d/opnsense-configd restart 2>/dev/null
echo "RMM Agent Plugin removed."
SCRIPT

chmod +x "${STAGEDIR}/+POST_INSTALL" "${STAGEDIR}/+PRE_DEINSTALL" "${STAGEDIR}/+POST_DEINSTALL"

# 4. Create package using tar (FreeBSD txz pkg format)
echo "==> Creating package..."
cd "${STAGEDIR}"

# pkg expects: +COMPACT_MANIFEST, +MANIFEST at root, files under usr/
tar -cJf "${PKGFILE}" +COMPACT_MANIFEST +MANIFEST +POST_INSTALL +PRE_DEINSTALL +POST_DEINSTALL $(find usr -type f | sort)

cd ..
rm -rf "${STAGEDIR}"

echo ""
echo "==> Package erstellt: ${PKGFILE}"
echo "    Version: ${VERSION}"
echo "    Groesse: $(ls -lh ${PKGFILE} | awk '{print $5}')"
echo ""
echo "==> Installation auf OPNsense:"
echo "    scp ${PKGFILE} root@opnsense:/tmp/"
echo "    ssh root@opnsense 'pkg add /tmp/${NAME}-${VERSION}.pkg'"
echo ""
echo "==> Danach:"
echo "    - WebGUI oeffnen → Services → RMM Agent"
echo "    - Backend URL und API Key eintragen"
echo "    - Aktiviert ankreuzen → Speichern"
