#!/bin/sh -e

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

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

# Create Var_of_$1 variable and set it to $2 if exist, otherwise to $1
Helpify() # var_name=var_value [cmdline_key_name]
{
	eval "Var_of_${1%%=*}='
                                    (\$${2:-${1%%=*}})'"
}

Verbose()
{
	[ -n "$verbose" ] || return 0
	printf %s\\n "${0##*/}: $*" >&2
}

show_version()
{
	cat <<EOF
$PROG, a part of separator suite, generation 3, version 1.0

Written by:
	Mikhail Yakshin <greycat@altlinux.org>

Based on ideas and code from previous projects by:
	Anton Farygin <rider@altlinux.ru>
	Anton D. Kachalov <mouse@altlinux.org>
	L.A. Kostis <lakostis@altlinux.org>
	Dmitry V. Levin <ldv@altlinux.org>

Copyright (C) 2006  Mikhail Yakshin <greycat@altlinux.org>
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
	exit
}

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

show_help()
{
	cat <<EOF
Usage: $PROG [options]

Valid options are:
  -w, --work=DIRECTORY     work directory
  -o, --options            override options from profile config (name=value)
  -q, --quiet              try to be more quiet
  -v, --verbose            print a message from each action
  -d, --debug              enable debug messages
  -V, --version            print program version and exit
  -h, --help               show this text and exit

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

EOF
	exit
}

# Runs single command in chroot
chroot_run()
{
	hsh-run $verbose $quiet --rooter "$work_dir" -- "$@"
}

# Runs prepared script
chroot_exec()
{
	hsh-run $verbose $quiet --rooter --execute="$@" "$work_dir"
}

# Install package(s) into chroot
chroot_install()
{
	hsh-install "$work_dir" \
		${verbose:+$verbose} \
		${quiet:+$quiet} \
		${excludedocs:+$excludedocs} \
		$@ \
		&& Verbose "Success: install <$@> in chroot" \
		|| Fatal "Failed: install <$@> in chroot"
}
