name: release Linux

on:
  workflow_dispatch:
    inputs:
      release_tag:
        required: false
        type: string
  push:
    branches:
      - master
    tags:
      - 'v*'
      - 'V*'

permissions:
  contents: write

env:
  OutputArch: "linux-64"
  OutputArchArm: "linux-arm64"
  OutputPath64:  "${{ github.workspace }}/v2rayN/Release/linux-64"
  OutputPathArm64:  "${{ github.workspace }}/v2rayN/Release/linux-arm64"

jobs:
  build:
    strategy:
      matrix:
        configuration: [Release]
    runs-on: ubuntu-24.04

    steps:
    - name: Checkout
      uses: actions/checkout@v6.0.1
      with:
        submodules: 'recursive'
        fetch-depth: '0'

    - name: Setup .NET
      uses: actions/setup-dotnet@v5.0.1
      with:
        dotnet-version: '8.0.x'

    - name: Build
      run: |
        cd v2rayN
        dotnet publish ./v2rayN.Desktop/v2rayN.Desktop.csproj -c Release -r linux-x64   -p:SelfContained=true -o "$OutputPath64"
        dotnet publish ./v2rayN.Desktop/v2rayN.Desktop.csproj -c Release -r linux-arm64 -p:SelfContained=true -o "$OutputPathArm64"
        dotnet publish ./AmazTool/AmazTool.csproj             -c Release -r linux-x64   -p:SelfContained=true -p:PublishTrimmed=true -o "$OutputPath64"
        dotnet publish ./AmazTool/AmazTool.csproj             -c Release -r linux-arm64 -p:SelfContained=true -p:PublishTrimmed=true -o "$OutputPathArm64"

    - name: Upload build artifacts
      uses: actions/upload-artifact@v6.0.0
      with:
        name: v2rayN-linux
        path: |
          ${{ github.workspace }}/v2rayN/Release/linux*

    # release debian package
    - name: Package debian
      if: github.event.inputs.release_tag != ''
      run: |
        chmod 755 package-debian.sh
        ./package-debian.sh "$OutputArch" "$OutputPath64" "${{ github.event.inputs.release_tag }}"
        ./package-debian.sh "$OutputArchArm" "$OutputPathArm64" "${{ github.event.inputs.release_tag }}"

    - name: Upload deb to release
      uses: svenstaro/upload-release-action@v2
      if: github.event.inputs.release_tag != ''
      with:
        file: ${{ github.workspace }}/v2rayN*.deb
        tag: ${{ github.event.inputs.release_tag }}
        file_glob: true
        prerelease: true

    # release zip archive
    - name: Package release zip archive
      if: github.event.inputs.release_tag != ''
      run: |
        chmod 755 package-release-zip.sh
        ./package-release-zip.sh "$OutputArch" "$OutputPath64"
        ./package-release-zip.sh "$OutputArchArm" "$OutputPathArm64"
    
    - name: Upload zip archive to release
      uses: svenstaro/upload-release-action@v2
      if: github.event.inputs.release_tag != ''
      with:
        file: ${{ github.workspace }}/v2rayN*.zip
        tag: ${{ github.event.inputs.release_tag }}
        file_glob: true
        prerelease: true

  rpm:
    needs: build
    if: |
      (github.event_name == 'workflow_dispatch' && github.event.inputs.release_tag != '') ||
      (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/'))
    runs-on: ubuntu-24.04
    container:
      image: registry.access.redhat.com/ubi10/ubi
    env:
      RELEASE_TAG: ${{ github.event.inputs.release_tag != '' && github.event.inputs.release_tag || github.ref_name }}

    steps:
    - name: Prepare tools (Red Hat)
      run: |
        dnf repolist all
        dnf -y makecache
        dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-10.noarch.rpm
        dnf -y install sudo git rpm-build rpmdevtools dnf-plugins-core rsync findutils tar gzip unzip which

    - name: Checkout repo (for scripts)
      uses: actions/checkout@v6.0.1
      with:
        submodules: 'recursive'
        fetch-depth: '0'

    - name: Restore build artifacts
      uses: actions/download-artifact@v7
      with:
        name: v2rayN-linux
        path: ${{ github.workspace }}/v2rayN/Release

    - name: Ensure script permissions
      run: chmod 755 package-rhel.sh

    - name: Package RPM (RHEL-family)
      run: ./package-rhel.sh "${RELEASE_TAG}" --arch all

    - name: Collect RPMs into workspace
      run: |
        mkdir -p "$GITHUB_WORKSPACE/dist/rpm"
        rsync -av "$HOME/rpmbuild/RPMS/" "$GITHUB_WORKSPACE/dist/rpm/" || true
        find "$GITHUB_WORKSPACE/dist/rpm" -name "v2rayN-*-1*.x86_64.rpm"  -exec mv {} "$GITHUB_WORKSPACE/dist/rpm/v2rayN-linux-rhel-64.rpm" \;  || true
        find "$GITHUB_WORKSPACE/dist/rpm" -name "v2rayN-*-1*.aarch64.rpm" -exec mv {} "$GITHUB_WORKSPACE/dist/rpm/v2rayN-linux-rhel-arm64.rpm" \; || true
        echo "==== Dist tree ===="
        ls -R "$GITHUB_WORKSPACE/dist/rpm" || true

    - name: Upload RPM artifacts
      uses: actions/upload-artifact@v6.0.0
      with:
        name: v2rayN-rpm
        path: dist/rpm/**/*.rpm

    - name: Upload RPMs to release
      uses: svenstaro/upload-release-action@v2
      with:
        file: dist/rpm/**/*.rpm
        tag: ${{ env.RELEASE_TAG }}
        file_glob: true
        prerelease: true
