name: Linux Build

on:
  workflow_call:
    inputs:
      flutter_version:
        required: true
        type: string
      workflow_profile:
        required: false
        type: string
        default: "ci"
      artifact_name:
        required: false
        type: string
        default: "Linux"
      include_metadata_defines:
        required: false
        type: boolean
        default: false
      include_dev_define:
        required: false
        type: boolean
        default: false
      package_release_artifacts:
        required: false
        type: boolean
        default: false
      extra_build_args:
        required: false
        type: string
        default: ""

jobs:
  build-linux:
    name: Build Linux (${{ inputs.workflow_profile }})
    runs-on: ubuntu-latest
    container:
      image: archlinux:base-devel
    steps:
      - uses: actions/checkout@v6

      - name: Install pre dependencies
        run: |
          pacman -Syu --noconfirm
          pacman -S --noconfirm jq git clang cmake ninja wget patchelf

      - uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ inputs.flutter_version }}
          cache: true

      - name: Install dependencies
        run: |
          pacman -S --noconfirm gtk3 gstreamer libayatana-appindicator libkeybinder3 wpewebkit
          git config --global --add safe.directory /__t/flutter/*
          flutter pub get

      - name: Build Linux
        shell: bash
        run: |
          BUILD_ARGS="${{ inputs.extra_build_args }}"
          if [[ "${{ inputs.include_dev_define }}" == "true" ]]; then
            BUILD_ARGS="$BUILD_ARGS --dart-define=DEV=true"
          fi
          if [[ "${{ inputs.include_metadata_defines }}" == "true" ]]; then
            BUILD_ARGS="$BUILD_ARGS --dart-define=GIT_COMMIT_HASH=$(git rev-parse --short HEAD) --dart-define=BUILD_TIMESTAMP=$(date +%s)"
          fi
          flutter build linux $BUILD_ARGS

      - name: Create WPE symlinks
        run: |
          cd build/linux/x64/release/bundle/lib/
          mv libwpe-1.0.so.* libwpe-1.0.so.1
          mv libWPEWebKit-2.0.so.* libWPEWebKit-2.0.so.1

      - name: Bundle Libraries
        run: |
          BUNDLE_LIB_DIR="build/linux/x64/release/bundle/lib"

          cp /usr/lib/libicui18n.so.78 "$BUNDLE_LIB_DIR/"
          cp /usr/lib/libicuuc.so.78 "$BUNDLE_LIB_DIR/"
          cp /usr/lib/libjpeg.so.8 "$BUNDLE_LIB_DIR/"
          cp /usr/lib/libxml2.so.16 "$BUNDLE_LIB_DIR/"
          cp /usr/lib/libicudata.so.78 "$BUNDLE_LIB_DIR/"

          cd build/linux/x64/release/bundle
          patchelf --force-rpath --set-rpath '$ORIGIN/lib' ciyue

      - name: Compression
        if: ${{ inputs.package_release_artifacts }}
        run: tar -cJvf ciyue-linux.tar.xz build/linux/x64/release/bundle/*

      - name: Build AppImage
        if: ${{ inputs.package_release_artifacts }}
        run: |
          wget -O appimagetool "https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-x86_64.AppImage"
          chmod +x appimagetool
          mv appimagetool /usr/local/bin/

          ./tools/build_appimage.sh
          mv Ciyue-x86_64.AppImage Ciyue-latest-x86_64.AppImage
          mv Ciyue-x86_64.AppImage.zsync Ciyue-latest-x86_64.AppImage.zsync

      - name: Build Deb
        if: ${{ inputs.package_release_artifacts }}
        run: |
          pacman -S --noconfirm dpkg

          ./tools/build_deb.sh
          mv *.deb ciyue-x86_64.deb

      - name: Build rpm
        if: ${{ inputs.package_release_artifacts }}
        run: |
          pacman -S --noconfirm rpm-tools

          ./tools/build_rpm.sh
          mv ciyue-*.x86_64.rpm ciyue.x86_64.rpm

      - name: Upload bundle
        if: ${{ !inputs.package_release_artifacts }}
        uses: actions/upload-artifact@v4
        with:
          name: ${{ inputs.artifact_name }}
          path: build/linux/x64/release/bundle/

      - name: Upload release artifacts
        if: ${{ inputs.package_release_artifacts }}
        uses: actions/upload-artifact@v4
        with:
          name: ${{ inputs.artifact_name }}
          path: |
            ciyue-linux.tar.xz
            Ciyue-latest-x86_64.AppImage
            *.zsync
            ciyue-x86_64.deb
            ciyue-x86_64.rpm
