#!/bin/bash
set -eux

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

if [ -d /etc/httpd ]; then
    install -m 0644 -o root -g root $(dirname $0)/../files/horizon.conf /etc/httpd/conf.d/horizon.conf
    sed -e "s|HORIZON_VENV_DIR|$HORIZON_VENV_DIR|g" -i /etc/httpd/conf.d/horizon.conf
fi

if [ -d /etc/apache2 ]; then
    install -m 0644 -o root -g root $(dirname $0)/../files/horizon.conf /etc/apache2/sites-available
    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 /etc/apache2/sites-available/horizon.conf
fi

os-svc-install -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
