services:
  tmserver:
    image: git.techniverse.net/scriptos/trackmania-server:1.3.0
    build:
      context: .
    container_name: tmserver
    restart: unless-stopped
    stop_grace_period: 30s
    depends_on:
      mariadb:
        condition: service_healthy
    ports:
      - "${SERVER_PORT:-2350}:${SERVER_PORT:-2350}/tcp"
      - "${SERVER_PORT:-2350}:${SERVER_PORT:-2350}/udp"
      - "${SERVER_P2P_PORT:-3450}:${SERVER_P2P_PORT:-3450}/tcp"
      - "80:80/tcp"
    env_file:
      - .env
    volumes:
      - ./data/gamedata:/opt/tmserver/GameData
      - ./data/controlpanel:/var/www/html
      - ./data/xaseco:/opt/tmserver/xaseco
    networks:
      tmserver_net:
        ipv4_address: 172.20.60.10

  mariadb:
    image: mariadb:11.4
    container_name: tmserver-mariadb
    restart: unless-stopped
    env_file:
      - .env
    environment:
      MYSQL_ROOT_PASSWORD: ${MARIADB_ROOT_PASSWORD}
      # RemoteCP-Datenbank (via MariaDB-Init)
      MYSQL_DATABASE: ${REMOTECP_DB_NAME:-remotecp}
      MYSQL_USER: ${REMOTECP_DB_USER:-remotecp}
      MYSQL_PASSWORD: ${REMOTECP_DB_PASSWORD}
      # XAseco-Datenbank (via Init-Script init-xaseco-db.sh)
      XASECO_DB_NAME: ${XASECO_DB_NAME:-xaseco}
      XASECO_DB_USER: ${XASECO_DB_USER:-xaseco}
      XASECO_DB_PASSWORD: ${XASECO_DB_PASSWORD}
    volumes:
      - ./data/mariadb:/var/lib/mysql
      - ./assets/db/init-xaseco-db.sh:/docker-entrypoint-initdb.d/20-init-xaseco-db.sh:ro
    healthcheck:
      test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    networks:
      tmserver_net:
        ipv4_address: 172.20.60.11

  ip-watcher:
    image: alpine:3.21
    container_name: tmserver-ip-watcher
    restart: unless-stopped
    depends_on:
      - tmserver
    command: ["/bin/sh", "/opt/WatchPublicIP.sh"]
    env_file:
      - .env
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./assets/bin/WatchPublicIP.sh:/opt/WatchPublicIP.sh:ro
    networks:
      tmserver_net:
        ipv4_address: 172.20.60.12

networks:
  tmserver_net:
    name: tmserver.dockernetwork.local
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.60.0/25
          gateway: 172.20.60.1
          ip_range: 172.20.60.64/26
