#!/usr/bin/env bash

# Contains sources from https://github.com/debuerreotype/debuerreotype

set -Eeuo pipefail

thisDir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
source "$thisDir/.constants.sh" \
	'<target-dir> <command> [args...]' \
	'rootfs apt-get update'

eval "$dgetopt"
while true; do
	flag="$1"; shift
	dgetopt-case "$flag"
	case "$flag" in
		--) break ;;
		*) eusage "unknown flag '$flag'" ;;
	esac
done

targetDir="${1:-}"; shift || eusage 'missing target-dir'
cmd="${1:-}"; shift || eusage 'missing command'
[ -n "$targetDir" ]
epoch="$(< "$targetDir/garden-epoch")"
[ -n "$epoch" ]

export targetDir epoch
unshare -f --mount bash -Eeuo pipefail -c '
	[ -n "$targetDir" ] # just to be safe
	for dir in dev proc sys; do
		if [ -e "$targetDir/$dir" ]; then
			mount --rbind "/$dir" "$targetDir/$dir"
		fi
	done
	if [[ ! -e "$targetDir/etc/resolv.conf" && -L "$targetDir/etc/resolv.conf" ]]; then
		mv "$targetDir/etc/resolv.conf" "$targetDir/etc/resolv.conf.orig"
		touch $targetDir/etc/resolv.conf
	fi
	cp /etc/resolv.conf "$targetDir/etc/resolv.conf"
	exec chroot "$targetDir" /usr/bin/env -i PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" TZ="$TZ" LC_ALL="$LC_ALL" SOURCE_DATE_EPOCH="$epoch" "$@"
' -- "$cmd" "$@"
if [[ -e "$targetDir/etc/resolv.conf.orig" || -L "$targetDir/etc/resolv.conf.orig" ]]; then
	mv "$targetDir/etc/resolv.conf.orig" "$targetDir/etc/resolv.conf"
fi
