name: release
on: 
  push:

env:
  CARGO_NDK_VERSION: "3.1.2"
  LLVM_VERSION: "15.0.6"
  FLUTTER_VERSION: "3.10.6"
  FLUTTER_RUST_BRIDGE_VERSION: "1.75.3"
  # for arm64 linux
  FLUTTER_ELINUX_VERSION: "3.10.6"
  FLUTTER_ELINUX_COMMIT_ID: "410b3ca42f2cd0c485edf517a1666652bab442d4"
  # vcpkg version: 2023.04.15
  # for multiarch gcc compatibility
  VCPKG_COMMIT_ID: "501db0f17ef6df184fcdbfbe0f87cde2313b6ab1"
  VERSION: "1.2.3"
  NDK_VERSION: "r25c"
  #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 }}'

jobs:
  release:
    name: ${{ matrix.job.target }} (${{ matrix.job.os }})
    runs-on: [self-hosted, Windows]
    strategy:
      fail-fast: false
      matrix:
        job:
          - { target: x86_64-pc-windows-msvc, os: windows-2019, arch: x86_64 }
    steps:
      - name: Checkout source code
        uses: actions/checkout@v3

      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          target: ${{ matrix.job.target }}
          override: true
          profile: minimal # minimal component installation (ie, no documentation)

      - uses: Swatinem/rust-cache@v2
        with:
          prefix-key: ${{ matrix.job.os }}

      - name: Install flutter rust bridge deps
        shell: powershell
        run: |
          git config --global core.longpaths true
          cargo install flutter_rust_bridge_codegen --version ${{ env.FLUTTER_RUST_BRIDGE_VERSION }} --features "uuid"
          Push-Location flutter ; flutter pub get ; Pop-Location
          ~/.cargo/bin/flutter_rust_bridge_codegen --rust-input ./src/flutter_ffi.rs --dart-output ./flutter/lib/generated_bridge.dart

      - name: Build rustdesk
        shell: powershell
        env:
          VCPKG_ROOT: C:\rustdesk_thirdpary_lib\vcpkg
        run: python3 .\build.py --portable --hwcodec --flutter --feature IddDriver

      - name: find Runner.res
        continue-on-error: true
        shell: bash
        run: |
          runner_res=$("C:\Program Files\Git\usr\bin\find.exe" . -name "Runner.res");
          if [ "$runner_res" == "" ]; then
            echo "Runner.res: not found";
          else
            echo "Runner.res: $runner_res";
            cp $runner_res ./libs/portable/Runner.res;
            echo "list ./libs/portable/Runner.res";
            ls -l ./libs/portable/Runner.res;
          fi

      - name: Sign rustdesk files
        uses: GermanBluefox/code-sign-action@v7
        with:
          certificate: '${{ secrets.WINDOWS_PFX_BASE64 }}'
          password: '${{ secrets.WINDOWS_PFX_PASSWORD }}'
          certificatesha1: '${{ secrets.WINDOWS_PFX_SHA1_THUMBPRINT }}'
          # certificatename: '${{ secrets.CERTNAME }}'
          folder: './flutter/build/windows/runner/Release/'
          recursive: true

      - name: Build self-extracted executable
        shell: bash
        run: |
          pushd ./libs/portable
          python3 ./generate.py -f ../../flutter/build/windows/runner/Release/ -o . -e ../../flutter/build/windows/runner/Release/rustdesk.exe
          popd
          mkdir -p ./SignOutput
          mv ./target/release/rustdesk-portable-packer.exe ./SignOutput/rustdesk-${{ env.VERSION }}-${{ matrix.job.arch }}.exe

      - name: Sign rustdesk self-extracted file
        uses: GermanBluefox/code-sign-action@v7
        with:
          certificate: '${{ secrets.WINDOWS_PFX_BASE64 }}'
          password: '${{ secrets.WINDOWS_PFX_PASSWORD }}'
          certificatesha1: '${{ secrets.WINDOWS_PFX_SHA1_THUMBPRINT }}'
          # certificatename: '${{ secrets.WINDOWS_PFX_NAME }}'
          folder: './SignOutput'
          recursive: false

      - name: Publish Release
        uses: https://gitea.com/actions/release-action@main
        with:
          files: |
            ./SignOutput/rustdesk-*.exe
