#!/bin/bash -e
### Copyright 1999-2026. WebPros International GmbH. All rights reserved.

usage()
{
	if [ -n "$*" ]; then
		echo "run-with: $*" >&2
		exit 2
	fi

	cat <<-EOT
	Usage: run-with [OPTIONS] [--] COMMAND [ARG ...]
	Run COMMAND with changes to how it's executed.

	  --stdin  FILE             Redirect stdin from FILE.
	  --stdout FILE             Redirect stdout to FILE.
	  --stderr FILE             Redirect stderr to FILE.
	  --timeout DURATION        Command execution timeout.
	  --stop-timeout DURATION   Kill command after the initial attempt to stop it,
	                            if it's still running after this timeout.
	                            This option is ignored if --timeout is not set.
	  
	  FILE is an absolute file path.
	  DURATION is a floating point number with an optional suffix: 's' for seconds
	  (the default), 'm' for minutes, 'h' for hours or 'd' for days.
	EOT
	exit 2
}

# --- parse args ---

opt_stdin=
opt_stdout=
opt_stderr=
opt_run_timeout=
opt_stop_timeout=

TEMP=`getopt -o h --long stdin:,stdout:,stderr:,timeout:,stop-timeout:,help \
	-n run-with -- "$@"` || exit 2
eval set -- "$TEMP"

while [ "$#" -gt 0 ]; do
	case "$1" in
		--stdin)
			opt_stdin="$2"
			shift 2
			;;
		--stdout)
			opt_stdout="$2"
			shift 2
			;;
		--stderr)
			opt_stderr="$2"
			shift 2
			;;
		--timeout)
			opt_run_timeout="$2"
			shift 2
			;;
		--stop-timeout)
			opt_stop_timeout="$2"
			shift 2
			;;
		-h|--help)
			usage
			;;
		--)
			shift
			break
			;;
		*)
			usage "unhandled argument '$1'"
			;;
	esac
done

[ -n "$*" ] || usage "command is empty"
! [ -n "$opt_stop_timeout" -a -z "$opt_run_timeout" ] || opt_stop_timeout=

# --- execute ---

[ -z "$opt_stdin"  ] || exec 0<"$opt_stdin"
[ -z "$opt_stdout" ] || exec 1>"$opt_stdout"
[ -z "$opt_stderr" ] || exec 2>"$opt_stderr"

if [ -n "$opt_run_timeout" ]; then
	exec /usr/bin/timeout ${opt_stop_timeout:+--kill-after "$opt_stop_timeout"} "$opt_run_timeout" "$@"
else
	exec "$@"
fi
