name: itch.io Build

# By default, runs if you push to master. keeps your deployed app in sync with master branch.
on:
  workflow_dispatch:
  # push:
  #   branches:
  #     - main
  #   paths-ignore:
  #     - "README.md"
# to only run when you do a new github release, comment out above part and uncomment the below trigger.
# on:
#   release:
#     types:
#       - published

permissions:
  contents: write # for committing to gh-pages branch.

jobs:
  build-itch-io:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3 # repo checkout
      - uses: actions-rs/toolchain@v1 # get rust toolchain for wasm
        with:
          profile: minimal
          toolchain: stable
          target: wasm32-unknown-unknown
          override: true
      - name: Rust Cache # cache the rust build artefacts
        uses: Swatinem/rust-cache@v2
      - name: Download and install Trunk binary
        working-directory: crates/rpack_egui
        run: wget -qO- https://github.com/thedodd/trunk/releases/latest/download/trunk-x86_64-unknown-linux-gnu.tar.gz | tar -xzf-
      - name: Update file
        working-directory: crates/rpack_egui
        run: sed -i '15d' index.html
      - name: Build
        working-directory: crates/rpack_egui
        run: ./trunk build --release
      - name: Update result file
        working-directory: crates/rpack_egui
        run: sed -i 's|/rpack|./rpack|g' dist/index.html
      - name: Itch.io - Publish
        uses: KikimoraGames/itch-publish@v0.0.3
        with:
          gameData: ./crates/rpack_egui/dist
          itchUsername: mevlyshkin
          itchGameId: rpack
          buildChannel: wasm
          butlerApiKey: ${{ secrets.butlerApi }}
