#!/bin/bash
# Note: using bash to support not posix printf -v, which is used to escape args. 
# The posix alternative was causing pid leaks.

# Usage: <script> /tmp/filename "command cmdparam1 cmdparamN"

DNS_ADDRESS=$1
shift

TEST_RESOLVCONF=$1
shift

USER=$1
shift

COMMAND=$1
shift

echo nameserver $DNS_ADDRESS > $TEST_RESOLVCONF

# Escape args, otherwise exec command below fails.
printf -v ARGS "%q " "$@"

unshare -m sh -s <<EOF
	mount --bind $TEST_RESOLVCONF /etc/resolv.conf
	sudo -u $USER $COMMAND $ARGS 
EOF