#!/usr/bin/env bash
# Unmount a name from the staging area and drop it from mounts.list.
set -euo pipefail

here="$(cd "$(dirname "$(realpath "$0")")" && pwd)"
root="$(dirname "$here")"
record="$root/mounts.list"
mountctl="$here/mountctl"

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

name="$1"
"$mountctl" umount "$name"
tmp="$(mktemp)"
awk -F'\t' -v n="$name" '$2 != n' "$record" > "$tmp"
mv "$tmp" "$record"
