name: "[Single] Build Linux"

on:
  workflow_dispatch:
    inputs:
      nightly:
        description: "Nightly prepare"
        required: true
        type: boolean
        default: false

      tag:
        description: "Release Tag"
        required: true
        type: string

      aarch64:
        description: "Build for aarch64"
        required: false
        type: boolean
        default: false

  workflow_call:
    inputs:
      nightly:
        description: "Nightly prepare"
        required: true
        type: boolean
        default: false

      tag:
        description: "Release Tag"
        required: true
        type: string

      aarch64:
        description: "Build for aarch64"
        required: false
        type: boolean
        default: false

jobs:
  build:
    runs-on: ubuntu-22.04 # 需要手动升级到 24.04

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust stable
        run: |
          rustup install stable --profile minimal --no-self-update
          rustup default stable
      - name: Setup Cargo binstall
        if: ${{ inputs.aarch64 == true }}
        uses: cargo-bins/cargo-binstall@main
      - name: Setup Cross Toolchain
        if: ${{ inputs.aarch64 == true }}
        run: |
          rustup target add aarch64-unknown-linux-gnu
          cargo binstall -y cross
      - name: Setup Toolchain
        run: |
          sudo apt-get update
          sudo apt-get install -y libgtk-3-dev webkit2gtk-4.0 libappindicator3-dev librsvg2-dev patchelf openssl
      - uses: Swatinem/rust-cache@v2
        with:
          workspaces: "./backend/"
          prefix-key: "rust-stable"
          key: ubuntu-latest
          shared-key: "release"

      - name: Install Node latest
        uses: actions/setup-node@v4
        with:
          node-version: latest

      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          run_install: false

      - name: Pnpm install deps and download resources
        run: |
          pnpm i
          ${{ inputs.aarch64 == true && 'pnpm check --arch arm64 --sidecar-host aarch64-unknown-linux-gnu' || 'pnpm check' }}

      - name: Nightly Prepare
        if: ${{ inputs.nightly == true }}
        run: |
          pnpm prepare:nightly
      - name: Build UI
        run: |
          pnpm -F ui build
      - name: Tauri build
        uses: tauri-apps/tauri-action@v0
        if: ${{ inputs.aarch64 == false }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAURI_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
          TAURI_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}
          NIGHTLY: ${{ inputs.nightly == true  && 'true' || 'false' }}
        with:
          tagName: ${{ inputs.tag }}
          releaseName: "Clash Nyanpasu Dev"
          releaseBody: "More new features are now supported."
          releaseDraft: false
          prerelease: true
          tauriScript: pnpm tauri
          args: ${{ inputs.nightly == true && '-f nightly -c ./backend/tauri/tauri.nightly.conf.json' || '-f default-meta -c ./backend/tauri/tauri.conf.json' }}
      - name: Tauri build and upload (aarch64)
        if: ${{ inputs.aarch64 == true }}
        shell: bash
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAURI_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
          TAURI_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}
          NIGHTLY: ${{ inputs.nightly == true  && 'true' || 'false' }}
          CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
          CC_aarch64_unknown_linux_gnu: aarch64-linux-gnu-gcc
          CXX_aarch64_unknown_linux_gnu: aarch64-linux-gnu-g++
          PKG_CONFIG_PATH: /usr/lib/aarch64-linux-gnu/pkgconfig
          PKG_CONFIG_ALLOW_CROSS: 1
        run: |
          ${{ inputs.nightly == true && 'pnpm build:nightly -r cross --target aarch64-unknown-linux-gnu -b "rpm,deb,updater"' || 'pnpm build -r cross --target aarch64-unknown-linux-gnu -b "rpm,deb,updater"' }}
          find ./backend/target \( -name "*.deb" -o -name "*.rpm" \) | while read file; do
            gh release upload ${{ inputs.tag }} "$file" --clobber
          done

      - name: Calc the archive signature
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          TAG_NAME=${{ inputs.tag }}
          find ./backend/target \( -name "*.deb" -o -name "*.rpm" \) | while read file; do
            sha_file="$file.sha256"

            if [[ ! -f "$sha_file" ]]; then
              sha256sum "$file" > "$sha_file"
              echo "Created checksum file for: $file"
            fi

            gh release upload $TAG_NAME "$sha_file" --clobber
            echo "Uploaded $sha_file to release $TAG_NAME"
          done
