#!/bin/sh -efu

. shell-args
. shell-error
. shell-quote

# our tools
toolsdir=/usr/share/mkve/tools
mkve_config=$toolsdir/mkve-config
mkve_pack_kvm=$toolsdir/mkve-pack-kvm
mkve_pack_openvz=$toolsdir/mkve-pack-openvz

###############################################################################
#
# __copycat: copy section:name:value from one info file to another
# copycat: __copycat($info, $workdir/info, ...)
#
# $1 = source
# $2 = destination
# $3 = section
# $4 = name
# $5 = critical flag
#
__copycat()
{
	local src="$1" dst="$2" sect="$3" name="$4" crt="$5"
	local value="$($mkve_config get "$src" "$sect" "$name" 2>/dev/null)"

	if [ -z "$value" -a -n "$crt" ]; then
		fatal "__copycat: can't get ($sect:$name) from $src but critical flag is set"
	elif [ -n "$value" ]; then
		$mkve_config set "$dst" "$sect" "$name" "$value" ||
		fatal "__copycat: failed on ($1,$2,$3,$4,$5)"
	fi
}

copycat()
{
	local crt=
	if [ "$1" = "-c" ]; then crt="yes"; shift; fi

	__copycat "$info" "info" "$1" "$2" "$crt"
}


###############################################################################
#
# check_non_emptyness: check, that given option is non-zero
#
# return value: run fatal() on error
#
# $1 = name of the option
# $2 = option itself
#
check_non_emptyness()
{
	[ -n "${2// /}" ] ||
		fatal "$1 is unset or empty"
}

###############################################################################
#
# check_ctid: check, that given id is a valid openvz ctid
#
# return value: prints out matched "$ctid"
#               run fatal() on error
#
# $1 = id to check
#
check_ctid()
{
	local ctid

	check_non_emptyness '<ctid>' "$1"
	for ctid in $(vzlist --all -H -o ctid); do
		if [ "$1" = "$ctid" ]; then
			printf '%s' "$ctid"
			return
		fi
	done
	fatal "ctid '$1' is not found"
}

###############################################################################
#
# check_output: given a name return full path corresponding to that name
#
# return value: will fail if an argument is empty
#
# $1 = output file
#
check_output()
{
	local output="$1"

	check_non_emptyness '<output>' "$output"
	if [ -f "$output" ]; then
		verbose "file $output already exist"
		output="$(realpath "$output")"
	elif [ "${output##/}" = "$output" ]; then
		output="$(realpath .)/$output"
	fi

	printf '%s' "$output"
}

###############################################################################
#
# mk_workdir: create a temporary working directory into directory $1.
#
# return value: name of directory
#               function will fail if an argument is empty
#
# $1 = a directory where to create workdir
#
mk_workdir()
{
	local ret

	check_non_emptyness '<mk_workdir argument>' "$1"
	mkdir -p "$1"
	ret=$(mktemp --directory --tmpdir="$1" XXXXXX 2>&1) ||
		fatal "mk_workdir: $ret"
	printf '%s' "$ret"
}

###############################################################################
#
# cleanup_openvz_config: delete lines that must be deleted
#
# return value: none
#
# $1 = an openvz config file to cleanup
#
cleanup_openvz_config()
{
	local conf="$1"
	[ -f "$conf" ] || fatal "$conf: no such file"

	sed -i "$conf" \
		-e '/^$/d' \
		-e '/^#.*$/d' \
		-e '/^VERSION=.*/d' \
		-e '/^VE_ROOT=.*/d' \
		-e '/^VE_PRIVATE=.*/d' \
		-e '/^OSTEMPLATE=.*/d' \
		-e '/^ORIGIN_SAMPLE=.*/d' \
		-e '/^NAME=.*/d' \
		-e '/^IP_ADDRESS=.*/d' \
		#
}
