#!/usr/bin/env bash
# Compila o Damka (MIT, Anton Medvedev; React 19 + Vite 8; damas russas, "poddavki" e "ugolki") a partir do commit
# ed824bf de github.com/antonmedv/damka, com o "npm ci" do package-lock.json do autor, "vite build --base=./" e o
# Node 24.21.0 de artifacts/tools. Mudancas (todas aqui, em Builds/damka/):
# - a interface vai para o ingles: Builds/damka/en.ts (traducao de src/i18n/ru.ts) vira o idioma de src/i18n/index.ts;
# as duas metas em russo do index.html passam para o ingles;
# - sem analytics: o plugin do Yandex Metrika sai do vite.config.ts (o build do autor injeta o tag.js do mc.yandex.ru);
# - sem som: o move.ogg/move.mp3 (um clique sem origem declarada) e o botao de som saem; o jogo continua igual;
# - o worker de busca le as tabelas de final de jogo (db/) de ao lado da pagina, nao do caminho do worker (com
# --base=./ o BASE_URL relativo apontaria para assets/db/).
# Roda so "vite build" (o "tsc -b" do autor so confere tipos). As tabelas db/ (12 MB) sao as do repositorio.
#
# src/FredGames.Bundler/Builds/build-damka.sh
#
# Saida: artifacts/build/damka-ed824bf/damka-ed824bf-site.tar.gz (pasta site/); dois builds seguidos dao os mesmos bytes.
set -euo pipefail
COMMIT=ed824bfbff291ae35d328a0b95a9a98236135ca4
SHA256=1d3e743f585ab5e71362c38ea91cc2b77d83bedaeb0076c9c4914e4c7eda9cd9
NODE=24.21.0
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
root="$(git -C "$here" rev-parse --show-toplevel 2>/dev/null || pwd)"
slug=damka
out="$root/artifacts/build/$slug-${COMMIT:0:7}"
cache="$root/artifacts/build/cache"
nodebin="$root/artifacts/tools/node-v$NODE/bin"
[ -x "$nodebin/node" ] && [ "$("$nodebin/node" -v)" = "v$NODE" ] || { echo "Precisa do Node $NODE em $nodebin." >&2; exit 1; }
export PATH="$nodebin:/usr/bin:/bin"
mkdir -p "$cache"
tarball="$cache/$slug-${COMMIT:0:7}.tar.gz"
[ -f "$tarball" ] || curl -fsSL --retry 4 "https://github.com/antonmedv/damka/archive/$COMMIT.tar.gz" -o "$tarball"
echo "$SHA256 $tarball" | shasum -a 256 -c -
rm -rf "$out"
mkdir -p "$out/tree"
tar -xzf "$tarball" -C "$out/tree" --strip-components 1
cd "$out/tree"
cp "$here/damka/en.ts" src/i18n/en.ts
python3 -I - <<'PY'
def edit(path, pairs):
s = open(path, encoding="utf-8").read()
for old, new in pairs:
assert s.count(old) == 1, (path, old)
s = s.replace(old, new)
open(path, "w", encoding="utf-8", newline="").write(s)
edit("src/i18n/index.ts", [
("import { ru } from './ru.ts'\n", "import { en } from './en.ts'\n"),
("// Only Russian for now; an English file will slot in here later.\nexport const t: Strings = ru",
"export const t: Strings = en"),
])
edit("index.html", [
('content="Шашки, поддавки и уголки в браузере."', 'content="Russian checkers, giveaway checkers and corners in the browser."'),
(' \n', ' \n'),
(' \n', ''),
])
edit("vite.config.ts", [("plugins: [react(), yandexMetrika()],", "plugins: [react()],")])
edit("src/opponents/search.worker.ts", [
("createEndgameLoader(import.meta.env.BASE_URL, EAGER_PIECES)",
"createEndgameLoader(new URL('../', self.location.href).href, EAGER_PIECES)"),
])
edit("src/sound/sound.ts", [
("import moveMp3 from './move.mp3?no-inline'\nimport moveOgg from './move.ogg?no-inline'\n", ""),
("""/** Ogg is the smaller file; Safari only decodes the mp3. */
function soundUrl(): string {
const audio = document.createElement('audio')
return audio.canPlayType('audio/ogg; codecs=vorbis') === ''
? moveMp3
: moveOgg
}
""", ""),
(""" bytes ??= fetch(soundUrl())
.then((response) => (response.ok ? response.arrayBuffer() : null))
.catch(() => null)
""", " bytes ??= Promise.resolve(null) // FredyGames: no sound file is shipped\n"),
])
edit("src/ui/NavBar.tsx", [
(" \n ", " \n }"),
])
PY
rm -f src/sound/move.ogg src/sound/move.mp3 public/manifest.webmanifest
export npm_config_cache="$cache/npm-$slug" npm_config_update_notifier=false
npm ci --ignore-scripts --no-audit --no-fund --loglevel=error
node node_modules/vite/bin/vite.js build --base=./ --outDir "$out/site" > "$out/build.log" 2>&1 || { tail -30 "$out/build.log"; exit 1; }
# so URLs de namespace (w3.org) e o texto dos erros do React (react.dev) ficam nos .js; nada de yandex nem de recurso externo
if grep -rIli "yandex" "$out/site" | grep . || grep -E "(src|href)=\"https?://" "$out/site/index.html"; then
echo "referencia externa na build" >&2; exit 1
fi
cd "$out"
find site -exec touch -t 202611010000 {} +
find site -type f | LC_ALL=C sort > files.txt
COPYFILE_DISABLE=1 tar --format=ustar --uid 0 --gid 0 --uname root --gname root -n -cf - -T files.txt | gzip -n -9 > "$slug-${COMMIT:0:7}-site.tar.gz"
grep -v "/db/" files.txt; echo "db/: $(grep -c '/db/' files.txt) arquivos"
rm -rf files.txt tree site build.log
shasum -a 256 "$slug-${COMMIT:0:7}-site.tar.gz"