#!/bin/bash

cd /omd/sites/monitor
. .profile
. lib/omd/init_profile
. etc/omd/site.conf
if [ "$CONFIG_APACHE_MODE" == none ]; then
  exit 5
fi

. "$OMD_ROOT/.profile"

# Start httpd in the C locale by default.
HTTPD_LANG="C"

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

APACHE_RUN_USER="monitor"
APACHE_RUN_GROUP="monitor"

APACHE_BIN="/usr/sbin/apache2"
PID_FILE=/omd/sites/monitor/tmp/apache/run/apache.pid
CONFIG_FILE=/omd/sites/monitor/etc/apache/apache.conf

pidof_apache() {
  # if there is actually an apache2 process whose pid is in PIDFILE,
  # print it and return 0.
  if [ -e "$PID_FILE" ]; then
    PID=$(cat "$PID_FILE")
    if kill -0 $PID >/dev/null 2>&1; then
      echo $PID
      return 0
    fi
  else
    # It might happen that there is no pidfile but a process is running
    # As fallback check the process table for the oldest apache process
    # running as this user
    PID=$(pgrep -u $OMD_SITE -o -f $APACHE_BIN)
    if [ -n "$PID" ]; then
        echo $PID
        return 0
    fi
  fi
  return 1
}

apache_wait_stop() {
  # running ?
  pid=$(pidof_apache) || true
  if [ -n "${pid:-}" ]; then
    kill ${pid:-}
  else
    echo -n '(not running)...'
    return 0
  fi

  # wait until really stopped
  if [ -n "${pid:-}" ]; then
    i=0
    while kill -0 "${pid:-}" 2> /dev/null;  do
      if [ $i = '120' ]; then
        kill_stale_php_cgis
        return 1
      else
        echo -n "."
        i=$(($i+1))
        sleep 0.1
      fi
     done
  fi
  
  [ -f "$PID_FILE" ] && rm -f "$PID_FILE"
  kill_stale_php_cgis
  return 0
}

apache_wait_start() {
  if pidof_apache >/dev/null 2>&1; then
    echo -n '(already running)...'
    return 0
  fi

  mkdir -p /omd/sites/monitor/tmp/apache/run

  # (nearly) reproducible problems with apache at boot-time. (alloc_listener: failed to set up sockaddr for 127.0.0.1)
  # With this ping the problem disappears.
  test -f /etc/debian_version && test $(cut -f1 -d" " /proc/uptime | cut -f1 -d".") -lt 300 && ping -nqc 10 $CONFIG_APACHE_TCP_ADDR >/dev/null 2>&1
  $APACHE_BIN -f "$CONFIG_FILE"
  
  i=0
  while ! pidof_apache >/dev/null 2>&1;  do
    if [ $i = '10' ]; then
      return 1
    else
      echo -n "."
      i=$(($i+1))
      sleep 0.1
    fi
  done

  return 0
}

kill_stale_php_cgis() 
{
    i=1
    killall -e /usr/bin/php-cgi -u monitor >/dev/null 2>&1 
    while killall -e /usr/bin/php-cgi -u monitor >/dev/null 2>&1
    do
        i=$((i+1))
        if [ $i -gt 50 ] ; then 
            return
        fi
        sleep 0.1
    done
}

__init_hook $0 $1 pre
case $1 in
  start)
    echo -n "Starting dedicated Apache for site monitor..."
    if apache_wait_start; then
      __init_hook $0 $1 post 0
      echo 'OK'
      exit 0
    else
      __init_hook $0 $1 post 1
      echo 'ERROR'
      exit 1
    fi
  ;;
  stop)
    echo -n "Stopping dedicated Apache for site monitor..."
    if apache_wait_stop; then
      __init_hook $0 $1 post 0
      echo 'OK'
      exit 0
    else
      __init_hook $0 $1 post 1
      echo 'ERROR'
      exit 1
    fi
  ;;
  restart)
    $0 stop
    $0 start
  ;;
  reload)
    echo "Reloading dedicated Apache for site monitor"
    $APACHE_BIN -f "$CONFIG_FILE" -k graceful
    __init_hook $0 $1 post $?
  ;;
  status)
    PID=$(pidof_apache) || true
    if [ -n "$PID" ]; then
      echo "Apache is running (pid $PID)."
      exit 0
    else
      echo "Apache is NOT running."
      exit 1
    fi
  ;;
  check)
  ;;
  *)
    echo "Usage: $0 {start|stop|restart|reload|status}"
  ;;
esac
