#!/bin/sh
#
# bundle-ovirt-engine-health
# Copyright (C) 2017 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

script="$(readlink -f "$0")"
ENGINE_SETUP_BIN_DIR="$(dirname "${script}")"

[ -z "${ENGINESETUPDIR}" ] && ENGINESETUPDIR="$(dirname "${ENGINE_SETUP_BIN_DIR}")"

[ -z "${ENGINEDATADIR}" ] && ENGINEDATADIR="$(dirname "${ENGINESETUPDIR}")"

. "${ENGINEDATADIR}/bin/engine-prolog.sh"

[ -z "${SHELL}" ] && SHELL=/bin/sh

[ -z "${OTOPI_BUNDLE}" ] && OTOPI_BUNDLE=/usr/share/otopi/otopi-bundle

[ -z "${TARGET}" ] && TARGET=$(mktemp -d)

[ -z "${DESTDIR}" ] && DESTDIR=/

[ -z "${OUTPUT}" ] && OUTPUT=ovirt-engine-health-check.run

PYTHON_LIB_DIR="$(python -c "from distutils.sysconfig import get_python_lib as f;print(f())")"
PYTHON_PLAT_LIB_DIR="$(python -c "from distutils.sysconfig import get_python_lib as f;print(f(True))")"
[ -z "${ENGINE_PYTHON_LIB}" ] && ENGINE_PYTHON_LIB="${PYTHON_LIB_DIR}/ovirt_engine"
[ -z "${OVIRT_SETUP_LIB}" ] && OVIRT_SETUP_LIB="${PYTHON_LIB_DIR}/ovirt_setup_lib"

[ -z "${PSYCOPG2_LIB}" ] && PSYCOPG2_LIB="${PYTHON_PLAT_LIB_DIR}/psycopg2"

"${SHELL}" "${OTOPI_BUNDLE}" \
	--gettext-domain="${PACKAGE_NAME}" \
	--target="${TARGET}" \
	--root="${DESTDIR}" \
	--otopi-sources="${OTOPI_SOURCES}"

# Main script
rm -f "${TARGET}"/ovirt-engine-health
ln -s "${ENGINE_SETUP_BIN_DIR}"/ovirt-engine-health "${TARGET}"/ovirt-engine-health

# Env
rm -f "${TARGET}"/ovirt-engine-setup.env
ln -s "${ENGINE_SETUP_BIN_DIR}"/ovirt-engine-setup.env "${TARGET}"/ovirt-engine-setup.env

# "Our" Libraries
rm -f "${TARGET}"/pythonlib/ovirt_engine
ln -s "${ENGINE_PYTHON_LIB}" "${TARGET}"/pythonlib/ovirt_engine
rm -f "${TARGET}"/pythonlib/ovirt_engine_setup
ln -s "${ENGINESETUPDIR}"/ovirt_engine_setup "${TARGET}"/pythonlib/ovirt_engine_setup
rm -f "${TARGET}"/pythonlib/ovirt_setup_lib
ln -s "${OVIRT_SETUP_LIB}" "${TARGET}"/pythonlib/ovirt_setup_lib

# Additional Libraries
#rm -f "${TARGET}"/pythonlib/psycopg2
#ln -s "${PSYCOPG2_LIB}" "${TARGET}"/pythonlib/psycopg2

# Plugins
# otopi's buildin plugins are already installed by otopi-bundle
rm -f "${TARGET}"/otopi-plugins/ovirt-engine-common
ln -s "${ENGINESETUPDIR}"/plugins/ovirt-engine-common "${TARGET}"/otopi-plugins/ovirt-engine-common
rm -f "${TARGET}"/otopi-plugins/ovirt-engine-checks
ln -s "${ENGINESETUPDIR}"/plugins/ovirt-engine-checks "${TARGET}"/otopi-plugins/ovirt-engine-checks
rm -f "${TARGET}"/otopi-plugins/ovirt-engine-health
ln -s "${ENGINESETUPDIR}"/plugins/ovirt-engine-health "${TARGET}"/otopi-plugins/ovirt-engine-health

makeself --quiet --follow "${TARGET}" "${OUTPUT}" "ovirt-engine health check" './ovirt-engine-health'

[ -z "${KEEP_TARGET_DIR}" ] && rm -rf "${TARGET}"
