name: derive
on:
  workflow_dispatch:
    inputs:
      game:
        description: "Game key"
        required: true
        default: cs2
        type: choice
        options:
          - cs2
          - dota2

concurrency:
  group: derive-${{ github.event.inputs.game }}
  cancel-in-progress: false

jobs:
  derive:
    runs-on: s2-runner
    env:
      GAME: ${{ github.event.inputs.game }}
      STEAM_APPS: /home/cs2/.steam/SteamApps
      STEAM_USER: source2rosetta
      RELEASE_BASE: ${{ github.server_url }}/${{ github.repository }}/releases/download
      OVERRIDE_DIR: /home/cs2/rosetta-override
    steps:
      - uses: actions/checkout@v6.0.2

      - name: Update the install to the current build
        run: |
          case "$GAME" in
            cs2)   APPID=730 ;;
            dota2) APPID=570 ;;
            *) echo "unknown game '$GAME' (expected cs2 or dota2)"; exit 1 ;;
          esac
          echo "APPID=$APPID" >> "$GITHUB_ENV"
          steamcmd +login "$STEAM_USER" +app_update "$APPID" +quit

      - name: Resolve the game paths + the new buildid
        run: |
          MANIFEST="$STEAM_APPS/appmanifest_$APPID.acf"
          [ -f "$MANIFEST" ] || { echo "no manifest at $MANIFEST — check STEAM_APPS"; exit 1; }
          INSTALLDIR=$(grep -oP '"installdir"\s+"\K[^"]+' "$MANIFEST")
          BUILDID=$(grep -oP '"buildid"\s+"\K[0-9]+' "$MANIFEST")
          GAME_DIR="$STEAM_APPS/common/$INSTALLDIR/game"
          [ -d "$GAME_DIR" ] || { echo "expected a game tree at '$GAME_DIR' — check STEAM_APPS"; exit 1; }
          { echo "BUILDID=$BUILDID"; echo "GAME_DIR=$GAME_DIR"; } >> "$GITHUB_ENV"

      - name: Build the deriver
        run: cargo build --release

      - name: Fetch the previous model + seed (or take a manually-placed override)
        run: |
          mkdir -p in dist work
          OVR="$OVERRIDE_DIR/$GAME"
          for stem in "model-$GAME.json" "seed-$GAME.json"; do
            if [ -f "$OVR/$stem" ]; then
              echo "::warning::baseline OVERRIDE in use for $stem — taken from $OVR, not the $GAME-latest release"
              cp "$OVR/$stem" "in/$stem"
              echo "OVERRIDE_USED=1" >> "$GITHUB_ENV"
            else
              curl -fsSL -o "in/$stem.gz" "$RELEASE_BASE/$GAME-latest/$stem.gz"
              gunzip -c "in/$stem.gz" > "in/$stem"
            fi
          done

      - name: Produce — derive + validate-live + typed netvars + fold model N -> N+1
        run: |
          ln -sfn "$GAME_DIR" "work/$BUILDID"
          ./target/release/source2rosetta --game "$GAME" produce \
            --seed         "in/seed-$GAME.json" \
            --corpus-model "in/model-$GAME.json" \
            --prototypes   mappings/prototypes.json \
            --semantics    "mappings/semantics-$GAME.json" \
            --ehandle-classes mappings/ehandle-classes.json \
            --target       "work/$BUILDID" \
            --game-dir     "$GAME_DIR" \
            --version      "$GAME-$BUILDID-0" \
            --out-dir      dist
          cp "in/seed-$GAME.json" "dist/seed-$GAME.json"

      - name: Compress the internal artifacts for release
        run: |
          gzip -6 "dist/model-$GAME.json"
          gzip -6 "dist/seed-$GAME.json"

      - name: Publish the immutable per-build snapshot
        uses: https://code.forgejo.org/actions/forgejo-release@v2
        with:
          direction: upload
          url: ${{ github.server_url }}
          repo: ${{ github.repository }}
          tag: ${{ env.GAME }}-${{ env.BUILDID }}-0
          release-dir: dist
          token: ${{ secrets.GITHUB_TOKEN }}
          override: true
          release-notes: "${{ env.GAME }} gamedata for Steam build ${{ env.BUILDID }}, derived by source2rosetta. Patch 0 = the game-update build; later patches on this buildid come from contribution PRs."

      - name: Move <game>-latest to this build
        uses: https://code.forgejo.org/actions/forgejo-release@v2
        with:
          direction: upload
          url: ${{ github.server_url }}
          repo: ${{ github.repository }}
          tag: ${{ env.GAME }}-latest
          release-dir: dist
          token: ${{ secrets.GITHUB_TOKEN }}
          override: true
          release-notes: "Rolling ${{ env.GAME }} gamedata — always the newest build (currently ${{ env.BUILDID }}). Stable URL; assets overwritten each update."

      - name: Consume the baseline override
        if: env.OVERRIDE_USED != ''
        run: |
          rm -f "$OVERRIDE_DIR/$GAME"/*
          echo "override consumed — subsequent $GAME runs resume from the $GAME-latest release"
