#!/bin/sh
# 20260228 ChatGPT
# $Header$
#
# Copy/paste:
#   cd ~/work/Voron/renderlab/web/batch_glb_png
#   chmod +x link_glbs.sh
#   ./link_glbs.sh /home/jlpoole/work/Voron/test1
#
# This creates symlinks under ./glbs/ pointing to every *.glb under ROOT.

set -eu

ROOT="${1:-}"
[ -n "$ROOT" ] || { echo "Usage: $0 /path/to/root" >&2; exit 2; }
[ -d "$ROOT" ] || { echo "ERROR: not a directory: $ROOT" >&2; exit 2; }

OUTDIR="./glbs"
mkdir -p "$OUTDIR"

ts="$(date +%Y%m%d_%H%M%S)"
log="link_glbs_${ts}.log"

# Make ROOT absolute and strip any trailing slash for consistent relpath math.
ROOT_ABS="$(cd "$ROOT" && pwd)"
ROOT_ABS="${ROOT_ABS%/}"

echo "ROOT:    $ROOT_ABS"    | tee "$log"
echo "OUTDIR:  $(cd "$OUTDIR" && pwd)" | tee -a "$log"
echo "LOG:     $log"         | tee -a "$log"
echo ""                     | tee -a "$log"

# Sanitize: turn a path into a filesystem-safe basename.
# Example:
#   Klicky-Probe/Printers/Voron/.../Dock_mount_fixed_v2.stl.glb
# becomes:
#   Klicky-Probe__Printers__Voron__...__Dock_mount_fixed_v2.stl.glb
sanitize() {
  # shellcheck disable=SC2001
  echo "$1" \
  | sed 's|^/||' \
  | sed 's|/|__|g' \
  | sed 's|[^A-Za-z0-9._-]|_|g'
}

count=0
skipped=0
collisions=0

# Use -print0 to handle spaces safely.
find "$ROOT_ABS" -type f -name '*.glb' -print0 \
| while IFS= read -r -d '' f; do
    # Compute relative path under ROOT_ABS
    rel="${f#$ROOT_ABS/}"

    name="$(sanitize "$rel")"
    linkpath="$OUTDIR/$name"

    # If the link exists and points to the same target, skip quietly.
    if [ -L "$linkpath" ]; then
      target="$(readlink "$linkpath" || true)"
      if [ "$target" = "$f" ]; then
        echo "SKIP (already linked): $linkpath -> $f" >> "$log"
        skipped=$((skipped+1))
        continue
      fi
    fi

    # If the name already exists but points elsewhere, disambiguate.
    if [ -e "$linkpath" ]; then
      i=1
      base="$linkpath"
      while [ -e "$linkpath" ]; do
        linkpath="${base%.glb}_$i.glb"
        i=$((i+1))
      done
      collisions=$((collisions+1))
    fi

    ln -s "$f" "$linkpath"
    echo "LINK: $linkpath -> $f" >> "$log"
    count=$((count+1))
  done

# The while loop runs in a subshell in many /bin/sh implementations,
# so counters above may not update. Summarize by counting log lines instead.
linked_lines="$(grep -c '^LINK: ' "$log" 2>/dev/null || echo 0)"
skipped_lines="$(grep -c '^SKIP ' "$log" 2>/dev/null || echo 0)"

echo "" | tee -a "$log"
echo "DONE. linked=$linked_lines skipped=$skipped_lines (details in $log)" | tee -a "$log"
