name: Build arm64 AppImage

on:
  workflow_dispatch:

env:
  FLUTTER_VERSION: "3.41.9"

jobs:
  build:
    runs-on: ubuntu-22.04
    outputs:
      version: ${{ steps.get_version.outputs.version }}

    steps:
      - uses: actions/checkout@v6

      - name: Get version from pubspec.yaml
        id: get_version
        run: |
          VERSION=$(sed -n 's/^version: \([0-9]*\.[0-9]*\.[0-9]*\).*/\1/p' app/pubspec.yaml)
          echo "version=$VERSION" >> $GITHUB_OUTPUT

  # Manage the Flutter version on the self-hosted runner manually!
  # Flutter currently doesn't provide Linux ARM64 binaries.
  build_appimage_arm_64:
    needs: build
    runs-on: [self-hosted, linux, arm64]

    steps:
      - uses: actions/checkout@v6

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y clang cmake libgtk-3-dev ninja-build libayatana-appindicator3-dev libfuse2

      - name: Dependencies
        working-directory: app
        run: flutter pub get

      - name: Compile linux
        working-directory: app
        run: flutter build linux

      - name: Copy compiled linux files
        run: |
          mkdir AppDir
          cp -r app/build/linux/arm64/release/bundle/* AppDir/

      - name: Copy logo to AppDir
        run: |
          mkdir -p AppDir/usr/share/icons/hicolor/32x32/apps
          cp app/assets/img/logo-32.png AppDir/usr/share/icons/hicolor/32x32/apps/localsend.png
          mkdir -p AppDir/usr/share/icons/hicolor/128x128/apps
          cp app/assets/img/logo-128.png AppDir/usr/share/icons/hicolor/128x128/apps/localsend.png
          mkdir -p AppDir/usr/share/icons/hicolor/256x256/apps
          cp app/assets/img/logo-256.png AppDir/usr/share/icons/hicolor/256x256/apps/localsend.png

      - name: Copy Recipe to correct location
        run: cp support/build/appimage/AppImageBuilder_arm_64.yml AppImageBuilder.yml

      - name: Build AppImage
        uses: AppImageCrafters/build-appimage@57c3bc6963f870ce3be103117de5b5e33ffbaeb6
        with:
          recipe: ./AppImageBuilder.yml

      - name: Upload AppImage file
        uses: actions/upload-artifact@v6
        with:
          name: appimage-arm-64-result
          path: ./*.AppImage
