#!/bin/bash
#-----------------
# Testing heat-api
#-----------------
set -e

DAEMONS=(heat-api heat-api-cfn)

for daemon in "${DAEMONS[@]}"; do
    apt-get install -y --no-install-recommends "$daemon" >/dev/null 2>&1

    if apache2ctl -t -D DUMP_VHOSTS 2>/dev/null | grep -q "${daemon}.conf"; then
        echo "OK: ${daemon} vhost enabled"
    else
        echo "ERROR: ${daemon} vhost not enabled"
        apache2ctl -S || true
        ls -l /etc/apache2/sites-available/ /etc/apache2/sites-enabled/ || true
        exit 1
    fi

    wsgi_script=$(awk '$1 == "WSGIScriptAlias" { print $3 }' \
        "/etc/apache2/sites-enabled/${daemon}.conf")
    if [ -x "${wsgi_script}" ]; then
        echo "OK: ${daemon} WSGI script exists"
    else
        echo "ERROR: ${daemon} WSGI script ${wsgi_script} not installed"
        ls -l /usr/bin/heat* || true
        exit 1
    fi

    apt-get remove -y "$daemon" >/dev/null 2>&1
done
