name: Create tag and build when new code gets to main
run-name: Create tag and build when new code gets to main
on:
  push:
    branches:
      - main
    tags-ignore:
      - "**"

env:
  GAME_NAME: GGJ26
  ITCHIO_USERNAME: Minimata
  ITCHIO_GAMEID: GGJ26


jobs:
  BumpTag:
    runs-on: ubuntu-latest
    outputs:
      tag_name: ${{ steps.bump-tag.outputs.new_tag }}
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
        with:
          lfs: false
      - name: Remove buggy pre-push hook
        run: |
          rm -f .git/hooks/pre-push          
      - name: Bump version and push tag
        id: bump-tag
        uses: anothrNick/github-tag-action@v1
        env:
          GITHUB_TOKEN: ${{ secrets.TOKEN }}
          GIT_API_TAGGING: false
          WITH_V: true
          PRERELEASE: false
          INITIAL_VERSION: 0.1.0
          DEFAULT_BUMP: patch

  Export:
    runs-on: ubuntu-latest
    needs: BumpTag
    container:
      image: barichello/godot-ci:4.6

    steps:
      - name: Install node, curl and zip
        run: |
          apt update && apt -y install curl zip nodejs          

      - name: Checkout
        uses: actions/checkout@v3

      - name: Import resources and build solution
        run: |
          godot --headless --editor --build-solutions --quit --import --path $PWD        

      - name: Setup Butler
        shell: bash
        env:
          BUTLER_API_KEY: ${{ secrets.BUTLER_TOKEN }}
        run: |
          mkdir ./tools 2>/dev/null || true
          pushd tools
            curl -sSLfo ./butler.zip "https://broth.itch.zone/butler/linux-amd64/LATEST/archive/default"
            unzip butler.zip
            chmod +x ./butler
          popd
          ./tools/butler -V

      - name: Build Windows
        run: |
          mkdir -v -p build/windows
          godot --headless --verbose --build-solutions --import --export-release "Windows" build/windows/${{ env.GAME_NAME }}.exe
          zip -r Windows.zip build/windows
      - name: Upload Windows to itch.io
        shell: bash
        env:
          BUTLER_API_KEY: ${{ secrets.BUTLER_TOKEN }}
        run: |
          versionArgument="--userversion ${{ needs.BumpTag.outputs.tag_name }}"
          ./tools/butler push \
            "Windows.zip" \
            ${{ env.ITCHIO_USERNAME }}/${{ env.ITCHIO_GAMEID }}:windows ${versionArgument}
      
      - name: Linux Build
        run: |
          mkdir -v -p build/linux
          godot --headless --verbose --export-release "Linux" build/linux/${{ env.GAME_NAME }}.x86_64
          zip -r Linux.zip build/linux
      - name: Upload Linux to itch.io
        shell: bash
        env:
          BUTLER_API_KEY: ${{ secrets.BUTLER_TOKEN }}
        run: |
          versionArgument="--userversion ${{ needs.BumpTag.outputs.tag_name }}"
          ./tools/butler push \
            "Linux.zip" \
            ${{ env.ITCHIO_USERNAME }}/${{ env.ITCHIO_GAMEID }}:linux ${versionArgument}

      - name: Web Build
        run: |
          mkdir -v -p build/web
          godot --headless --verbose --export-release "Web" build/web/index.html
      - name: Upload Web to itch.io
        shell: bash
        env:
          BUTLER_API_KEY: ${{ secrets.BUTLER_TOKEN }}
        run: |
          versionArgument="--userversion ${{ needs.BumpTag.outputs.tag_name }}"
          ./tools/butler push \
            "build/web" \
            ${{ env.ITCHIO_USERNAME }}/${{ env.ITCHIO_GAMEID }}:web ${versionArgument}
