#!/bin/bash

if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then
    set -x
fi
set -eu
set -o pipefail

SCRIPTDIR=$(dirname $0)

# pip 10 fails when installing the amphora-agent due to a package conflict
# in oslo.config PyYAML and the python3-yaml installed in the base images.
# We cannot backport the venv fix from master as the stable branch uses
# the ubuntu element which does not have enough disk space to install with the
# venv.
# So to make a minimal fix for the stable branches we are going to pin pip
# in the amphora image.
pip install -U --force-reinstall -c ${SCRIPTDIR}/cap-pip.txt pip

pip install -U -c /opt/upper-constraints.txt /opt/amphora-agent

# Accommodate centos default install location
ln -s /bin/amphora-agent /usr/local/bin/amphora-agent || true

mkdir /etc/octavia
# we assume certs, etc will come in through the config drive
mkdir /etc/octavia/certs
mkdir -p /var/lib/octavia

install -D -g root -o root -m 0644 ${SCRIPTDIR}/amphora-agent.logrotate /etc/logrotate.d/amphora-agent

case "$DIB_INIT_SYSTEM" in
    upstart)
        install -D -g root -o root -m 0644 ${SCRIPTDIR}/amphora-agent.conf /etc/init/amphora-agent.conf
        ;;
    systemd)
        install -D -g root -o root -m 0644 ${SCRIPTDIR}/amphora-agent.service /usr/lib/systemd/system/amphora-agent.service
        ;;
    sysv)
        install -D -g root -o root -m 0644 ${SCRIPTDIR}/amphora-agent.init /etc/init.d/amphora-agent.init
        ;;
    *)
        echo "Unsupported init system"
        exit 1
        ;;
esac
