name: Linux (x86_64) Nightly Build

on:
  workflow_dispatch:
    inputs:
      release_upload_url:
        description: 'Release upload URL'
        required: true

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build Linux Packages
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 'lts/*'
          cache: 'npm'
          
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        
      - name: Install Linux Dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            build-essential \
            pkg-config \
            libgtk-3-dev \
            libayatana-appindicator3-dev \
            librsvg2-dev \
            libglib2.0-dev \
            libjavascriptcoregtk-4.0-dev \
            libsoup-3.0-dev \
            libwebkit2gtk-4.1-dev
          
      - name: Install Dependencies
        run: |
          npm install
          
      - name: Build Frontend
        run: npm run build
        
      - name: Build AppImage (x86_64)
        run: |
          echo "Building AppImage for x86_64..."
          npm run tauri build -- --target x86_64-unknown-linux-gnu --bundles appimage
          cd src-tauri/target/x86_64-unknown-linux-gnu/release/bundle/appimage/
          for f in *.AppImage; do
            echo "APPIMAGE_PATH=src-tauri/target/x86_64-unknown-linux-gnu/release/bundle/appimage/$f" >> $GITHUB_ENV
          done

      - name: Build Debian Package (x86_64)
        run: |
          echo "Building Debian package for x86_64..."
          npm run tauri build -- --target x86_64-unknown-linux-gnu --bundles deb
          cd src-tauri/target/x86_64-unknown-linux-gnu/release/bundle/deb/
          for f in *.deb; do
            echo "DEB_PATH=src-tauri/target/x86_64-unknown-linux-gnu/release/bundle/deb/$f" >> $GITHUB_ENV
          done

      - name: Build RPM Package (x86_64)
        run: |
          echo "Building RPM package for x86_64..."
          npm run tauri build -- --target x86_64-unknown-linux-gnu --bundles rpm
          cd src-tauri/target/x86_64-unknown-linux-gnu/release/bundle/rpm/
          for f in *.rpm; do
            echo "RPM_PATH=src-tauri/target/x86_64-unknown-linux-gnu/release/bundle/rpm/$f" >> $GITHUB_ENV
          done

      - name: Get version from package.json
        id: version
        run: echo "version=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT

      - name: Upload AppImage to Release
        if: github.event.inputs.release_upload_url != ''
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
        with:
          upload_url: ${{ github.event.inputs.release_upload_url }}
          asset_path: ${{ env.APPIMAGE_PATH }}
          asset_name: NeoHtop_${{ steps.version.outputs.version }}_x86_64.AppImage
          asset_content_type: application/x-executable

      - name: Upload Debian Package to Release
        if: github.event.inputs.release_upload_url != ''
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
        with:
          upload_url: ${{ github.event.inputs.release_upload_url }}
          asset_path: ${{ env.DEB_PATH }}
          asset_name: NeoHtop_${{ steps.version.outputs.version }}_x86_64.deb
          asset_content_type: application/vnd.debian.binary-package

      - name: Upload RPM Package to Release
        if: github.event.inputs.release_upload_url != ''
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
        with:
          upload_url: ${{ github.event.inputs.release_upload_url }}
          asset_path: ${{ env.RPM_PATH }}
          asset_name: NeoHtop_${{ steps.version.outputs.version }}_x86_64.rpm
          asset_content_type: application/x-rpm
