#!/bin/sh

# PROVIDE: netdisco
# REQUIRE: DAEMON LOGIN
# AFTER: postgresql
# KEYWORD: shutdown

. /etc/rc.subr

name="netdisco"
rcvar="netdisco_enable"
start_precmd="netdisco_start_precmd"
start_cmd="netdisco_cmd"
stop_cmd="netdisco_cmd"
status_cmd="netdisco_cmd"
reload_cmd="netdisco_reload_cmd"
rcvar_cmd="netdisco_rcvar_cmd"
extra_commands="reload status"

: ${netdisco_home="/var/run/netdisco"}
: ${netdisco_user="netdisco"}
: ${netdisco_group="netdisco"}
netdisco_daemons="web backend"

netdisco_start_precmd() {
  # /var/run and /var/log
  if [ ! -d /var/run/netdisco ]; then
    mkdir /var/run/netdisco
    chown -R netdisco:netdisco /var/run/netdisco
  fi
  if [ ! -d /var/log/netdisco ]; then
    mkdir /var/log/netdisco
    chown netdisco:netdisco /var/log/netdisco
  fi
  if [ ! -d /usr/local/etc/netdisco/logs ]; then
    ln -s /var/log/netdisco /usr/local/etc/netdisco/logs
  fi
  # populate /var/run/netdisco
  for dir in environments perl5 netdisco-mibs; do
    test -d /var/run/netdisco/$dir || ln -fs /usr/local/etc/netdisco/$dir /var/run/netdisco/$dir
  done
  test -d /var/run/netdisco/logs || ln -s /var/log/netdisco /var/run/netdisco/logs
}

netdisco_cmd() {
  local name rcvar rcvars v command pidfile netdisco_daemons result force_run interpreter
  # Prevent recursive calling
  unset "${rc_arg}_cmd" "${rc_arg}_precmd" "${rc_arg}_postcmd"
  # Ignore rcvar and run command
  if [ -n "${_rc_prefix}" -a "${_rc_prefix}" = "one" ] || [ -n "${rc_force}" ] || [ -n "${rc_fast}" ]; then
      force_run=yes
  fi
  # Assume success
  result=0
  # Apply to all daemons
  for n in ${netdisco_daemons}; do
    name="netdisco_$n"
    rcvars=''; v=''
    name=${name}
    rcvar=${name}_enable
    command="/usr/local/bin/netdisco-${n}"
    command_args=${rc_arg}
    pidfile="/var/run/netdisco/netdisco-${n}.pid"
    [ "$n" = "web" ] && command_interpreter="perl" || command_interpreter=""
    # Daemon should be enabled and running
    if ( [ -n "${rcvar}" ] && checkyesno "${rcvar}" ) || [ -n "$force_run" ]; then
        run_rc_command "${_rc_prefix}${rc_arg}" ${rc_extra_args}
        # If any of the commands failed, take it as a global result
        result=$((${result} || $?))
    fi
  done
  return ${result}
}

netdisco_rcvar_cmd() {
  local name rcvar
  rcvar=${name}_enable
  # Prevent recursive calling
  unset "${rc_arg}_cmd" "${rc_arg}_precmd" "${rc_arg}_postcmd"
  # Check master variable
  run_rc_command "${_rc_prefix}${rc_arg}" ${rc_extra_args}
  # Check dependent variables
  for n in ${netdisco_daemons}; do
      name=netdisco_${n}
      # XXX
      rcvars=''; v=''
      rcvar=${name}_enable
      run_rc_command "${_rc_prefix}${rc_arg}" ${rc_extra_args}
  done
}

netdisco_config_init() {
  local name
  load_rc_config "${name}"
  for n in ${netdisco_daemons}; do
    load_rc_config "netdisco_${n}"
  done
  # Defaults
  netdisco_enable=${netdisco_enable:=NO}
  if [ -n "${rcvar}" ] && checkyesno "${rcvar}"; then
    netdisco_web_enable=${netdisco_enable}
    netdisco_backend_enable=${netdisco_enable}
  fi
  netdisco_web_enable=${netdisco_web_enable:-NO}
  netdisco_web_pidfile=${netdisco_web_pidfile:-"/var/run/netdisco/netdisco-web.pid"}
  netdisco_backend_enable=${netdisco_backend_enable:-NO}
  netdisco_web_user=${netdisco_user}
  netdisco_backend_user=${netdisco_user}
  netdisco_web_group=${netdisco_group}
  netdisco_backend_group=${netdisco_group}
  export NETDISCO_HOME=/var/run/netdisco
}

# load config
netdisco_config_init

run_rc_command "$1"
