name: Flutter Nightly MacOS Arm64 Build

on:
  #schedule:
    # schedule build every night
  #  - cron: "0/6 * * * *"
  workflow_dispatch:

env:
  RUST_VERSION: "1.75" # https://github.com/rustdesk/rustdesk/discussions/7503
  CARGO_NDK_VERSION: "3.1.2"
  LLVM_VERSION: "15.0.6"
  FLUTTER_VERSION: "3.19.6"
  FLUTTER_RUST_BRIDGE_VERSION: "1.80.1"
  # for arm64 linux because official Dart SDK does not work
  FLUTTER_ELINUX_VERSION: "3.16.9"
  TAG_NAME: "nightly"
  VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
  # vcpkg version: 2024.03.25
  VCPKG_COMMIT_ID: "a34c873a9717a888f58dc05268dea15592c2f0ff"
  VERSION: "1.2.4"
  NDK_VERSION: "r26d"
  #signing keys env variable checks
  ANDROID_SIGNING_KEY: "${{ secrets.ANDROID_SIGNING_KEY }}"
  MACOS_P12_BASE64: "${{ secrets.MACOS_P12_BASE64 }}"
  # To make a custom build with your own servers set the below secret values
  RS_PUB_KEY: "${{ secrets.RS_PUB_KEY }}"
  RENDEZVOUS_SERVER: "${{ secrets.RENDEZVOUS_SERVER }}"
  API_SERVER: "${{ secrets.API_SERVER }}"
  UPLOAD_ARTIFACT: "${{ inputs.upload-artifact }}"
  SIGN_BASE_URL: "${{ secrets.SIGN_BASE_URL }}"

