name: Build Linux AppImage

on:
  workflow_call:
  workflow_dispatch:

concurrency:
  group: build-linux-appimage-${{ github.ref }}
  cancel-in-progress: true

env:
  CACHE_REV: v1

jobs:
  linux_appimage_ubuntu24:
    name: Linux AppImage (ubuntu-24.04)
    runs-on: ubuntu-24.04
    timeout-minutes: 360
    env:
      DIST_ID: ubuntu24.04
      EVENT_TAG: ${{ github.event.release.tag_name }}

    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          lfs: 'false'

      - uses: lukka/get-cmake@latest
        with:
          cmakeVersion: "~4.3.0"
          useLocalCache: true
          useCloudCache: true

      - name: Compute version
        shell: bash
        run: |
          set -euo pipefail
          tag="${EVENT_TAG:-${GITHUB_REF_NAME:-}}"
          if [[ "$tag" == v* ]]; then
            ver="${tag#v}"
          else
            ver="git${GITHUB_SHA::7}"
          fi
          echo "VER=$ver" >> "$GITHUB_ENV"

      - name: Compute deps cache key
        shell: bash
        run: |
          set -euo pipefail
          deps_tree="$(git rev-parse HEAD:deps 2>/dev/null || echo no-deps)"
          recipe_hash="$(find build_linux.sh scripts/linux.d -type f -print0 | sort -z | xargs -0 cat | sha256sum | cut -c1-12)"
          echo "DEPS_CACHE_KEY=linux-${DIST_ID}-deps-${deps_tree}-${recipe_hash}-${CACHE_REV}" >> "$GITHUB_ENV"

      - name: Apt-Install Dependencies
        uses: ./.github/actions/apt-install-deps

      - name: Linux - cache deps
        id: cache_linux_deps
        uses: actions/cache@v5
        with:
          path: ${{ github.workspace }}/deps/build/OrcaSlicer_dep
          key: ${{ env.DEPS_CACHE_KEY }}

      - name: Linux - build deps if missing
        if: steps.cache_linux_deps.outputs.cache-hit != 'true'
        shell: bash
        run: |
          set -euo pipefail
          for attempt in 1 2 3; do
            echo "Build Linux deps attempt $attempt"
            if ./build_linux.sh -drlL; then
              exit 0
            fi
            if [ "$attempt" -lt 3 ]; then
              echo "Transient dependency download/build failure, retrying after backoff"
              sleep $((attempt * 20))
            fi
          done
          exit 1

      - name: Linux - build AppImage
        shell: bash
        run: |
          set -euo pipefail
          rm -rf build build-dbg build-dbginfo out
          mkdir -p out

          ./build_linux.sh -istrlL
          ./scripts/check_appimage_libs.sh ./build/package ./build/package/bin/orca-slicer

          mapfile -t appimages < <(find build -maxdepth 3 -type f -name '*.AppImage' ! -name 'appimagetool.AppImage' | LC_ALL=C sort)

          if (( ${#appimages[@]} == 0 )); then
            echo "ERROR: AppImage not produced"
            find build -maxdepth 3 -type f | LC_ALL=C sort
            exit 1
          fi

          if (( ${#appimages[@]} > 1 )); then
            echo "INFO: multiple AppImage candidates found, using the first sorted entry"
            printf '%s\n' "${appimages[@]}"
          fi

          appimage="${appimages[0]}"
          mv "$appimage" "out/OrcaSlicer-BMCU_Linux_AppImage_${DIST_ID}_amd64_${VER}.AppImage"
          chmod +x "out/OrcaSlicer-BMCU_Linux_AppImage_${DIST_ID}_amd64_${VER}.AppImage"

      - name: Linux - checksums
        shell: bash
        run: |
          set -euo pipefail
          (cd out && sha256sum * > "SHA256SUMS_${DIST_ID}_appimage.txt")

      - name: Upload artifacts (Linux AppImage ubuntu24.04)
        uses: actions/upload-artifact@v7
        with:
          name: orcaslicer-bmcu-linux-appimage-${{ env.DIST_ID }}-${{ github.sha }}
          path: out/
          if-no-files-found: error
