#!/bin/bash

# Start the application httpd instance

# Exit on any errors
set -e

function print_help {
    echo "Usage: $0 app-name namespace uuid server_alias"
    echo "Start a running application"

    echo "$0 $@" | logger -p local0.notice -t openshift_origin_httpd_start
    exit 1
}

while getopts 'd' OPTION
do
    case $OPTION in
        d) set -x
        ;;
        ?) print_help
        ;;
    esac
done


[ $# -eq 4 ] || print_help

source "/etc/openshift/node.conf"
source ${CARTRIDGE_BASE_PATH}/abstract/info/lib/util
source ${CARTRIDGE_BASE_PATH}/abstract/info/lib/apache

setup_basic_hook "$1" $2 $3
server_alias_name="$4"

if grep -q "ServerAlias ${server_alias_name}" /etc/httpd/conf.d/openshift/*/server_alias-*.conf
then
    echo "CLIENT_ERROR: FATAL ERROR: ${server_alias_name} Already exists."
    exit 2
fi

cat <<EOF > "/etc/httpd/conf.d/openshift/${uuid}_${namespace}_${application}/server_alias-${server_alias_name}.conf"
ServerAlias ${server_alias_name}
EOF

restart_httpd_graceful
