#!/bin/bash
set -eux

install-packages libapache2-mod-wsgi libssl-dev libffi-dev

TEMPLATE_ROOT=$(os-apply-config --print-templates)

if [ -d /etc/httpd ]; then
    # This is required to allow different ports configuration
    # e.g haproxy or apache listening on 80
    sed -i '/Listen/s/^/#/g' /etc/httpd/conf/httpd.conf
    mkdir -p $TEMPLATE_ROOT/etc/httpd/conf.d/
    install -m 0644 -o root -g root $(dirname $0)/../files/* $TEMPLATE_ROOT/etc/httpd/conf.d/
    sed -e "s|HORIZON_VENV_DIR|$HORIZON_VENV_DIR|g" -i $TEMPLATE_ROOT/etc/httpd/conf.d/horizon.conf
fi

if [ -d /etc/apache2 ]; then
    mkdir -p $TEMPLATE_ROOT/etc/apache2/sites-available
    install -m 0644 -o root -g root $(dirname $0)/../files/horizon.conf $TEMPLATE_ROOT/etc/apache2/sites-available
    install -m 0644 -o root -g root $(dirname $0)/../files/ports.conf $TEMPLATE_ROOT/etc/apache2/ports.conf
    ln -s /etc/apache2/sites-available/horizon.conf /etc/apache2/sites-enabled/horizon.conf
    ln -s /var/log/apache2 /var/log/httpd
    rm -f /etc/apache2/sites-enabled/{000-default.conf,default.conf,default-ssl.conf}
    sed -e "s|HORIZON_VENV_DIR|$HORIZON_VENV_DIR|g" -i $TEMPLATE_ROOT/etc/apache2/sites-available/horizon.conf
fi

os-svc-install $HORIZON_EXTRA_INSTALL_OPTS -i "$HORIZON_VENV_DIR" -u horizon -r /opt/stack/horizon

# Django won't start otherwise:
touch /etc/horizon/.secret_key_store
chmod 600 /etc/horizon/.secret_key_store
chown horizon:horizon /etc/horizon/.secret_key_store
