name: "Build cooker (persistent)"

env:
  PERSISTENCE: true

on:
  push:
    branches:
     - cooker*
     - feat*
    paths:
     - '.github/workflows/**'
     - 'automation_tools/**'
     - 'config/**'
     - 'config/es-de/**'
     - 'functions/**'
     - 'rd-submodules/**'
     - '*.sh'
     - 'net.retrodeck.retrodeck.yml'
     - 'net.retrodeck.retrodeck.appdata.xml'
  pull_request: 
    branches:
     - cooker*

  workflow_dispatch:

jobs:

  Building_RetroDECK:
    runs-on: retrodeck
    steps:

      # Circumventing this bug: https://github.com/flatpak/flatpak-builder/issues/317
      - name: Remove stuck mounts
        run: sudo umount -f /home/ubuntu/actions-runner/_work/RetroDECK/RetroDECK/.flatpak-builder/rofiles/*
        continue-on-error: true
    
      - name: Clone RetroDECK repo
        uses: actions/checkout@v3
        with:
          submodules: 'true'

      - name: "Install dependencies"
        run: "automation_tools/install_dependencies.sh"    

      - name: Generate cooker build ID
        run: "/bin/bash ${GITHUB_WORKSPACE}/automation_tools/cooker_build_id.sh"

      - name: Get branch name
        id: branch_name
        run: echo "BRANCH_NAME=$(echo $GITHUB_REF | sed 's|refs/heads/||')" >> $GITHUB_ENV

      # if it's a feature branch it will not marked as "latest" cooker version
      - name: "Set makeLatest (cooker only)"
        run: |
          if [[ "$BRANCH_NAME" == 'feat/'* || "$BRANCH_NAME" == 'refs/'* ]]; then
            echo "MAKE_LATEST=false" >> $GITHUB_ENV
          else
            echo "MAKE_LATEST=true" >> $GITHUB_ENV
          fi

      # if the branch is coming from a PR the tag should be manually built
      - name: "Generate version tag"
        run: |
          if [[ "$BRANCH_NAME" == 'refs/'* ]]; then
            pr_number=$(echo ${{env.BRANCH_NAME}} | awk -F'/' '{print $3}')
            source_branch=$(curl -s "https://api.github.com/repos/RetroDECK/RetroDECK/pulls/$pr_number" | jq -r '.head.ref')
            source_branch=${source_branch//\//-}
            echo "[DEBUG] source branch is: $source_branch"
            echo "TAG=PR-$pr_number-$source_branch-${{ env.buildid }}" >> $GITHUB_ENV
          else
            echo "TAG=${{env.BRANCH_NAME}}-${{ env.buildid }}" >> $GITHUB_ENV
          fi
          echo "Version TAG: ${{ env.TAG }}"

      # backing up manifest in case download fails and hashes must be recalculated
      - name: Manifest backup
        run: "cp ${GITHUB_WORKSPACE}/net.retrodeck.retrodeck.yml ${GITHUB_WORKSPACE}/net.retrodeck.retrodeck.yml.bak"

      - name: Run pre-build automation tasks
        run : "/bin/bash ${GITHUB_WORKSPACE}/automation_tools/pre_build_automation.sh"

      - name: "Adding flatpak portal for automated updates (cooker only)"
        run: "/bin/bash ${GITHUB_WORKSPACE}/automation_tools/cooker_flatpak_portal_add.sh"

      - name: "Updating release notes in appdata"
        run: "automation_tools/appdata_management.sh"

      - name: "[DEBUG] Outputting manifest"
        run: cat net.retrodeck.retrodeck.yml

      - name: "Build flatpak: download only"
        id: "flatpak-download"
        run: "/bin/bash ${GITHUB_WORKSPACE}/automation_tools/flatpak_build_download_only_persistent.sh"
        continue-on-error: true

      # Sometimes flatpak download fails, in this case it tries a second time
      - name: "Build flatpak: download only (retry)"
        if: steps.flatpak-download.outcome == 'failure'
        run: |
          echo "Download failed, maybe some hash changed since the build start."
          echo "Recalculating hashes and retrying download..."
          rm -f "{GITHUB_WORKSPACE}/net.retrodeck.retrodeck.yml"
          cp "${GITHUB_WORKSPACE}/net.retrodeck.retrodeck.yml.bak" "${GITHUB_WORKSPACE}/net.retrodeck.retrodeck.yml"
          "${GITHUB_WORKSPACE}/automation_tools/pre_build_automation.sh"
          "${GITHUB_WORKSPACE}/automation_tools/flatpak_build_download_only_persistent.sh"

      - name: Build flatpak
        run: "/bin/bash ${GITHUB_WORKSPACE}/automation_tools/flatpak_build_only_persistent.sh"

      - name: Create Artifact for flathub
        run: | 
          tar -czf ${GITHUB_WORKSPACE}/RetroDECK-Artifact-cooker.tar.gz -C ${GITHUB_WORKSPACE}/retrodeck-flatpak-cooker .
          hash=($(sha256sum ${GITHUB_WORKSPACE}/RetroDECK-Artifact-cooker.tar.gz))
          echo $hash > ${GITHUB_WORKSPACE}/RetroDECK-Artifact-cooker.sha
          mv -f RetroDECK-Artifact-cooker.* ${{ secrets.ARTIFACT_REPO }}
        continue-on-error: true

      - name: Create Bundle
        run: "/bin/bash ${GITHUB_WORKSPACE}/automation_tools/flatpak_build_bundle_persistent.sh"

      - name: Set environment variable with current branch name
        run: echo "GITHUB_REF_SLUG=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV

      - name: Get commits since last release
        run: |
          # Get the latest release tag
          LATEST_TAG=$(git describe --tags --abbrev=0)
          # Get all commits since the latest release tag
          COMMITS=$(git log $LATEST_TAG..HEAD --pretty=format:"- %s")
          # Set the output variable
          echo "::set-output name=commits::$COMMITS"
        id: commits
        continue-on-error: true

      - name: Publish the flatpak in a new cooker release
        uses: ncipollo/release-action@v1
        with:
          tag: "${{env.TAG}}"
          body: |
            # Release Notes (Cooker)
            This is a cooker snapshot based on the commit: ${{ github.event.repository.full_name }}@${{github.sha}}.
            On branch [${{env.BRANCH_NAME}}](https://github.com/RetroDECK/RetroDECK/tree/${{env.BRANCH_NAME}}).

            ## Commits since last release
            ${{ steps.commits.outputs.commits }}

            For the full release note for this build please refer to the channel [#BETA-TESTING](https://discord.gg/WDc5C9YWMx) on our Discord server.
      
            Cooker channel is provided for the community to test fixes and explore new functionality.
            Please DO NOT open issues or ask support on this build.

          artifacts: "RetroDECK-cooker.flatpak,RetroDECK-Artifact.tar.gz"
          allowUpdates: true
          makeLatest: ${{env.MAKE_LATEST}} # if it's a feat branch is not considered the latest build
          token: ${{ secrets.TRIGGER_BUILD_TOKEN }}
          repo: RetroDECK-cooker
        continue-on-error: true

      # In case it cannot publish the release at least it's providing the flatpak file for creating a manual release
      - name: Upload RetroDECK-cooker.flatpak
        uses: actions/upload-artifact@v3
        with:
          name: retrodeck-flatpak
          path: RetroDECK-cooker.flatpak
        continue-on-error: true

      # - name: Upload RetroDECK-cooker.flatpak to Gitea Release
      #   run: |
      #     # Set variables for Gitea host, organization, repository, access token, and release details
      #     GITEA_HOST="repo.retrodeck.net"
      #     ORGANIZATION="RetroDECK"
      #     REPO="RetroDECK-cooker"
      #     GITEA_TOKEN="${{ secrets.GITEA_TRIGGER_BUILD_TOKEN }}"
      #     RELEASE_NAME="RetroDECK ${{env.TAG}}"
      #     TAG="${{env.TAG}}"
      #     RELEASE_BODY="# Release Notes (Cooker)
      #     This is a cooker snapshot based on the commit: ${{ github.event.repository.full_name }}@${{github.sha}}.
      #     On branch [${{env.BRANCH_NAME}}](https://repo.retrodeck.net/RetroDECK/RetroDECK/src/branch/${{env.BRANCH_NAME}}).

      #     ## Commits since last release
      #     ${{ steps.commits.outputs.commits }}

      #     For the full release note for this build please refer to the channel [#BETA-TESTING](https://discord.gg/WDc5C9YWMx) on our Discord server.

      #     Cooker channel is provided for the community to test fixes and explore new functionality.
      #     Please DO NOT open issues or ask support on this build."

      #     # Create a release using curl and capture the release ID
      #     release_response=$(curl -X POST \
      #         -H "Authorization: token ${GITEA_TOKEN}" \
      #         -H "Content-Type: application/json" \
      #         -d "{\"tag_name\":\"$TAG\",\"name\":\"$RELEASE_NAME\",\"body\":\"$RELEASE_BODY\"}" \
      #         "http://$GITEA_HOST/api/v1/repos/$ORGANIZATION/$REPO/releases")

      #     # Extract the release ID from the response
      #     release_id=$(echo $release_response | jq -r '.id')

      #     # Upload artifacts
      #     curl -X POST \
      #         -H "Authorization: token ${GITEA_TOKEN}" \
      #         -H "Content-Type: multipart/form-data" \
      #         -F "attachment=@RetroDECK-cooker.flatpak" \
      #         "http://$GITEA_HOST/api/v1/repos/$ORGANIZATION/$REPO/releases/$release_id/assets?name=RetroDECK-cooker.flatpak"

      #     curl -X POST \
      #         -H "Authorization: token ${GITEA_TOKEN}" \
      #         -H "Content-Type: multipart/form-data" \
      #         -F "attachment=@RetroDECK-Artifact.tar.gz" \
      #         "http://$GITEA_HOST/api/v1/repos/$ORGANIZATION/$REPO/releases/$release_id/assets?name=RetroDECK-Artifact.tar.gz"