on:
  push:

jobs:
  GenerateReleaseZipfile:
    runs-on: ubuntu-latest
    # To allow access to the artifact with the GITHUB_TOKEN at the last step
    permissions: read-all
    steps:
      - name: Generate some content
        run: |
          set -ex
          mkdir toto
          echo tata > toto/tata.txt
          echo titi > toto/titi.txt
          echo titi titi > "toto/titi titi.txt"
          echo tutu > tutu.txt
          echo tutu >> tutu.txt
          echo tutuuuuu >> tutu2.txt
          echo tutuuuuu >> "tutu tutu.txt"
          #echo ratata >> 'tutu tutu*.txt'
          ls -R

      - name: "Github requires checkout for 'uses: ./' (or we could hard write the repository's URL ?)"
        run: |
          mkdir myaction && cd myaction
          git init
          test -z "$GITHUB_TOKEN" && GITHUB_TOKEN="${{ github.token }}"
          MY_AUTHENTICATED_URL="$( echo "$GITHUB_SERVER_URL" | sed "s#^\(https\?://\)#\1$GITHUB_TOKEN\@#" )"
          git remote add origin "$MY_AUTHENTICATED_URL"/"$GITHUB_REPOSITORY"
          # Little and optional speed optimization
          git config --local gc.auto 0
          git fetch --no-tags --prune --no-recurse-submodules --depth=1 origin "$GITHUB_SHA"
          git reset --hard "$GITHUB_SHA"

      - name: Testing the artifact uploading
        id: "uploading"
        uses: ./myaction
        #uses: actions/upload-artifact@v4
        with:
          path: |
            toto
            tutu*

      - name: "Cleanup Github"
        run: rm -rvf myaction

      - name: Is there any output for the previous step ?
        run: |
          set -x
          printf "steps.uploading.outputs.artifact-id: %s\n" "${{ steps.uploading.outputs.artifact-id }}"
          printf "steps.uploading.outputs.artifact-url: %s\n" "${{ steps.uploading.outputs.artifact-url }}"

      - name: Check the content of the uploaded artifact
        run: |
          # Stop at first error and be verbose
          set -ex

          # Create some temporary files/directory
          DOWNLOAD_FILE="$( mktemp )"
          SHASUM_FILE="$( mktemp )"
          TEST_ARTIFACT_DIR="$( mktemp -d )"

          # Get the fingerprint of our test
          find . -type f -exec sha256sum \{\} \;  > "$SHASUM_FILE"
          # Little optional checkup
          cat "$SHASUM_FILE"

          cd "$TEST_ARTIFACT_DIR"
          # In case the repository becomes private, we add our GITHUB_TOKEN to the artifact-url.
          test -z "$GITHUB_TOKEN" && GITHUB_TOKEN="${{ github.token }}"
          # TODO : can't get it to work :-/
          #wget --header "Authorization: Bearer $GITHUB_TOKEN" -O "$DOWNLOAD_FILE" "${{ steps.uploading.outputs.artifact-url }}"

          TMP_WGET_OUTPUT="$( mktemp )"
          wget -O "$DOWNLOAD_FILE" --header "Authorization: token $GITHUB_TOKEN" "https://api.github.com/repos/$GITHUB_REPOSITORY/actions/artifacts/${{ steps.uploading.outputs.artifact-id }}/zip" 2>"$TMP_WGET_OUTPUT" || (
            wget -O "$DOWNLOAD_FILE" "$( sed -n '/^Location: /s/^Location: //p' "$TMP_WGET_OUTPUT" | tail -n 1 )"
          )
          unzip "$DOWNLOAD_FILE"
          sha256sum -c "$SHASUM_FILE"

          # Cleanup
          cd -
          rm -f "$DOWNLOAD_FILE"
          rm -f "$SHASUM_FILE"
          rm -rf "$TEST_ARTIFACT_DIR"