#!/bin/sh

export LC_ALL=C LANG=C LANGUAGE=C

PROG="${0##*/}"

# base directories
vzdir=/etc/vz
config_dir=$vzdir/conf

# base options
config=
origin=

Info()
{
	printf %s\\n "$PROG: $*" >&2
}

verbose=
Verbose()
{
	[ -n "$verbose" ] || return 0
	Info "$*"
}

Fatal()
{
	printf %s\\n "$PROG: Fatal $*" >&2
	exit 1
}

show_usage()
{
	[ -z "$*" ] || Info "$*"
	printf %s\\n "Try \`$PROG --help' for more information." >&2
	exit 1
}

exit_handler() 
{
	local rc=$?
	trap - EXIT
	exit $rc
}

show_help()
{
    [ -z "$*" ] || Info "$*"
    cat <<EOF
$PROG - make OpenVZ config template of custom VE

Usage: $PROG [options]

Valid options are:
  --create=NAME		   create custom ve- config template with name NAME.
  --origin=ORIGIN          name of origin config to use.
  -h, --help               show this text and exit

Report bugs to http://bugs.altlinux.org/

EOF
    exit
}

TEMP=`getopt -n $PROG -o p:,q,h -l create:,origin:,help -- "$@"` || show_usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		--create) shift
		[ -z "$1" ] || config="$1"
		;;
		--origin) shift; origin="$1"
		;;
		-h|--help) show_help
		;;
		--) shift; break
		;;
		*) Fatal "Unrecognized option: $1"
		;;
	esac
	shift
done

[ "$name" ] || show_usage 'Config name not defined.'
[ "$origin" -a -f "$config_dir/ve-$origin.conf-sample" ] || show_usage 'Origin config template not found.'

trap exit_handler HUP PIPE INT QUIT TERM EXIT

cp -af $config_dir/ve-$origin.conf-sample $config_dir/ve-$config.conf-sample

