#!/bin/bash

[ ! -z "$LI_CONTROLLER_LIB_APACHE" ] && return 0
LI_CONTROLLER_LIB_APACHE=true

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

function rm_httpd_proxy {
    uuid="$1"
    namespace="$2"
    application="$3"
    rm -rf /etc/httpd/conf.d/openshift/${uuid}_${namespace}_${application}.conf \
           /etc/httpd/conf.d/openshift/${uuid}_${namespace}_${application} || \
           echo "Could not remove apache definition" 1>&2
    ${CARTRIDGE_BASE_PATH}/abstract/info/bin/httpd_singular graceful 2> /dev/null || \
    echo "Failed to restart master httpd, please contact support" 1>&2
}

function restart_httpd_graceful {
    ${CARTRIDGE_BASE_PATH}/abstract/info/bin/httpd_singular graceful 2> /dev/null || \
    error "Failed to restart master httpd, please contact support" 120
}


function pid_is_httpd() {
    ps -p "$1" 2> /dev/null | grep httpd > /dev/null
}

function killall_matching_httpds() {
   [ -z "$1" ]  &&  return 1
   ps -u `id -u` -o pid,command | grep "$1" | grep -v "grep" |    \
       awk '{ print $1 }' |  xargs kill -9  > /dev/null 2>&1  || :
}

function ensure_valid_httpd_process() {
    # $1 == pidfile.
    # $2 == httpd config file.
    [ -n "$1" ]  &&  [ -f "$1" ]  &&  pid_is_httpd `cat "$1"`  &&  return 0
    [ -n "$1" ]  &&  rm -f "$1"
    killall_matching_httpds "$2"
}
