#!/bin/ksh

set -eu

usage() {
	xargs 1>&2 <<-EOF
	usage: robsd-crossenv target utility [argument ...]
	EOF
	exit 1
}

. "${EXECDIR:-/usr/local/libexec/robsd}/util.sh"

setmode "robsd-cross"
setprogname "robsd-crossenv"

[ $# -lt 2 ] && usage

_target="$1"; shift

config_load -v "target=${_target}" <<'EOF'
BSDSRCDIR="${bsd-srcdir}"
CROSSDIR="${crossdir}"
EOF

if ! [ -d "${CROSSDIR}" ]; then
	fatal "${CROSSDIR}: no such directory"
fi

_env="$(make -f "${BSDSRCDIR}/Makefile.cross" \
	"TARGET=${_target}" "CROSSDIR=${CROSSDIR}" cross-env)"
eval "export ${_env} BSDSRCDIR=${BSDSRCDIR}"
"$@"
