#!/bin/sh
#
# $Revision: 1.3 $ $Date: 2026-09-01 04:42:07-04 $
# $Source: /usr/local/src/m/monit/admin/RCS/monit.server,v $
# $Host: furbag.home.arpa $
# $UUID: 256616b9-ad42-4009-8d54-90fadb065dff $
#
# NAME:
#   monit.server
#
# SYNOPSIS:
#   monit.server COMMAND
#
# DESCRIPTION:
#   Script file to start and stop Monit services.
#
#   COMMAND is one of the following:
#      start   -- starts Monit service
#      stop    -- stops Monit service
#      check   -- run syntax check for Monit control file
#      help    -- this message
#      id      -- print Monit unique ID
#      reload  -- reinitialise running Monit daemon
#      restart -- stops and restarts Monit
#      status  -- displays status of Monit service
#      summary -- displays service states
#
# INSTALLATION:
#   Put this in at least two places: where the system looks for
#   startup/shutdown files, and someplace where root can run it.
#
# AUTHOR:
#   Karl Vogel <vogelke+unix@pobox.com>
#   Based on "Life with qmail" script and
#   https://sosheskaz.github.io/tutorial/2017/03/28/FreeBSD-rcd-Setup.html

# How to use the rc subrouting.
. /etc/rc.subr

# Metadata holding basic information about this service.
name="monit"            # How the service will be invoked from service
rcvar="${name}_enable"  # set this in rc.conf to allow service to run
load_rc_config $name    # Loads the config file, if relevant.

# Exports, etc go here.
export PATH=/bin:/usr/bin:/usr/sbin:/usr/local/sbin:/usr/local/bin
export TERM=dumb
pidfile="/var/run/$name.pid"

# Command-line args
case "$1" in
  start)
    $name
    ;;

  stop)     # short sleep, or it may try to awaken a dead process
    $name quit
    sleep 1
    ;;

  check)    # syntax check for Monit control file
    $name -tv
    ;;

  id)       # show Monit unique ID
    $name -i
    ;;

  reload)   # reinitialise running Monit daemon
    $name reload
    ;;

  restart)
    $0 stop
    $0 start
    ;;

  status)
    $name status
    ;;

  summary)  # BUG: display messed up even with TERM=dumb
    $name summary
    $name report
    ;;

  version)
    $name -V
    ;;

  *) cat <<HELP
    start   -- starts Monit service
    stop    -- stops Monit service
    check   -- run syntax check for Monit control file
    help    -- this message
    id      -- print Monit unique ID
    reload  -- reinitialise running Monit daemon
    restart -- stops and restarts Monit
    status  -- displays status of Monit service
    summary -- displays service states
    version -- displays version and patch
HELP
    ;;
esac

exit 0
