Independent version counters per landing footer with last-change time and linkified commit; stamp script takes bank|exchange|merchant|--all.
193 lines
5.9 KiB
Bash
Executable file
193 lines
5.9 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Stamp landing footers per site: version + last content change + linkified commit.
|
|
#
|
|
# Sites: bank | exchange | merchant
|
|
#
|
|
# Usage (from koopa-admin-log root):
|
|
# ./scripts/taler-landing/stamp-landing-version.sh bank
|
|
# re-stamp bank with current version + HEAD
|
|
# ./scripts/taler-landing/stamp-landing-version.sh --bump bank
|
|
# bank version += 1, then stamp HEAD
|
|
# ./scripts/taler-landing/stamp-landing-version.sh --set 42 exchange
|
|
# force exchange version to 42
|
|
# ./scripts/taler-landing/stamp-landing-version.sh --all
|
|
# re-stamp all sites (no bump)
|
|
# ./scripts/taler-landing/stamp-landing-version.sh --bump --all
|
|
# bump every site by 1 (rare)
|
|
#
|
|
# Typical flow after editing one site:
|
|
# 1) edit configs/bank-landing/…
|
|
# 2) ./scripts/taler-landing/stamp-landing-version.sh --bump bank
|
|
# 3) git add configs/ && git commit -m "landing(bank): …"
|
|
# 4) ./scripts/taler-landing/stamp-landing-version.sh bank
|
|
# git add configs/ && git commit --amend --no-edit # optional pin
|
|
set -euo pipefail
|
|
|
|
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
|
VER_FILE="$ROOT/configs/shared/landing-version.json"
|
|
REPO_WEB="${LANDING_REPO_WEB:-https://git.hacktivism.ch/hernani/koopa-admin-log}"
|
|
|
|
declare -A SITE_HTML=(
|
|
[bank]="$ROOT/configs/bank-landing/index.html"
|
|
[exchange]="$ROOT/configs/exchange-landing/index.html"
|
|
[merchant]="$ROOT/configs/merchant-landing/index.html"
|
|
)
|
|
declare -A SITE_LABEL=(
|
|
[bank]="Bank landing"
|
|
[exchange]="Exchange landing"
|
|
[merchant]="Merchant landing"
|
|
)
|
|
declare -A SITE_PATH=(
|
|
[bank]="configs/bank-landing"
|
|
[exchange]="configs/exchange-landing"
|
|
[merchant]="configs/merchant-landing"
|
|
)
|
|
|
|
export TZ="${TZ:-Europe/Zurich}"
|
|
|
|
bump=0
|
|
set_ver=""
|
|
do_all=0
|
|
sites=()
|
|
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--bump) bump=1; shift ;;
|
|
--set) set_ver="${2:-}"; shift 2 ;;
|
|
--all) do_all=1; shift ;;
|
|
bank|exchange|merchant) sites+=("$1"); shift ;;
|
|
-h|--help)
|
|
sed -n '2,28p' "$0"
|
|
exit 0
|
|
;;
|
|
*) echo "unknown arg: $1 (sites: bank exchange merchant)" >&2; exit 2 ;;
|
|
esac
|
|
done
|
|
|
|
if [ "$do_all" = 1 ]; then
|
|
sites=(bank exchange merchant)
|
|
fi
|
|
if [ "${#sites[@]}" -eq 0 ]; then
|
|
echo "usage: $0 [--bump|--set N] bank|exchange|merchant|--all" >&2
|
|
exit 2
|
|
fi
|
|
|
|
if [ ! -f "$VER_FILE" ]; then
|
|
cat >"$VER_FILE" <<'EOF'
|
|
{"note":"per-site","sites":{"bank":{"version":42},"exchange":{"version":42},"merchant":{"version":42}}}
|
|
EOF
|
|
fi
|
|
|
|
if [ -n "${OVERRIDE_COMMIT:-}" ]; then
|
|
full="$OVERRIDE_COMMIT"
|
|
else
|
|
full=$(git -C "$ROOT" rev-parse HEAD 2>/dev/null || echo "unknown")
|
|
fi
|
|
short=$(git -C "$ROOT" rev-parse --short=7 "$full" 2>/dev/null || echo "${full:0:7}")
|
|
iso=$(date +%Y-%m-%dT%H:%M:%S%z | sed -E 's/([+-][0-9]{2})([0-9]{2})$/\1:\2/')
|
|
human=$(date +"%Y-%m-%d %H:%M %Z")
|
|
url="${REPO_WEB}/commit/${full}"
|
|
|
|
css_snip=' .landing-rev {
|
|
margin: 0.75rem 0 0;
|
|
font-size: 0.72rem;
|
|
color: var(--muted, #9a8fb0);
|
|
line-height: 1.45;
|
|
text-align: center;
|
|
}
|
|
.landing-rev a { color: inherit; text-decoration: underline; text-underline-offset: 2px; opacity: 0.95; }
|
|
.landing-rev strong { font-weight: 750; }
|
|
'
|
|
|
|
for site in "${sites[@]}"; do
|
|
html="${SITE_HTML[$site]:-}"
|
|
label="${SITE_LABEL[$site]:-}"
|
|
relpath="${SITE_PATH[$site]:-}"
|
|
[ -n "$html" ] && [ -f "$html" ] || { echo "missing site/html: $site" >&2; exit 1; }
|
|
|
|
# resolve version for this site
|
|
version=$(python3 - "$VER_FILE" "$site" "$bump" "$set_ver" <<'PY'
|
|
import json, sys
|
|
path, site, bump, set_ver = sys.argv[1:5]
|
|
data = json.load(open(path, encoding="utf-8"))
|
|
sites = data.setdefault("sites", {})
|
|
entry = sites.setdefault(site, {"version": 42})
|
|
v = int(entry.get("version", 42))
|
|
if set_ver:
|
|
v = int(set_ver)
|
|
elif bump == "1":
|
|
v += 1
|
|
print(v)
|
|
PY
|
|
)
|
|
|
|
# update JSON entry for this site (preserve others)
|
|
python3 - "$VER_FILE" "$site" "$version" "$label" "$relpath" "$iso" "$human" "$full" "$short" "$url" <<'PY'
|
|
import json, sys
|
|
path, site, version, label, relpath, iso, human, full, short, url = sys.argv[1:]
|
|
data = json.load(open(path, encoding="utf-8"))
|
|
data["note"] = (
|
|
"Per-site landing content versions (start 42). "
|
|
"Bump: scripts/taler-landing/stamp-landing-version.sh --bump bank|exchange|merchant"
|
|
)
|
|
sites = data.setdefault("sites", {})
|
|
sites[site] = {
|
|
"version": int(version),
|
|
"label": label,
|
|
"path": relpath,
|
|
"updated_iso": iso,
|
|
"updated_human": human,
|
|
"commit": full,
|
|
"commit_short": short,
|
|
"repo_commit_url": url,
|
|
}
|
|
open(path, "w", encoding="utf-8").write(json.dumps(data, indent=2, ensure_ascii=False) + "\n")
|
|
print(f"{site}: v{version} commit={short} at={human}")
|
|
PY
|
|
|
|
block=$(cat <<EOF
|
|
<!-- landing-rev -->
|
|
<p class="landing-rev" data-landing-site="${site}" data-landing-version="${version}" data-landing-commit="${short}">
|
|
${label} <strong>v${version}</strong>
|
|
· content <time datetime="${iso}">${human}</time>
|
|
· <a href="${url}" title="koopa-admin-log ${full}">${short}</a>
|
|
</p>
|
|
<!-- /landing-rev -->
|
|
EOF
|
|
)
|
|
|
|
# ensure CSS once
|
|
if ! grep -q '\.landing-rev' "$html"; then
|
|
python3 - "$html" "$css_snip" <<'PY'
|
|
import sys
|
|
path, snip = sys.argv[1], sys.argv[2]
|
|
t = open(path, encoding="utf-8").read()
|
|
if ".landing-rev" in t:
|
|
raise SystemExit(0)
|
|
idx = t.find("</style>")
|
|
if idx < 0:
|
|
raise SystemExit(f"no </style> in {path}")
|
|
open(path, "w", encoding="utf-8").write(t[:idx] + snip + "\n" + t[idx:])
|
|
print(f"css → {path}")
|
|
PY
|
|
fi
|
|
|
|
python3 - "$html" "$block" <<'PY'
|
|
import sys, re
|
|
path, block = sys.argv[1], sys.argv[2]
|
|
t = open(path, encoding="utf-8").read()
|
|
block = block.rstrip() + "\n"
|
|
pat = re.compile(r"[ \t]*<!-- landing-rev -->.*?<!-- /landing-rev -->\n?", re.S)
|
|
if pat.search(t):
|
|
t = pat.sub(block, t, count=1)
|
|
else:
|
|
t2, n = re.subn(r"(\n)([ \t]*</footer>)", r"\1" + block + r"\2", t, count=1)
|
|
if n != 1:
|
|
raise SystemExit(f"could not insert landing-rev in {path}")
|
|
t = t2
|
|
open(path, "w", encoding="utf-8").write(t)
|
|
print(f"stamp → {path}")
|
|
PY
|
|
done
|
|
|
|
echo "OK stamped: ${sites[*]}"
|