name: Build Latest and Release

on:
  workflow_dispatch:
    inputs:
      doRelease:
        description: 'Publish new release'
        type: boolean
        default: false
        required: false
      tag:
        type: string
        description: 'Release version tag (e.g. v1.2.3)'
        required: true
      ref:
        type: string
        description: 'Git ref from which to release'
        required: true
        default: 'main'

env:
  ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true

jobs:
  build-win-x64:
    runs-on: windows-latest

    steps:
      - name: Check-out repository
        uses: actions/checkout@v1

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'
          cache: 'pip'
          cache-dependency-path: |
            **/requirements*.txt
          architecture: 'x64'
      
      - name: Install Dependencies
        run: |
          pip install -r requirements.txt

      - name: Build Executable with Nuitka
        uses: Nuitka/Nuitka-Action@main
        with:
          nuitka-version: main
          script-name: gofilecli.py
          onefile: true

      - name: Upload Artifact[win-x64]
        uses: actions/upload-artifact@v3.1.3
        with:
          name: GoFileCLI_win-x64
          path: |
            build/gofilecli.exe
  
  build-linux-x64:
    runs-on: ubuntu-latest

    steps:
      - name: Check-out repository
        uses: actions/checkout@v1

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'
          cache: 'pip'
          cache-dependency-path: |
            **/requirements*.txt
          architecture: 'x64'
      
      - name: Install Dependencies
        run: |
          sudo apt-get install -y libasound2-dev
          pip install -r requirements.txt

      - name: Build Executable with Nuitka
        uses: Nuitka/Nuitka-Action@main
        with:
          nuitka-version: main
          script-name: gofilecli.py
          onefile: true
        
      - name: Rename Executable
        run: mv build/gofilecli.bin build/gofilecli

      - name: Upload Artifact[linux-x64]
        uses: actions/upload-artifact@v3.1.3
        with:
          name: GoFileCLI_linux-x64
          path: |
            build/gofilecli
    
  build-macos:
    runs-on: macos-latest

    steps:
      - name: Check-out repository
        uses: actions/checkout@v1

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'
          cache: 'pip'
          cache-dependency-path: |
            **/requirements*.txt
          architecture: 'arm64'
      
      - name: Install Dependencies
        run: |
          pip install -r requirements.txt
          pip install nuitka

      - name: Build Executable with Nuitka
        run: |
          python -m nuitka --onefile --assume-yes-for-downloads --output-dir=dist --macos-target-arch=arm64 gofilecli.py
          python -m nuitka --onefile --assume-yes-for-downloads --output-dir=dist_x86_64 --macos-target-arch=x86_64 gofilecli.py
      
      - name: Rename Executable
        run: |
          mv dist/gofilecli.bin dist/gofilecli
          mv dist_x86_64/gofilecli.bin dist_x86_64/gofilecli


      - name: Upload Artifact [osx-arm64]
        uses: actions/upload-artifact@v3.1.3
        with:
          name: GoFileCLI_osx-x64
          path: dist_x86_64/gofilecli
      
      - name: Upload Artifact [osx-arm64]
        uses: actions/upload-artifact@v3.1.3
        with:
          name: GoFileCLI_osx-arm64
          path: dist/gofilecli

  create_draft_release:
    name: Create Github draft release
    if: ${{ github.event.inputs.doRelease == 'true' }}
    needs: [build-win-x64,build-linux-x64,build-macos]
    runs-on: ubuntu-latest
    steps:
      - name: Audit gh version
        run: gh --version

      - name: Check for existing release
        id: check_release
        run: |
          echo "::echo::on"
          gh release view --repo '${{ github.repository }}' '${{ github.event.inputs.tag }}' \
            && echo "already_exists=true" >> $GITHUB_ENV \
            || echo "already_exists=false" >> $GITHUB_ENV
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}

      - name: Checkout repo
        if: env.already_exists == 'false'
        uses: actions/checkout@v3
        with:
          ref: '${{ github.event.inputs.ref }}'

      - name: Create release
        if: env.already_exists == 'false'
        run: >
          gh release create
          '${{ github.event.inputs.tag }}'
          --draft
          --repo '${{ github.repository }}'
          --title '${{ github.event.inputs.tag }}'
          --target '${{ github.event.inputs.ref }}'
          --generate-notes
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}

  attach_to_release:
    name: Attach native executables to release
    if: ${{ github.event.inputs.doRelease == 'true' }}
    needs: [create_draft_release]
    runs-on: ubuntu-latest
    steps:
      - name: Get current date
        id: date
        run: echo "date=$(date +'%Y%m%d')" >> $GITHUB_ENV
        
      - name: GH version
        run: gh --version
      
      - name: Fetch executables
        uses: actions/download-artifact@v3

      - name: List downloaded files
        run: |
          echo "Listing files in the working directory:"
          ls -R
      
      - name: Tar (linux, macOS)
        run: for dir in *{osx,linux}*; do tar cvzfp "${dir}_${{ env.date }}.tar.gz" "$dir"; done
        
      - name: Zip (windows)
        run: for dir in *win*; do zip -r "${dir}_${{ env.date }}.zip" "$dir"; done

      - name: Upload
        run: |
          until gh release upload --clobber --repo ${{ github.repository }} ${{ github.event.inputs.tag }} *.zip *.tar.gz; do
            echo "Attempt $((++attempts)) to upload release artifacts failed. Will retry in 20s"
            sleep 20
          done
        timeout-minutes: 10
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}