#!/bin/bash

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

DEV_MODE=${DEV_MODE:-"true"}
SCRIPTDIR=$(dirname $0)
GUEST_USERNAME=${GUEST_USERNAME:-"ubuntu"}
GUEST_VENV=/opt/guest-agent-venv

for folder in "/var/lib/trove" "/etc/trove" "/etc/trove/certs" "/etc/trove/conf.d" "/var/log/trove" "/opt/trove-guestagent"; do
    mkdir -p ${folder}
    chown -R ${GUEST_USERNAME}:root ${folder}
done

# The logrotate file owner should be root or user with uid 0.
install -D -g root -o root -m 0644 ${SCRIPTDIR}/guest-agent.logrotate /etc/logrotate.d/guest-agent

# Create a virtual environment (with dependencies installed) for guest agent service
/usr/bin/python3 -m venv ${GUEST_VENV}
${GUEST_VENV}/bin/pip install pip --upgrade
${GUEST_VENV}/bin/pip install -U -c /opt/upper-constraints.txt /opt/guest-agent
chown -R ${GUEST_USERNAME}:root ${GUEST_VENV}

# this should only happen in zuul system.
if [[ ${SYNC_LOG_TO_CONTROLLER} == "True" ]]; then
    sed "s/GUEST_USERNAME/${GUEST_USERNAME}/g;s/HOST_SCP_USERNAME/${HOST_SCP_USERNAME}/g;" ${SCRIPTDIR}/guest-log-collection.service > /etc/systemd/system/guest-log-collection.service
    cp ${SCRIPTDIR}/guest-log-collection.timer /etc/systemd/system/guest-log-collection.timer
fi

# Install trove-image-loader script and service
install -D -g root -o root -m 0755 ${SCRIPTDIR}/50-trove-image-loader /usr/local/sbin/trove-image-loader
install -D -g root -o root -m 0644 ${SCRIPTDIR}/trove-image-loader.service /etc/systemd/system/trove-image-loader.service

# Install docker network plugin
ln -s ${GUEST_VENV}/bin/trove-docker-plugin /usr/local/bin/trove-docker-plugin || true
install -D -g root -o root -m 0644 ${SCRIPTDIR}/docker-hostnic.socket /lib/systemd/system/docker-hostnic.socket

if [[ ${DEV_MODE} == "true" ]]; then
    [[ -n "${HOST_SCP_USERNAME}" ]] || die "HOST_SCP_USERNAME needs to be set to the trovestack host user"
    [[ -n "${ESCAPED_PATH_TROVE}" ]] || die "ESCAPED_PATH_TROVE needs to be set to the path to the trove directory on the trovestack host"
    sed "s/GUEST_USERNAME/${GUEST_USERNAME}/g" ${SCRIPTDIR}/docker-hostnic-dev.service > /lib/systemd/system/docker-hostnic.service
    sed "s/GUEST_USERNAME/${GUEST_USERNAME}/g;s/HOST_SCP_USERNAME/${HOST_SCP_USERNAME}/g;s/PATH_TROVE/${ESCAPED_PATH_TROVE}/g" ${SCRIPTDIR}/guest-agent-dev.service > /etc/systemd/system/guest-agent.service
else
    # Link the trove-guestagent out to /usr/local/bin where the startup scripts look for
    ln -s ${GUEST_VENV}/bin/trove-guestagent /usr/local/bin/guest-agent || true
    install -D -g root -o root -m 0644 ${SCRIPTDIR}/docker-hostnic.service /lib/systemd/system/docker-hostnic.service

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