#!/usr/bin/env bash
set -euo pipefail

# Deploy NetDisco on tsys-librenms
# Part of OAM platform [#337]
# L2 network discovery and mapping via SNMP

echo "=== Setting up NetDisco ==="

mkdir -p /opt/oam/netdisco/{config,data,pgdata}

# Write deployment config
cat > /opt/oam/netdisco/config/deployment.yml <<'YMLEOF'
# NetDisco deployment configuration [#337]
# SNMP community: kn3lmgmt (v2c)

database:
  host: netdisco-postgresql
  dbname: netdisco
  user: netdisco
  pass: netdisco

snmp_auth:
  - tag: default_v2c
    community: kn3lmgmt
    read: true
    write: false
    version: 2

  - tag: default_v1
    community: kn3lmgmt
    read: true
    write: false
    version: 1

schedule:
  discoverall:
    first: 60
    every: 3600
  macwalk:
    first: 120
    every: 900
  arpwalk:
    first: 150
    every: 900
  nbtwalk:
    first: 180
    every: 900

node_freshness: 0
YMLEOF

# Write docker-compose
cat > /opt/oam/netdisco/docker-compose.yml <<'DCEOF'
services:
  netdisco-postgresql:
    image: netdisco/netdisco:2.102001-postgresql
    container_name: oam-netdisco-db
    environment:
      - POSTGRES_USER=netdisco
      - POSTGRES_PASSWORD=netdisco
      - POSTGRES_DB=netdisco
    volumes:
      - /opt/oam/netdisco/pgdata:/var/lib/postgresql/data
    restart: unless-stopped

  netdisco-backend:
    image: netdisco/netdisco:2.102001-backend
    container_name: oam-netdisco-backend
    depends_on:
      - netdisco-postgresql
    environment:
      - NETDISCO_DB_HOST=netdisco-postgresql
      - NETDISCO_DB_NAME=netdisco
      - NETDISCO_DB_USER=netdisco
      - NETDISCO_DB_PASS=netdisco
    volumes:
      - /opt/oam/netdisco/config/deployment.yml:/home/netdisco/environments/deployment.yml
      - /opt/oam/netdisco/data:/home/netdisco/netdisco-sqlite
    restart: unless-stopped

  netdisco-web:
    image: netdisco/netdisco:2.102001-web
    container_name: oam-netdisco-web
    depends_on:
      - netdisco-postgresql
      - netdisco-backend
    environment:
      - NETDISCO_DB_HOST=netdisco-postgresql
      - NETDISCO_DB_NAME=netdisco
      - NETDISCO_DB_USER=netdisco
      - NETDISCO_DB_PASS=netdisco
    ports:
      - "8082:5000"
    volumes:
      - /opt/oam/netdisco/config/deployment.yml:/home/netdisco/environments/deployment.yml
    restart: unless-stopped
DCEOF

echo "=== Pulling NetDisco images ==="
cd /opt/oam/netdisco && docker compose pull 2>&1

echo "=== Starting NetDisco stack ==="
cd /opt/oam/netdisco && docker compose up -d 2>&1

echo "=== Waiting for PostgreSQL to initialize (30s) ==="
sleep 30

echo "=== Container status ==="
docker ps --filter name=oam-netdisco --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"

echo "=== Adding devices to NetDisco ==="
for dev in pfv-r5-core-01.knel.net pfv-r3-tor-mgmt-01.knel.net pfv-r3-tor-stor-01.knel.net pfv-r6-mgmt-01.knel.net; do
  echo "--- Discovering $dev ---"
  docker exec oam-netdisco-backend /home/netdisco/bin/netdisco-do discover -d "$dev" 2>&1 | tail -5
done

echo "=== NetDisco web URL: http://tsys-librenms.knel.net:8082 ==="
echo "=== Done ==="
