#!/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 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"
}
