#!/bin/sh
# apply-overlay.sh — Wild Dragon reskin applied to a freshly cloned
# datarhei/restreamer-ui tree. Two phases:
#
# 1. File overlay: cp the contents of $OVERLAY/{public,src} on top of
# the upstream working tree. Whole-file replacements only — simple
# and idempotent. Modified views (Edit/index.js, Main/index.js) and
# all new components (WHEP.js, WHEPStatus.js, Logo/) live here.
#
# 2. Targeted awk patches for one-line UI strings that aren't worth a
# whole-file overlay (the header title, welcome strings). Each
# pattern is anchored to unique surrounding context so a future
# upstream rename fails loudly rather than silently no-op'ing.
#
# Caller: the Dockerfile ui-builder stage. Expects:
# $OVERLAY = /overlay (repo overlay/ COPYd into the build)
# $UI = /ui (cloned upstream source root)
#
# Idempotent on a single source tree (re-running is a no-op).
set -eu
OVERLAY="${OVERLAY:-/overlay}"
UI="${UI:-/ui}"
echo "wilddragon-overlay: layering $OVERLAY -> $UI"
# Phase 1 — file copies. -L follows symlinks, -p preserves perms,
# -R recursive. --delete is intentionally omitted: the upstream tree
# stays intact except for the files we override.
for sub in public src; do
if [ -d "$OVERLAY/$sub" ]; then
cp -RLp "$OVERLAY/$sub/." "$UI/$sub/"
fi
done
# Phase 2 — targeted awk patches. Fails loudly if the needle is missing
# (upstream changed the patched line).
patch_line() {
file="$1"; needle="$2"; replacement="$3"
if ! grep -qF "$needle" "$file"; then
echo "wilddragon-overlay: ERROR — pattern not found in $file:"
echo " $needle"
exit 1
fi
tmp="$(mktemp)"
awk -v n="$needle" -v r="$replacement" '
index($0, n) { sub(n, r); }
{ print }
' "$file" > "$tmp"
mv "$tmp" "$file"
echo "wilddragon-overlay: patched $(basename "$file") — $needle -> $replacement"
}
patch_line "$UI/src/Header.js" \
'Restreamer' \
'Wild Dragon'
patch_line "$UI/src/views/Welcome.js" \
'title="Welcome to Restreamer v2"' \
'title="Welcome to Wild Dragon"'
patch_line "$UI/src/views/Settings.js" \
'title="Welcome to Restreamer v2"' \
'title="Welcome to Wild Dragon"'
echo "wilddragon-overlay: done."