#!/usr/bin/env bash
# ----------------------------------------------------------------------
# Minimal bootstrap: update apt sources and install/configure Zabbix 7.0
# ----------------------------------------------------------------------
export DEBIAN_FRONTEND=noninteractive
set -euo pipefail

# Create /dos directory for logging
mkdir -p /dos

# ── Zabbix repo & key ─────────────────────────────────────────────
wget -qO- https://repo.zabbix.com/zabbix/7.0/debian/pool/main/z/zabbix-release/zabbix-release_7.0-2+debian12_all.deb \
    > /tmp/zabbix-release.deb
dpkg -i /tmp/zabbix-release.deb
apt-get update
rm /tmp/zabbix-release.deb

# ── Upgrading base packages ───────────────────────────────────────
apt-get -y dist-upgrade

# ── Installing Zabbix proxy + agent ────────────────────────────────
apt-get -y install zabbix-proxy-sqlite3 zabbix-sql-scripts zabbix-agent2 jq

# ── Zabbix variables ─────────────────────────────────────────────
ZABBIX_HOST_UUID=$(uuidgen)
ZABBIX_AGENT_UUID=$(uuidgen)
ZABBIX_PSK_UUID=$(uuidgen)
ZABBIX_PSK=$(openssl rand -hex 32) 
LOCAL_ADDRESS=$(grep -oP '(?<=(domain|search)\s).*' /etc/resolv.conf | head -n 1)

# ── Create Databse ───────────────────────────────────────────────
mkdir -p /etc/zabbix/db
cat /usr/share/zabbix-sql-scripts/sqlite3/proxy.sql | sqlite3 /etc/zabbix/db/zabbix.db
chown zabbix:zabbix /etc/zabbix/db -R

# ── Create PSK ─────────────────────────────────────────────────
echo $ZABBIX_PSK > /etc/zabbix/zabbix.psk
chmod 400 /etc/zabbix/zabbix.psk
chown zabbix:zabbix /etc/zabbix/zabbix.psk

# ── Edit Proxy Configuration ────────────────────────────────────
# ── Server settings ─────────────────────────────────────────────
sed -i "s/^Server=127\.0\.0\.1/Server=zabbix.digital-origin.co.uk/g" /etc/zabbix/zabbix_proxy.conf
# ── Hostname ───────────────────────────────────────────────────
sed -i "s/^Hostname=Zabbix proxy/Hostname=$ZABBIX_HOST_UUID/g" /etc/zabbix/zabbix_proxy.conf
# ── DBName ─────────────────────────────────────────────────────
sed -i "s/^DBName=zabbix_proxy/DBName=\/etc\/zabbix\/db\/zabbix\.db/g" /etc/zabbix/zabbix_proxy.conf
# ── TLSConnect ─────────────────────────────────────────────────
sed -i "s/^# TLSConnect=unencrypted/TLSConnect=psk/g" /etc/zabbix/zabbix_proxy.conf
# ── TLSPSKIdentity ─────────────────────────────────────────────
sed -i "s/^# TLSPSKIdentity=/TLSPSKIdentity=$ZABBIX_PSK_UUID/g" /etc/zabbix/zabbix_proxy.conf
# ── TLSPSKFile ─────────────────────────────────────────────────
sed -i "s/^# TLSPSKFile=/TLSPSKFile=\/etc\/zabbix\/zabbix\.psk/g" /etc/zabbix/zabbix_proxy.conf

# ── Edit Agent Config ──────────────────────────────────────────
sed -i "s/^Hostname=Zabbix server/Hostname=$ZABBIX_AGENT_UUID/g" /etc/zabbix/zabbix_agent2.conf

# ── Enable service ─────────────────────────────────────────────
systemctl enable --now zabbix-proxy
systemctl restart zabbix-agent2

# ── Register with Zabbix server ─────────────────────────────────
if [ -z "$1" ]; then
  echo "Error: Zabbix API credentials not provided as an argument." >> /dos/log
  exit 1
fi
ZABBIX_API_CREDENTIALS=$(echo "$1" | base64 -d)

echo "Zabbix API credentials received." >> /dos/zabbixcredentials.log

ZABBIX_API_KEY_RESPONSE=$(curl --location 'https://zabbix.digital-origin.co.uk/api_jsonrpc.php' \
--header 'Content-Type: application/json' \
--data "{
    \"id\": 1,
    \"jsonrpc\": \"2.0\",
    \"method\": \"user.login\",
    \"params\": {
        \"username\": \"$(expr "$ZABBIX_API_CREDENTIALS" : '\(.*\):.*')\",
        \"password\": \"$(expr "$ZABBIX_API_CREDENTIALS" : '.*:\(.*\)')\"
    }
}") >> /dos/zabbixcurl.log 2>&1
echo $ZABBIX_API_KEY_RESPONSE >> /dos/zabbixcurl.log
ZABBIX_API_KEY=$(echo $ZABBIX_API_KEY_RESPONSE | jq -r '.result') >> /dos/zabbixcurl.log 2>&1

echo $ZABBIX_API_KEY >> /dos/zabbixcurl.log

curl --location 'https://zabbix.digital-origin.co.uk/api_jsonrpc.php' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $ZABBIX_API_KEY" \
--data "{
    \"id\": 1,
    \"jsonrpc\": \"2.0\",
    \"method\": \"proxy.create\",
    \"params\": {
        \"name\": \"$ZABBIX_HOST_UUID\",
        \"operating_mode\": 0,
        \"description\": \"[customer:Someone]\r\n[local_address:zabbixmonitor.$LOCAL_ADDRESS]\",
        \"tls_accept\": 2,
        \"tls_psk_identity\": \"$ZABBIX_PSK_UUID\",
        \"tls_psk\": \"$ZABBIX_PSK\"
    }
}" >> /dos/zabbixcurl.log 2>&1