#!/bin/sh

set -e

_PROGRAM="${1}"

if [ -n "${_PROGRAM}" ]
then
	shift
else
	echo "Usage: ${0} PROGRAM CONTAINER"
	exit 1
fi

if [ ! -x "$(which lxc-${_PROGRAM} 2>/dev/null)" ]
then
	echo "E: lxc-${_PROGRAM} - no such program" >&2
	exit 1
fi

_CONTAINER="${1}"

if [ -n "${_CONTAINER}" ]
then
	shift

	if ! lxc-ls | grep -qs ^${_CONTAINER}$
	then
		echo "E: ${_CONTAINER} - no such container" >&2
		exit 1
	fi

	_OPTIONS="--name ${_CONTAINER}"
else
	case "${_PROGRAM}" in
		backup|list|restore)
			;;

		*)
			echo "Usage: ${0} PROGRAM CONTAINER"
			exit 1
			;;
	esac
fi

lxc-${_PROGRAM} ${_OPTIONS} ${@}
