version: '2.2'
services:
  # 1. Counter-Strike 1.6 gameserver sends UDP logs to the daemon
  # See: https://github.com/startersclan/docker-sourceservers
  cstrike:
    image: goldsourceservers/cstrike:latest
    ports:
      - 27015:27015/udp
    networks:
      default:
        ipv4_address: 10.5.0.100  # Static IP
    stdin_open: true
    tty: true
    stop_signal: SIGKILL
    entrypoint:
      - /bin/bash
    command:
      - -c
      - |
          set -eu
          exec hlds_linux -console -noipx -secure -game cstrike +map de_dust2 +maxplayers 32 +sv_lan 0 +ip 0.0.0.0 +port 27015 +rcon_password password +log on +logaddress_add 10.5.0.50 27500

  # Uncomment to test CS2     
  # 1. Counter-Strike 2 gameserver sends HTTP logs to source-udp-forwarder
  # See: https://github.com/startersclan/docker-sourceservers
  # cs2:
  #   image: sourceservers/cs2:latest
  #   ports:
  #     - 27016:27016/tcp
  #     - 27016:27016/udp
  #   networks:
  #     default:
  #       ipv4_address: 10.5.0.101  # Static IP
  #   stdin_open: true
  #   tty: true
  #   stop_signal: SIGKILL
  #   depends_on:
  #     - source-udp-forwarder-cs2
  #   entrypoint:
  #     - /bin/bash
  #   command:
  #     - -c
  #     - |
  #         set -eu
  #         exec game/bin/linuxsteamrt64/cs2 -dedicated -port 27016 +game_type 0 +game_mode 1 +mapgroup mg_active +map de_dust2 +rcon_password password +log on +logaddress_add_http "http://source-udp-forwarder-cs2:26999"

  # Uncomment to test CS2     
  # 2. source-udp-forwarder proxy forwards gameserver logs to the daemon
  # See: https://github.com/startersclan/source-udp-forwarder
  # source-udp-forwarder-cs2:
  #   image: startersclan/source-udp-forwarder:latest
  #   environment:
  #     - LISTEN_ADDR=:26999
  #     - UDP_FORWARD_ADDR=daemon:27500
  #     - FORWARD_PROXY_KEY=somedaemonsecret # The daemon's proxy_key secret
  #     - FORWARD_GAMESERVER_IP=10.5.0.101 # The gameserver's IP as registered in the HLStatsX:CE database
  #     - FORWARD_GAMESERVER_PORT=27016 # The gameserver's IP as registered in the HLStatsX:CE database
  #     - LOG_LEVEL=INFO
  #     - LOG_FORMAT=txt
  #   networks:
  #     - default
  #   depends_on:
  #     - daemon

  # 3. HLStatsX:CE perl daemon accepts the gameserver logs. Gameserver Logs are parsed and stats are recorded
  # The daemon's proxy_key secret can only be setup in the HLStatsX:CE Web Admin Panel Settings under 'Proxy Settings' section
  daemon:
    build:
      dockerfile: Dockerfile.daemon
      context: .
      target: dev
    ports:
      - 27500:27500/udp   # For external servers to send logs to the daemon
    networks:
      default:
        ipv4_address: 10.5.0.50  # Static IP
    depends_on:
      - db
    command:
      - --ip=0.0.0.0
      - --port=27500
      - --db-host=db:3306
      - --db-name=hlstatsxce
      - --db-username=hlstatsxce
      - --db-password=hlstatsxce
      - --nodns-resolveip
      - --debug
      # - --debug
      # - --help

  # Cron - awards
  awards:
    build:
      dockerfile: Dockerfile.daemon
      context: .
      target: dev
    stop_signal: SIGKILL
    entrypoint:
      - /bin/sh
    command:
      - -c
      - |
          set -eu

          echo "Creating /awards.sh"
          cat - > /awards.sh <<'EOF'
          #!/bin/sh
          set -eu
          cd /scripts
          perl hlstats-awards.pl --db-host=db:3306 --db-name=hlstatsxce --db-username=hlstatsxce --db-password=hlstatsxce #--help
          EOF
          chmod +x /awards.sh

          # Run at 00:00 daily. To customize your cron schedule, use https://crontab.guru
          echo "Creating crontab"
          crontab - <<'EOF'
          PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
          0 0 * * * /awards.sh > /proc/1/fd/1 2>/proc/1/fd/2
          EOF
          crontab -l

          echo "Running cron"
          cron -f

  # 4. HLStatsX:CE DB
  db:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=root    # Username 'root', password 'root'
      - MYSQL_USER=hlstatsxce
      - MYSQL_PASSWORD=hlstatsxce
      - MYSQL_DATABASE=hlstatsxce
    volumes:
      - db-volume:/var/lib/mysql
      - ./src/sql/install.sql:/docker-entrypoint-initdb.d/01-install.sql:ro  # This seeds the DB only on the first time
    networks:
      - default

  # 5. HLStatsX:CE web
  # Available at http://localhost:8081
  # Admin Panel username: admin, password: 123456
  web:
    build:
      dockerfile: Dockerfile.web
      context: .
      target: dev
    volumes:
      - ./src/web:/web
      # - ./config/web/supervisor.conf:/supervisor.conf:ro
      # - ./config/web/etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      # - ./config/web/usr/local/etc/php/conf.d/php.ini:/usr/local/etc/php/conf.d/php.ini:ro
      # - ./config/web/usr/local/etc/php-fpm.d/php-fpm.conf:/usr/local/etc/php-fpm.d/php-fpm.conf:ro
    environment:
      # - XDEBUG_MODE=off   # Uncomment to disable xdebug
      - DB_ADDR=db
      - DB_NAME=hlstatsxce
      - DB_USER=hlstatsxce
      - DB_PASS=hlstatsxce
      - DB_DEBUG=0
    ports:
      - 8081:80
      - 9000
    networks:
      - default
    extra_hosts:
      # For xdebug to reach the host via `host.docker.internal`. See: https://github.com/moby/moby/pull/40007#issuecomment-578729356 and https://stackoverflow.com/questions/49907308/installing-xdebug-in-docker
      # If xdebug does not work, you may need to add an iptables rule to the INPUT chain: iptables -A INPUT -i br+ -j ACCEPT
      - host.docker.internal:host-gateway
    depends_on:
      - init-container

  # Cron - Heatmaps
  heatmaps:
    build:
      dockerfile: Dockerfile.web
      context: .
      target: dev
    volumes:
      - ./src/heatmaps:/heatmaps
      - ./src/web:/web
    environment:
      # - XDEBUG_MODE=off   # Uncomment to disable xdebug
      - DB_HOST=db
      - DB_NAME=hlstatsxce
      - DB_USER=hlstatsxce
      - DB_PASS=hlstatsxce
      - HLXCE_WEB=/web
      - HUD_URL=http://www.hlxcommunity.com
      - OUTPUT_SIZE=medium
      - DEBUG=1
    extra_hosts:
      # For xdebug to reach the host via `host.docker.internal`. See: https://github.com/moby/moby/pull/40007#issuecomment-578729356 and https://stackoverflow.com/questions/49907308/installing-xdebug-in-docker
      # If xdebug does not work, you may need to add an iptables rule to the INPUT chain: iptables -A INPUT -i br+ -j ACCEPT
      - host.docker.internal:host-gateway
    depends_on:
      - init-container
      - web
    working_dir: /heatmaps
    stop_signal: SIGKILL
    entrypoint:
      - /bin/sh
    command:
      - -c
      - |
          set -eu

          # Run at 00:00 daily. To customize your cron schedule, use https://crontab.guru
          echo "Creating crontab"
          crontab - <<'EOF'
          0 0 * * * php /heatmaps/generate.php > /proc/1/fd/1 2>/proc/1/fd/2
          EOF
          crontab -l

          echo "Running crond"
          exec crond -f

  # PHPMyAdmin to manage DB
  # Available at http://localhost:8083
  phpmyadmin:
    image: phpmyadmin:5.2
    environment:
      - PMA_HOST=db
    ports:
      - 8083:80
    networks:
      - default

  # Init container to set permissions in mounted folders and volumes
  init-container:
    image: alpine:latest
    volumes:
      - ./src/web:/web
      - db-volume:/var/lib/mysql
    networks:
      - default
    entrypoint:
      - /bin/sh
    command:
      - -c
      - |
          set -eu

          echo "Granting web nginx and php read permissions"
          find /web -type d -exec chmod 755 {} \;
          find /web -type f -exec chmod 644 {} \;

          echo "Granting web php write permissions"
          chmod 777 /web/hlstatsimg/progress
          chmod 777 /web/hlstatsimg/graph

          echo "Granting db write permissions"
          chown -R 999:999 /var/lib/mysql

networks:
  default:
    driver: bridge
    ipam:
     config:
       - subnet: 10.5.0.0/16
         gateway: 10.5.0.1

volumes:
  db-volume:
