#!/bin/sh
#
# PROVIDE: nghttpx
# REQUIRE: NETWORKING SERVERS
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable nghttpx_multi:
#
# nghttpx_multi_enable (bool):        Set to "NO" by default.
#                                     Set it to "YES" to enable nghttpx_multi.
# nghttpx_multi_profiles (str):       Set to "" by default.
#                                     Define profile names (e.g. "profile01")
# nghttpx_multi_<name>_enable (bool): Set to ${nghttpx_multi_enable} by default.
#                                     Set it to "YES" or "NO" to independently enable or disable profile <name>.
#

. /etc/rc.subr

name="nghttpx_multi"
rcvar=nghttpx_multi_enable

command="/usr/local/bin/nghttpx"

load_rc_config $name

: ${nghttpx_multi_enable:="NO"}

if [ -n "$2" ]
then
  profile="$2"

  if [ "x${nghttpx_multi_profiles}" != "x" ]
  then
    pidfile="/var/run/nghttpx.${profile}.pid"
    required_files="/usr/local/etc/nghttpx/nghttpx.${profile}.conf"

    command_args="--pid-file=$pidfile --conf=${required_files}"
    eval nghttpx_multi_enable="\${nghttpx_multi_${profile}_enable:=${nghttpx_multi_enable}}"
  else
    echo "$0: extra argument ignored." >&2
  fi
else
  if [ "x${nghttpx_multi_profiles}" != "x" -a "x$1" != "x" ]
  then
    for profile in ${nghttpx_multi_profiles}
    do
      eval nghttpx_multi_enable_tmp="\${nghttpx_multi_${profile}_enable:=${nghttpx_multi_enable}}"

      case "x${nghttpx_multi_enable_tmp}"
      in
        x|x[Nn][Oo])
          continue
          ;;
        x[Yy][Ee][Ss])
          ;;
        *)
          echo "Bad value \"${nghttpx_multi_enable_tmp}\" for nghttpx_multi_${profile}_enable. Profile ${profile} skipped." >&2
          continue
          ;;
      esac

      echo "===> nghttpx_multi profile: ${profile}"
      '/usr/local/etc/rc.d/nghttpx_multi' "$1" "${profile}"
    done

    exit 0
  fi
fi

run_rc_command "$1"
