#!/usr/bin/env bash
# Mock `install` for the ROOT-mode suite — see mock-bin-root/groupadd. Logs
# the argv to $SONOMOS_TEST_ROOT_LOG, then performs the COPY (into the sandbox
# paths the suite points every destination at) and the MODE, but not the
# ownership: an unprivileged test cannot chown to root, so the suite names
# its own uid as root (SONOMOS_TEST_ROOT_UID) instead. Only the argv shapes
# linux.sh uses are understood: [-D] [-o OWNER] [-g GROUP] [-m MODE] SRC DST.
[ -n "${SONOMOS_TEST_ROOT_LOG:-}" ] && printf 'install %s\n' "$*" >> "$SONOMOS_TEST_ROOT_LOG"
mkparents=0
mode=""
while [ "$#" -gt 2 ]; do
  case "$1" in
    -D) mkparents=1; shift ;;
    -o|-g) shift 2 ;;
    -m) mode="$2"; shift 2 ;;
    --) shift ;;
    *) echo "MOCK-install: unsupported argv: $*" >&2; exit 97 ;;
  esac
done
[ "$#" -eq 2 ] || { echo "MOCK-install: expected SRC DST" >&2; exit 97; }
[ "$mkparents" = 1 ] && mkdir -p "$(dirname "$2")"
rm -f "$2" && cp "$1" "$2" || exit 1
[ -z "$mode" ] || chmod "$mode" "$2"
