#!/bin/sh
#This script takes care of starting nginx daemons

#NGINX_HELPER_PID="/usr/local/nginx/logs/nginx-helper.pid"
NGINX_HELPER_PID="/var/run/nginx-helper.pid"
NGINX_HELPER=/usr/sbin/nginx-helper

if ! [ -x ${NGINX_HELPER} ]; then
  echo "${NGINX_HELPER} does not exist!"
  exit 0
fi

case "$1" in
start)
#  mkfifo /tmp/nginx_error.log -m 0644
  ${NGINX_HELPER} &
  echo $! > ${NGINX_HELPER_PID}
  ;;
stop)
#  rm -rf /tmp/nginx_error.log
  if [ -e ${NGINX_HELPER_PID} ]; then
    kill -9 "$(cat ${NGINX_HELPER_PID})" 2>/dev/null
    killall nginx 2>/dev/null
    sleep 1
    rm ${NGINX_HELPER_PID}
  fi
  ;;
restart)
  $0 stop
  $0 start
  ;;
esac
