rinkplanner/tools/sync_assets.sh
torsten 0ac0624ba2 fix: Dokumente-Ordner sprachunabhaengig aufloesen
- neues tools/_paths.sh loest den Dokumente-Ordner zur Laufzeit auf:
  xdg-user-dir DOCUMENTS, sonst ~/Dokumente, sonst ~/Documents
- sync_assets.sh und rotoscope.sh nutzen workbench_dir statt
  $HOME/Documents/workbench_rinkplanner
- promote_rotoscope.py und blender/rotoscope.py loesen den Workbench-Pfad
  genauso auf und bevorzugen weiterhin RINKPLANNER_WORKBENCH
- Doku auf den tatsaechlichen Pfad ~/Dokumente/workbench_rinkplanner
  umgestellt
2026-11-01 13:57:02 +01:00

412 lines
12 KiB
Bash
Executable file

#!/usr/bin/env bash
# Synchronisiert externe 3D-Assets aus dem Workbench-Archiv oder per FTP.
#
# Reihenfolge beim Sync:
# 1. Ziel ist vorhanden und hat den Manifest-Hash -> OK
# 2. Workbench-Quelle ist vorhanden -> kopieren
# 3. Sonst: vom FTP-Spiegel laden (für Rechner ohne Workbench)
#
# Der FTP-Spiegel ist eine Verteilungskopie, keine zweite Quelle der Wahrheit.
# Integritätsanker ist immer der SHA-256 aus asset_manifest.json.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# shellcheck source=tools/_paths.sh
source "$REPO_ROOT/tools/_paths.sh"
PROJECT_DIR="$REPO_ROOT/rinkplanner"
MANIFEST="$PROJECT_DIR/assets/asset_manifest.json"
WORKBENCH="$(workbench_dir)"
GODOT="${GODOT:-/opt/godot/Godot_v4.7.2-stable_linux.x86_64}"
if [[ ! -x "$GODOT" && -x "/Applications/Godot.app/Contents/MacOS/Godot" ]]; then
GODOT="/Applications/Godot.app/Contents/MacOS/Godot"
fi
CREDENTIALS_FILE="${RINKPLANNER_CREDENTIALS:-$REPO_ROOT/docs/credentials.txt}"
MODE="check"
FORCE=0
ENV_FTP_HOST="${RINKPLANNER_FTP_HOST:-}"
ENV_FTP_PORT="${RINKPLANNER_FTP_PORT:-}"
ENV_FTP_USER="${RINKPLANNER_FTP_USER:-}"
ENV_FTP_PASSWORD="${RINKPLANNER_FTP_PASSWORD:-}"
ENV_FTP_ROOT="${RINKPLANNER_FTP_ROOT:-}"
ENV_FTP_ASSET_DIR="${RINKPLANNER_FTP_ASSET_DIR:-}"
ENV_FTP_VERIFY="${RINKPLANNER_FTP_VERIFY:-}"
FILE_FTP_URL=""
FILE_FTP_USER=""
FILE_FTP_PASSWORD=""
FILE_FTP_ROOT=""
FILE_FTP_ASSET_DIR=""
FILE_FTP_VERIFY=""
FILE_FTP_PORT=""
usage() {
cat <<'EOF'
Aufruf: tools/sync_assets.sh [--check|--sync|--publish] [--force]
--check Prüft nur, ob die Ziele vorhanden und hashes korrekt sind (Standard).
--sync Kopiert fehlende Assets aus der Workbench oder lädt sie per FTP
und importiert sie anschließend in Godot.
--publish Lädt die Workbench-Dateien auf den FTP-Spiegel und prüft sie dort.
--force Überschreibt vorhandene Ziele mit abweichendem Hash (nur mit --sync).
Umgebung:
RINKPLANNER_WORKBENCH Pfad zum Workbench-Archiv
(Standard: <Dokumente>/workbench_rinkplanner,
z. B. ~/Dokumente/workbench_rinkplanner)
RINKPLANNER_CREDENTIALS Pfad zur FTP-Konfiguration
(Standard: <repo>/docs/credentials.txt)
RINKPLANNER_FTP_HOST FTP-Host (überschreibt FTP_URL)
RINKPLANNER_FTP_PORT FTP-Port (Standard: 21)
RINKPLANNER_FTP_USER FTP-Benutzer
RINKPLANNER_FTP_PASSWORD FTP-Passwort
RINKPLANNER_FTP_ROOT App-Verzeichnis auf dem Server (z. B. rinkplanner)
RINKPLANNER_FTP_ASSET_DIR Asset-Verzeichnis (Standard: <ROOT>/assets)
RINKPLANNER_FTP_VERIFY 1 = TLS-Zertifikat prüfen, 0 = nicht prüfen
GODOT Godot-Binary für den Import
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--check) MODE="check" ;;
--sync) MODE="sync" ;;
--publish) MODE="publish" ;;
--force) FORCE=1 ;;
-h|--help) usage; exit 0 ;;
*) echo "Unbekannte Option: $1" >&2; usage >&2; exit 2 ;;
esac
shift
done
if [[ ! -f "$MANIFEST" ]]; then
echo "Manifest nicht gefunden: $MANIFEST" >&2
exit 1
fi
if [[ "$FORCE" -eq 1 && "$MODE" != "sync" ]]; then
echo "--force ist nur zusammen mit --sync sinnvoll." >&2
exit 2
fi
# --- FTP-Konfiguration lesen -------------------------------------------------
if [[ -f "$CREDENTIALS_FILE" ]]; then
while IFS= read -r line || [[ -n "$line" ]]; do
line="${line%$'\r'}"
[[ -z "$line" || "$line" == \#* ]] && continue
[[ "$line" != *=* ]] && continue
key="${line%%=*}"
value="${line#*=}"
value="${value#\"}"; value="${value%\"}"
value="${value#\'}"; value="${value%\'}"
case "$key" in
FTP_URL) FILE_FTP_URL="$value" ;;
FTP_USER) FILE_FTP_USER="$value" ;;
FTP_PASSWORD) FILE_FTP_PASSWORD="$value" ;;
FTP_DIRECTORY) FILE_FTP_ROOT="$value" ;;
FTP_ASSET_DIRECTORY) FILE_FTP_ASSET_DIR="$value" ;;
FTP_VERIFY) FILE_FTP_VERIFY="$value" ;;
FTP_PORT) FILE_FTP_PORT="$value" ;;
esac
done < "$CREDENTIALS_FILE"
fi
case "$FILE_FTP_URL" in
ftp://*) FILE_FTP_URL="${FILE_FTP_URL#ftp://}" ;;
ftps://*) FILE_FTP_URL="${FILE_FTP_URL#ftps://}" ;;
esac
FILE_FTP_URL="${FILE_FTP_URL%%/*}"
if [[ "$FILE_FTP_URL" == *:* ]]; then
FILE_FTP_PORT="${FILE_FTP_URL##*:}"
FILE_FTP_URL="${FILE_FTP_URL%:*}"
fi
FTP_HOST="${ENV_FTP_HOST:-$FILE_FTP_URL}"
FTP_PORT="${ENV_FTP_PORT:-${FILE_FTP_PORT:-21}}"
FTP_USER="${ENV_FTP_USER:-$FILE_FTP_USER}"
FTP_PASSWORD="${ENV_FTP_PASSWORD:-$FILE_FTP_PASSWORD}"
FTP_ROOT="${ENV_FTP_ROOT:-$FILE_FTP_ROOT}"
FTP_ASSET_DIR="${ENV_FTP_ASSET_DIR:-$FILE_FTP_ASSET_DIR}"
FTP_VERIFY="${ENV_FTP_VERIFY:-${FILE_FTP_VERIFY:-0}}"
FTP_ROOT="${FTP_ROOT#/}"; FTP_ROOT="${FTP_ROOT%/}"
if [[ -z "$FTP_ASSET_DIR" ]]; then
if [[ -n "$FTP_ROOT" ]]; then
FTP_ASSET_DIR="$FTP_ROOT/assets"
else
FTP_ASSET_DIR="assets"
fi
fi
FTP_ASSET_DIR="${FTP_ASSET_DIR#/}"; FTP_ASSET_DIR="${FTP_ASSET_DIR%/}"
REMOTE_READY=0
CURL_BASE=()
ensure_remote() {
if [[ "$REMOTE_READY" -eq 1 ]]; then
return
fi
if [[ -z "$FTP_HOST" || -z "$FTP_USER" || -z "$FTP_PASSWORD" ]]; then
echo "FTP-Zugang fehlt. Lege $CREDENTIALS_FILE an oder setze RINKPLANNER_FTP_*." >&2
exit 1
fi
CURL_BASE=(
--ssl-reqd
--silent
--show-error
--connect-timeout 20
--max-time 900
--user "$FTP_USER:$FTP_PASSWORD"
)
case "$FTP_VERIFY" in
1|true|TRUE|yes|YES)
;;
*)
CURL_BASE+=(--insecure)
echo "Hinweis: TLS-Zertifikat wird nicht geprüft; Integrität sichert der SHA-256 aus dem Manifest." >&2
;;
esac
REMOTE_READY=1
}
remote_url() {
local remote_path="$1"
remote_path="${remote_path#/}"
printf 'ftp://%s:%s/%s/%s' "$FTP_HOST" "$FTP_PORT" "$FTP_ASSET_DIR" "$remote_path"
}
remote_download() {
local remote_path="$1"
local destination="$2"
local url
url="$(remote_url "$remote_path")"
curl "${CURL_BASE[@]}" --output "$destination" "$url"
}
remote_upload() {
local remote_path="$1"
local source_path="$2"
local url
url="$(remote_url "$remote_path")"
curl "${CURL_BASE[@]}" --ftp-create-dirs --upload-file "$source_path" "$url"
}
# --- Manifest lesen ----------------------------------------------------------
rows=()
while IFS= read -r row; do
[[ -n "$row" ]] && rows+=("$row")
done < <(python3 - "$MANIFEST" <<'PY'
import json
import sys
with open(sys.argv[1], encoding="utf-8") as handle:
document = json.load(handle)
for asset in document.get("assets", []):
print("\t".join([
asset["id"],
asset["source"],
asset["target"],
asset.get("remote", ""),
asset["sha256"],
str(asset["bytes"]),
]))
PY
)
if [[ ${#rows[@]} -eq 0 ]]; then
echo "Keine Assets im Manifest." >&2
exit 1
fi
file_size() {
local size
if size="$(stat -c '%s' "$1" 2>/dev/null)"; then
printf '%s' "$size"
else
stat -f '%z' "$1"
fi
}
if command -v sha256sum >/dev/null 2>&1; then
file_sha() {
sha256sum "$1" | cut -d' ' -f1
}
else
file_sha() {
shasum -a 256 "$1" | cut -d' ' -f1
}
fi
matches_hash() {
local path="$1"
local expected_sha="$2"
local expected_bytes="$3"
[[ -f "$path" ]] || return 1
[[ "$(file_size "$path")" == "$expected_bytes" ]] || return 1
[[ "$(file_sha "$path")" == "$expected_sha" ]]
}
# --- Publish -----------------------------------------------------------------
if [[ "$MODE" == "publish" ]]; then
ensure_remote
problems=0
for row in "${rows[@]}"; do
IFS=$'\t' read -r asset_id relative_source relative_target remote_path expected_sha expected_bytes <<< "$row"
source_path="$WORKBENCH/$relative_source"
echo "[$asset_id]"
if [[ -z "$remote_path" ]]; then
echo " KEIN FTP-PFAD im Manifest." >&2
problems=$((problems + 1))
continue
fi
if ! matches_hash "$source_path" "$expected_sha" "$expected_bytes"; then
echo " QUELLE FEHLT ODER HAT FALSCHEN HASH: $source_path" >&2
problems=$((problems + 1))
continue
fi
echo " Lade hoch: $FTP_ASSET_DIR/$remote_path"
if ! remote_upload "$remote_path" "$source_path"; then
echo " UPLOAD FEHLGESCHLAGEN." >&2
problems=$((problems + 1))
continue
fi
tmp="$(mktemp "${TMPDIR:-/tmp}/rinkplanner-asset.XXXXXX")"
if ! remote_download "$remote_path" "$tmp"; then
echo " KONTROLLE FEHLGESCHLAGEN (Download)." >&2
rm -f "$tmp"
problems=$((problems + 1))
continue
fi
if ! matches_hash "$tmp" "$expected_sha" "$expected_bytes"; then
echo " KONTROLLE FEHLGESCHLAGEN (Hash)." >&2
rm -f "$tmp"
problems=$((problems + 1))
continue
fi
rm -f "$tmp"
echo " OK"
done
if [[ "$problems" -gt 0 ]]; then
echo "" >&2
echo "$problems Asset(s) nicht veröffentlicht." >&2
exit 1
fi
echo "FTP-Spiegel OK."
exit 0
fi
# --- Check und Sync ----------------------------------------------------------
problems=0
changed=0
for row in "${rows[@]}"; do
IFS=$'\t' read -r asset_id relative_source relative_target remote_path expected_sha expected_bytes <<< "$row"
source_path="$WORKBENCH/$relative_source"
target_path="$PROJECT_DIR/$relative_target"
echo "[$asset_id]"
if matches_hash "$target_path" "$expected_sha" "$expected_bytes"; then
echo " OK"
continue
fi
if [[ "$MODE" == "check" ]]; then
if [[ ! -f "$target_path" ]]; then
echo " FEHLT (Ziel): $target_path" >&2
else
echo " HASH-FEHLER (Ziel): $target_path" >&2
fi
if [[ -f "$source_path" ]]; then
echo " Mit tools/sync_assets.sh --sync aus der Workbench kopieren." >&2
else
echo " Mit tools/sync_assets.sh --sync aus dem FTP-Spiegel laden." >&2
fi
problems=$((problems + 1))
continue
fi
if [[ -f "$target_path" && "$FORCE" -ne 1 ]]; then
echo " ABWEICHUNG: $target_path" >&2
echo " Ziel hat einen anderen Hash. Mit --force überschreiben." >&2
problems=$((problems + 1))
continue
fi
if [[ -f "$source_path" ]]; then
if ! matches_hash "$source_path" "$expected_sha" "$expected_bytes"; then
echo " HASH-FEHLER (Quelle): $source_path" >&2
echo " Manifest und Workbench-Datei passen nicht zusammen." >&2
problems=$((problems + 1))
continue
fi
mkdir -p "$(dirname "$target_path")"
cp "$source_path" "$target_path"
if ! matches_hash "$target_path" "$expected_sha" "$expected_bytes"; then
echo " KOPIERFEHLER: $target_path" >&2
problems=$((problems + 1))
continue
fi
echo " Kopiert (Workbench): $relative_target"
changed=$((changed + 1))
continue
fi
if [[ -z "$remote_path" ]]; then
echo " FEHLT: weder Workbench noch FTP-Pfad vorhanden." >&2
problems=$((problems + 1))
continue
fi
ensure_remote
tmp="$(mktemp "${TMPDIR:-/tmp}/rinkplanner-asset.XXXXXX")"
echo " Workbench fehlt, lade aus FTP: $FTP_ASSET_DIR/$remote_path"
if ! remote_download "$remote_path" "$tmp"; then
echo " DOWNLOAD FEHLGESCHLAGEN." >&2
rm -f "$tmp"
problems=$((problems + 1))
continue
fi
if ! matches_hash "$tmp" "$expected_sha" "$expected_bytes"; then
echo " HASH-FEHLER (FTP): $remote_path" >&2
rm -f "$tmp"
problems=$((problems + 1))
continue
fi
mkdir -p "$(dirname "$target_path")"
mv "$tmp" "$target_path"
chmod 644 "$target_path"
echo " Geladen (FTP): $relative_target"
changed=$((changed + 1))
done
if [[ "$problems" -gt 0 ]]; then
echo "" >&2
echo "$problems Asset(s) nicht in Ordnung." >&2
exit 1
fi
if [[ "$MODE" == "sync" && "$changed" -gt 0 ]]; then
if [[ -x "$GODOT" ]]; then
echo "Godot-Import..."
"$GODOT" --headless --path "$PROJECT_DIR" --import >/dev/null
else
echo "Godot-Binary nicht gefunden: $GODOT" >&2
echo "Assets liegen bereit, Import bitte manuell anstoßen." >&2
fi
fi
echo "Assets OK."