#!/bin/sh
#
# ovirt-hosted-engine-setup -- ovirt hosted engine setup
# Copyright (C) 2013 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#

die() {
	local m="$1"
	echo "FATAL: ${m}" >&2
	exit 1
}

usage() {
	cat << __EOF__
Usage: $0
	--config-append=file
		Load extra configuration files.
	--generate-answer=file
		Generate answer file.
	--upgrade-appliance
		Upgrade the engine-appliance

__EOF__
	exit 1
}

environment="OVESETUP_CORE/offlinePackager=bool:True"
environment="${environment} PACKAGER/yumpackagerEnabled=bool:False"

plugingroups="gr-he-common:gr-he-setup"

while [ -n "$1" ]; do
	x="$1"
	v="${x#*=}"
	shift
	case "${x}" in
		--otopi-environment=*)
			otopienv="${v}"
		;;
		--config-append=*)
			environment="${environment} APPEND:CORE/configFileAppend=str:${v}"
		;;
		--generate-answer=*)
			environment="${environment} OVEHOSTED_CORE/userAnswerFile=str:${v}"
		;;
		--help)
			usage
		;;
		--upgrade-appliance)
 			plugingroups="gr-he-common:gr-he-upgradeappliance"
		;;
		*)
			die "Invalid option '${x}'"
		;;
	esac
done

script="$(readlink -f "$0")"
scriptdir="$(dirname "${script}")"
if [ ! -f "${scriptdir}/ovirt-hosted-engine-setup.env" ]; then
	#FIXME: must be configured by configure.ac
	scriptdir=/usr/share/ovirt-hosted-engine-setup/scripts
fi
. "${scriptdir}/ovirt-hosted-engine-setup.env"
baseenv="APPEND:BASE/pluginPath=str:${scriptdir}/../plugins APPEND:BASE/pluginGroups=str:${plugingroups}"
exec "${otopidir}/otopi" "${baseenv} ${environment} ${otopienv}"
