#!/bin/bash
#
# chkconfig:    345 31 69
# description:  nginx unit appserver
#
### BEGIN INIT INFO
# Provides:          unit httpd
# Required-Start:    $syslog $network $remote_fs
# Required-Stop:     $syslog $network $remote_fs
# Default-Start:     3 5
# Default-Stop:      0 1 2 4 6
# Short-Description: nginx unit appserver
# Description:       NGINX Unit is a lightweight, dynamic, open-source server
#                    for diverse web applications
### END INIT INFO

UNIT_BIN=/usr/sbin/unitd
UNIT_PID=/var/run/unit.pid

if [ -r /etc/rc.status ]
then
  source /etc/rc.status
  START="/sbin/startproc"
  STATUS="/sbin/checkproc"
  SUCCESS="echo \$rc_done"
  FAILURE="echo \$rc_failed"
else
  source /etc/rc.d/init.d/functions
  START="daemon"
  STATUS="status"
  SUCCESS="success; echo"
  FAILURE="failure; echo"
fi

RETVAL=0

case "$1" in
  start)
    echo -n "Starting unit: "
    ${START} ${UNIT_BIN} > /var/log/unit/startup.out 2>&1
    RETVAL=$?
    ;;
  stop)
    echo -n "Stopping unit: "
    killproc ${UNIT_BIN} > /dev/null 2>&1
    RETVAL=$?
    ;;
  restart)
    $0 stop
    $0 start
    unset RETVAL
    ;;
  status)
    echo -n "Status of unit: "
    ${STATUS} ${UNIT_BIN} > /dev/null 2>&1
    RETVAL=$?
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart}"
    exit 1
    ;;
esac
[ -z ${RETVAL} ] && exit
[ ${RETVAL} -eq 0 ] && eval "${SUCCESS}" || eval "${FAILURE}"
exit ${RETVAL}
