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

echo ">> HX-KI Falkenstein Layer-3 Setup startet..."

BASE_DIR="/opt/hx-ki"

echo ">> Verzeichnisse anlegen..."
mkdir -p "${BASE_DIR}/syncthing/config"
mkdir -p "${BASE_DIR}/syncthing/data"
mkdir -p "${BASE_DIR}/postres" || true
mkdir -p "${BASE_DIR}/postgres"
mkdir -p "${BASE_DIR}/web"
mkdir -p "${BASE_DIR}/mautic/db"
mkdir -p "${BASE_DIR}/mautic/app"
mkdir -p "${BASE_DIR}/archive"
mkdir -p "${BASE_DIR}/grafana"

echo ">> Docker Netzwerk hxki-internal anlegen (falls nötig)..."
docker network create hxki-internal || true

COMPOSE_FILE="${BASE_DIR}/docker/docker-compose.yml"
mkdir -p "${BASE_DIR}/docker"

echo ">> docker-compose.yml schreiben nach ${COMPOSE_FILE}..."

cat > "${COMPOSE_FILE}" << 'EOF'
version: "3.9"

services:
  syncthing:
    image: lscr.io/linuxserver/syncthing:latest
    container_name: hxki-syncthing
    restart: unless-stopped
    networks:
      - hxki-internal
    volumes:
      - /opt/hx-ki/syncthing/config:/config
      - /opt/hx-ki/syncthing/data:/data
    ports:
      - "8384:8384"
      - "22000:22000/tcp"
      - "22000:22000/udp"
      - "21027:21027/udp"

  postgres:
    image: postgres:16
    container_name: hxki-postgres
    restart: unless-stopped
    networks:
      - hxki-internal
    environment:
      POSTGRES_USER: hxki
      POSTGRES_PASSWORD: supersecure
      POSTGRES_DB: hxki_roles
    volumes:
      - /opt/hx-ki/postgres:/var/lib/postgresql/data
    ports:
      - "5432:5432"

  n8n:
    image: docker.n8n.io/n8nio/n8n
    container_name: hxki-n8n
    restart: unless-stopped
    depends_on:
      - postgres
    networks:
      - hxki-internal
    environment:
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: postgres
      DB_POSTGRESDB_PORT: 5432
      DB_POSTGRESDB_USER: hxki
      DB_POSTGRESDB_PASSWORD: supersecure
      DB_POSTGRESDB_DATABASE: hxki_roles
      N8N_HOST: 0.0.0.0
      N8N_PORT: 5678
      N8N_PROTOCOL: http
    ports:
      - "5678:5678"

  web:
    image: nginx:latest
    container_name: hxki-web
    restart: unless-stopped
    networks:
      - hxki-internal
    volumes:
      - /opt/hx-ki/web:/usr/share/nginx/html:ro
    ports:
      - "80:80"

  mariadb:
    image: mariadb:10.11
    container_name: hxki-mariadb
    restart: unless-stopped
    networks:
      - hxki-internal
    environment:
      MYSQL_ROOT_PASSWORD: supersecure
      MYSQL_DATABASE: mautic
      MYSQL_USER: mautic
      MYSQL_PASSWORD: mauticpass
    volumes:
      - /opt/hx-ki/mautic/db:/var/lib/mysql
    ports:
      - "3306:3306"

  mautic:
    image: mautic/mautic:5-apache
    container_name: hxki-mautic
    restart: unless-stopped
    depends_on:
      - mariadb
    networks:
      - hxki-internal
    environment:
      MAUTIC_DB_HOST: mariadb
      MAUTIC_DB_USER: mautic
      MAUTIC_DB_PASSWORD: mauticpass
      MAUTIC_DB_NAME: mautic
      MAUTIC_TRUSTED_PROXIES: 0.0.0.0/0
    volumes:
      - /opt/hx-ki/mautic/app:/var/www/html
    ports:
      - "8080:80"

  gitea:
    image: gitea/gitea:latest
    container_name: hxki-gitea
    restart: unless-stopped
    networks:
      - hxki-internal
    volumes:
      - /opt/hx-ki/archive:/data
    ports:
      - "3000:3000"
      - "222:22"

  grafana:
    image: grafana/grafana-oss:latest
    container_name: hxki-grafana
    restart: unless-stopped
    networks:
      - hxki-internal
    volumes:
      - /opt/hx-ki/grafana:/var/lib/grafana
    ports:
      - "3001:3000"

networks:
  hxki-internal:
    external: true
EOF

echo ">> Stack starten (docker compose up -d)..."
cd "${BASE_DIR}/docker"
docker compose up -d

echo ">> HX-KI Falkenstein Layer-3 Setup: FERTIG."
docker ps
