#!/bin/sh
# Install the wireBender layout where the Hydrogen 3D editor looks for
# layouts ($HOME/project/project-content/layouts/), and the node firmware
# that runs the bench into project-content/firmware/.
set -e

LAYOUT_DIR="$HOME/project/project-content/layouts"
FIRMWARE_DIR="$HOME/project/project-content/firmware"
TARGET="$LAYOUT_DIR/wireBender.json"

mkdir -p "$LAYOUT_DIR" "$FIRMWARE_DIR"

# Preserve local edits: if a different wireBender.json is already there
# (for example one the user re-saved from the editor), keep a backup
# beside it before overwriting with the packaged layout.
if [ -f "$TARGET" ] && ! cmp -s "$PWD/wireBender.json" "$TARGET"; then
  cp "$TARGET" "$TARGET.bak"
  echo "existing wireBender.json differed; saved a copy at $TARGET.bak"
fi

cp "$PWD/wireBender.json" "$TARGET"
echo "installed: wireBender layout at $TARGET"

for fw in "$PWD"/firmware/*.uf2; do
  [ -f "$fw" ] || continue
  cp "$fw" "$FIRMWARE_DIR/"
  echo "installed: $(basename "$fw") in $FIRMWARE_DIR"
done

echo "open the layout from the 3D Editor's layout picker, or ask the AI:"
echo "  \"open the wireBender layout in the 3D editor\""
echo "flash the node firmware to the eval frames before running the bench."