#!/usr/bin/env bash
# Mount a host folder into the staging area so it appears inside the
# running container (which bind-mounts the staging area with rslave).
# Records the mount in mounts.list so recover-mounts can redo it.
set -euo pipefail

here="$(cd "$(dirname "$(realpath "$0")")" && pwd)"   # .../src
root="$(dirname "$here")"                              # .../opencode-workspace
staging="$root/workspace"
record="$root/mounts.list"
mountctl="$here/mountctl"

if [ "$#" -lt 1 ]; then
  echo "usage: mount-opencode <source> [name]" >&2
  exit 2
fi

src="$(realpath "$1")"
name="${2:-$(basename "$src")}"

mkdir -p "$staging/$name"
"$mountctl" mount "$src" "$name"
printf '%s\t%s\n' "$src" "$name" >> "$record"
