#!/bin/bash

confdir="/etc/apache2"
logfile="/var/log/mk-httpd-conf.log"
lockfile="/var/lock/mk-httpd-conf.lock"

source /root/etc/config
source /usr/local/lib/hostsharing/pac
source /usr/local/lib/hostsharing/dom
source /usr/local/lib/hostsharing/misc

function log() {
   message="${1}"
   echo "${message}"
   echo "`date`: ${message}" >>"${logfile}"
}

function die() {
   message="${1}"
   log "${message}"
   log "Aborted."
   dotlockfile -u "${lockfile}"
   exit 1
}

dotlockfile "${lockfile}"
log "Started (${*})."

while [ "${1:0:1}" == "-" ]; do
    shift
done

if [ ${#} -ge 1 ]; then
    pattern="${1}"
else
    pattern="*"
fi

if [ "${pattern}" == "*" ]; then
   log "Cleaning configuration."
   cd "${confdir}/sites-generated/"
   ls -c1 |xargs -r rm
   cd "${confdir}/sites-enabled/"
   ls -c1 |xargs -r rm
fi

for pac in `pacs_by_name "${pattern}"`; do

    log "Processing pac: ${pac}."
    pac_get "${pac}" || die "Cannot get pac info for ${pac}."

    if [ -e "/etc/apache2/pems/${pac}.pem" ]; then
        ssl_cert="${pac}"
    else
        ssl_cert="default"
    fi

    if [ -e "/etc/apache2/pems/${ssl_cert}.chain.pem" ]; then
        ssl_chain=""
    else
        ssl_chain="#"
    fi

    if [ -z "${PAC_IPNUMBER_EX}" ]; then
       PAC_IPNUMBER_EX="${PAC_IPNUMBER}"
    fi

    if [ -n "${PAC_ISCANCELLED}" ]; then
        pac_template="locked.pac.template"
    elif [ -n "${PAC_TEMPLATE}" ]; then
        pac_template="${PAC_TEMPLATE}.pac.template"
    else
        prefix=`echo "$PAC_TYPE" |cut -d "/" -f 1 |tr "[:upper:]" "[:lower:]"`
        pac_template="${prefix}.pac.template"
    fi    
   
    sed -e "s/{HIVE}/${THIS_HIVE}/g" \
        -e "s/{PAC}/${PAC_NAME}/g" \
        -e "s/{USER}/${PAC_OWNER}/g" \
	-e "s/{GROUP}/${PAC_NAME}/g" \
	-e "s/{IPNUMBER}/${PAC_IPNUMBER}/g" \
	-e "s/{IPNUMBEREX}/${PAC_IPNUMBER_EX}/g" \
	-e "s/{SSLCERT}/${ssl_cert}/g" \
	-e "s/{SSLCHAIN}/${ssl_chain}/g" \
	<"${confdir}/templates/${pac_template}" >"${confdir}/sites-generated/${pac}.hostsharing.net"

    level="1111111111110111"
    cd "${confdir}/sites-enabled"
    
    if [ -e "../sites-special/${pac}.hostsharing.net" ]; then
        ln -sf "../sites-special/${pac}.hostsharing.net" "${level}-${pac}.hostsharing.net"
    else
        ln -sf "../sites-generated/${pac}.hostsharing.net" "${level}-${pac}.hostsharing.net"
    fi

    for dom in `doms_by_owner "${pac}.*"`; do

        log "Processing dom: ${dom}."
        dom_get "${dom}" || die "Cannot get dom info for ${dom}."

        if [ -e "/etc/apache2/pems/${pac}.pem" ]; then
            ssl_cert="${pac}"
        else
            ssl_cert="default"
        fi

        if [ -e "/etc/apache2/pems/${ssl_cert}.chain.pem" ]; then
            ssl_chain=""
        else
            ssl_chain="#"
        fi

        if [ -z "${DOM_IPNUMBER_EX}" ]; then
            DOM_IPNUMBER_EX="${DOM_IPNUMBER}"
        fi

        if [ -n "${PAC_ISCANCELLED}" ]; then
            dom_template="locked.dom.template"
        elif [ -n "${DOM_TEMPLATE}" ]; then
            dom_template="${DOM_TEMPLATE}.dom.template"
        else
	    prefix=`echo "$PAC_TYPE" |cut -d "/" -f 1 |tr "[:upper:]" "[:lower:]"`
            dom_template="${prefix}.dom.template"
        fi

        sed -e "s/{HIVE}/${THIS_HIVE}/g" \
            -e "s/{PAC}/${PAC_NAME}/g" \
	    -e "s/{DOM}/${DOM_DOMAIN}/g" \
            -e "s/{USER}/${DOM_OWNER}/g" \
            -e "s/{GROUP}/${PAC_NAME}/g" \
            -e "s/{IPNUMBER}/${DOM_IPNUMBER}/g" \
	    -e "s/{IPNUMBEREX}/${DOM_IPNUMBER_EX}/g" \
	    -e "s/{SSLCERT}/${ssl_cert}/g" \
	    -e "s/{SSLCHAIN}/${ssl_chain}/g" \
            <"${confdir}/templates/${dom_template}" >"${confdir}/sites-generated/${dom}"

        level="$((1111111111111111-10**`getDomLevel "${dom}"`))"       
        cd "${confdir}/sites-enabled"

        if [ -e "../sites-special/${dom}" ]; then
            ln -sf "../sites-special/${dom}" "${level}-${dom}"  
        else
            ln -sf "../sites-generated/${dom}" "${level}-${dom}"
        fi

    done
done

log "Finished."
dotlockfile -u "${lockfile}"
exit 0