jobs:
  build-appimage:
    name: Build image ${{ matrix.job.target }}
    runs-on: ubuntu-20.04
    strategy:
      fail-fast: false
      matrix:
        job:
          - {
              target: x86_64-unknown-linux-gnu,
              arch: x86_64,
            }
          - {
              target: aarch64-unknown-linux-gnu,
              arch: aarch64,
            }
    steps:
      - name: Checkout source code
        uses: actions/checkout@v3

      - name: Rename Binary
        run: |
          sudo apt-get update -y
          sudo apt-get install -y wget libarchive-tools
          wget https://github.com/rustdesk/rustdesk/releases/download/nightly/rustdesk-${{ env.VERSION }}-${{ matrix.job.arch }}.deb
          mv rustdesk-${{ env.VERSION }}-${{ matrix.job.arch }}.deb rustdesk-${{ env.VERSION }}.deb

      - name: Patch archlinux PKGBUILD
        if: matrix.job.arch == 'x86_64' 
        run: |
          sed -i "s/x86_64/${{ matrix.job.arch }}/g" res/PKGBUILD
          if [[ "${{ matrix.job.arch }}" == "aarch64" ]]; then
            sed -i "s/linux\/x64/linux\/arm64/g" ./res/PKGBUILD
          fi
          bsdtar -zxvf rustdesk-${{ env.VERSION }}.deb
          tar -xvf ./data.tar.xz
          case ${{ matrix.job.arch }} in
            aarch64)
              mkdir -p flutter/build/linux/arm64/release/bundle
              cp -rf usr/lib/rustdesk/* flutter/build/linux/arm64/release/bundle/
              ;;
            x86_64)
              mkdir -p flutter/build/linux/x64/release/bundle
              cp -rf usr/lib/rustdesk/* flutter/build/linux/x64/release/bundle/
              ;;
          esac

      - name: Build archlinux package
        if: matrix.job.arch == 'x86_64' 
        uses: rustdesk-org/arch-makepkg-action@master
        with:
          packages: >
            llvm
            clang
            libva
            libvdpau
            rust
            gstreamer
            unzip
            git
            cmake
            gcc
            curl
            wget
            nasm
            zip
            make
            pkg-config
            clang
            gtk3
            xdotool
            libxcb
            libxfixes
            alsa-lib
            pipewire
            python
            ttf-arphic-uming
            libappindicator-gtk3
            pam
            gst-plugins-base
            gst-plugin-pipewire
          scripts: |
            cd res && HBB=`pwd`/.. FLUTTER=1 makepkg -f

      - name: Publish archlinux package
        if: matrix.job.arch == 'x86_64' 
        uses: softprops/action-gh-release@v1
        with:
          prerelease: true
          tag_name: ${{ env.TAG_NAME }}
          files: |
            res/rustdesk-${{ env.VERSION }}*.zst

      - name: Build appimage package
        shell: bash
        run: |
          # set-up appimage-builder
          pushd /tmp
          wget -O appimage-builder-x86_64.AppImage https://github.com/AppImageCrafters/appimage-builder/releases/download/v1.1.0/appimage-builder-1.1.0-x86_64.AppImage
          chmod +x appimage-builder-x86_64.AppImage
          sudo mv appimage-builder-x86_64.AppImage /usr/local/bin/appimage-builder
          popd
          # run appimage-builder
          pushd appimage
          sudo appimage-builder --skip-tests --recipe ./AppImageBuilder-${{ matrix.job.arch }}.yml

      - name: Publish appimage package
        uses: softprops/action-gh-release@v1
        with:
          prerelease: true
          tag_name: ${{ env.TAG_NAME }}
          files: |
            ./appimage/rustdesk-${{ env.VERSION }}-*.AppImage

  build-flatpak:
    name: Build Flatpak ${{ matrix.job.target }}
    runs-on: ${{ matrix.job.on }}
    strategy:
      fail-fast: false
      matrix:
        job:
          - {
              target: x86_64-unknown-linux-gnu,
              distro: ubuntu18.04,
              on: ubuntu-20.04,
              arch: x86_64,
            }
          - {
              target: aarch64-unknown-linux-gnu,
              # try out newer flatpak since error of "error: Nothing matches org.freedesktop.Platform in remote flathub"
              distro: ubuntu22.04,
              on: [self-hosted, Linux, ARM64],
              arch: aarch64,
            }
    steps:
      - name: Checkout source code
        uses: actions/checkout@v3

      - name: Rename Binary
        run: |
          sudo apt-get update -y
          sudo apt-get install -y wget
          wget https://github.com/rustdesk/rustdesk/releases/download/nightly/rustdesk-${{ env.VERSION }}-${{ matrix.job.arch }}.deb
          mv rustdesk-${{ env.VERSION }}-${{ matrix.job.arch }}.deb rustdesk-${{ env.VERSION }}.deb

      - uses: rustdesk-org/run-on-arch-action@amd64-support
        name: Build rustdesk flatpak package for ${{ matrix.job.arch }}
        id: rpm
        with:
          arch: ${{ matrix.job.arch }}
          distro: ${{ matrix.job.distro }}
          githubToken: ${{ github.token }}
          setup: |
            ls -l "${PWD}"
          dockerRunArgs: |
            --volume "${PWD}:/workspace"
          shell: /bin/bash
          install: |
            apt-get update -y
            apt-get install -y \
               curl \
               git \
               rpm \
               wget
          run: |
            # disable git safe.directory
            git config --global --add safe.directory "*"
            pushd /workspace
            # install 
            apt-get update -y
            apt-get install -y \
               cmake \
               curl \
               flatpak \
               flatpak-builder \
               gcc \
               git \
               g++ \
               libgtk-3-dev \
               nasm \
               wget
            # flatpak deps
            flatpak --user remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
            flatpak --user install -y flathub org.freedesktop.Platform/${{ matrix.job.arch }}/23.08
            flatpak --user install -y flathub org.freedesktop.Sdk/${{ matrix.job.arch }}/23.08
            # package
            pushd flatpak
            git clone https://github.com/flathub/shared-modules.git --depth=1
            flatpak-builder --user --force-clean --repo=repo ./build ./rustdesk.json
            flatpak build-bundle ./repo rustdesk-${{ env.VERSION }}-${{ matrix.job.arch }}.flatpak com.rustdesk.RustDesk

      - name: Publish flatpak package
        uses: softprops/action-gh-release@v1
        with:
          prerelease: true
          tag_name: ${{ env.TAG_NAME }}
          files: |
            flatpak/rustdesk-${{ env.VERSION }}-${{ matrix.job.arch }}.flatpak
