version: '3.8'

services:
  # Base image with all build dependencies
  js8call-base:
    build:
      context: .
      dockerfile: Dockerfile.base
    image: js8call-base:ubuntu-24.04

  # Hamlib builder (depends on base)
  hamlib-builder:
    build:
      context: .
      dockerfile: Dockerfile.hamlib
      cache_from:
        - js8call-base:ubuntu-24.04
        - js8call-hamlib:latest
    image: js8call-hamlib:latest
    depends_on:
      - js8call-base

  # Main build service
  js8call-build:
    build:
      context: ..
      dockerfile: docker/Dockerfile
      target: output
      cache_from:
        - js8call-base:ubuntu-24.04
        - js8call-hamlib:latest
        - js8call-builder:ubuntu-24.04
      args:
        BUILDKIT_INLINE_CACHE: 1
    image: js8call-builder:ubuntu-24.04
    container_name: js8call-build
    volumes:
      - ./output:/output
      - ccache:/ccache
    environment:
      - CCACHE_DIR=/ccache
    command: |
      sh -c "cp /*.deb /*.AppImage /output/ 2>/dev/null || true"
    depends_on:
      - hamlib-builder

  # Development build with shell access
  js8call-dev:
    build:
      context: ..
      dockerfile: docker/Dockerfile
      target: development
      cache_from:
        - js8call-base:ubuntu-24.04
        - js8call-hamlib:latest
    image: js8call-dev:ubuntu-24.04
    container_name: js8call-dev
    volumes:
      - ..:/js8call-prefix/src
      - ./output:/output
      - ccache:/ccache
    environment:
      - CCACHE_DIR=/ccache
    working_dir: /js8call-prefix/build
    command: /bin/bash
    stdin_open: true
    tty: true
    depends_on:
      - hamlib-builder

  # Quick rebuild service (uses existing images)
  js8call-rebuild:
    build:
      context: ..
      dockerfile: docker/Dockerfile
      target: js8call-builder
      cache_from:
        - js8call-builder:ubuntu-24.04
    image: js8call-rebuild:latest
    volumes:
      - ccache:/ccache
    environment:
      - CCACHE_DIR=/ccache
    depends_on:
      - hamlib-builder

  # Runtime service for running JS8Call with GUI
  js8call-runtime:
    build:
      context: .
      dockerfile: Dockerfile.runtime
    image: js8call-runtime:ubuntu-24.04
    container_name: js8call-runtime
    environment:
      - DISPLAY=${DISPLAY}
      - XAUTHORITY=/tmp/.docker.xauth
      - PULSE_SERVER=unix:/tmp/pulse-socket
    volumes:
      - /tmp/.X11-unix:/tmp/.X11-unix:rw
      - /tmp/.docker.xauth:/tmp/.docker.xauth:rw
      - ./output/js8call-x86_64.AppImage:/opt/js8call/js8call.AppImage:ro
      - ~/.config/JS8Call:/home/js8call/.config/JS8Call
      # Audio - PulseAudio socket (adjust path as needed)
      - /run/user/1000/pulse/native:/tmp/pulse-socket
    devices:
      - /dev/dri
      - /dev/snd
    network_mode: host
    stdin_open: true
    tty: true

volumes:
  ccache:
    driver: local