name: build-mailadler-linux-arm64

on:
  workflow_dispatch:
  push:
    branches: [main, master]
    paths:
      - 'src/**'
      - 'CMakeLists.txt'
      - '.github/workflows/build-linux_arm64.yml'
  pull_request:
    branches: [main, master]

env:
  QT_VERSION: 6.6.3

jobs:
  build:
    runs-on: [self-hosted, linux, ARM64]

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Install dependencies (first run only)
        run: |
          # Nur installieren wenn noch nicht vorhanden
          if ! command -v cmake &> /dev/null; then
            sudo apt-get update
            sudo apt-get install -y \
              build-essential \
              cmake \
              ninja-build \
              qt6-base-dev \
              qt6-websockets-dev \
              libgl1-mesa-dev \
              libxkbcommon-dev \
              libxcb-xinerama0-dev \
              libxcb-cursor0 \
              libsqlite3-dev \
              libssl-dev
          fi

      - name: Configure
        run: |
          cmake -B build -G Ninja \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_INSTALL_PREFIX=/usr \
            -DEXTERNAL_LAUNCHERS=OFF

      - name: Build
        run: cmake --build build --parallel

      - name: Prepare version
        run: |
          VERSION=$(date +"%y.%-m.%-d-%H%M")
          echo "VERSION=$VERSION" >> $GITHUB_ENV

      - name: Package
        run: |
          cd build
          DESTDIR=../mailadler-arm64 cmake --install .
          cd ..
          tar -cJf mailadler-linux-arm64-${{ env.VERSION }}.txz mailadler-arm64

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: mailadler-linux-arm64
          path: mailadler-linux-arm64-*.txz

      - name: Upload to release (on tag)
        if: startsWith(github.ref, 'refs/tags/')
        uses: softprops/action-gh-release@v1
        with:
          files: mailadler-linux-arm64-*.txz
