#!/bin/bash

# emhttp_event script (a work in process).

# This script is called by the emhttp process as a result of various events that take place.
# Caution: the 'emhttp' process will hang until this script completes!

# The first argument to the script is a string indicating the event:

# driver_loaded
#   Occurs early in emhttp initialization.
#   Can also occur as a result of init-config and device slot change.
#   Status information is valid.

# array_started
#   Occurs during cmdStart execution.
#   The 'md' devices are valid.

# disks_mounted
#   Occurs during cmdStart execution.
#   The disks and user shares (if enabled) are mounted.

# started
#   Signals end of cmdStart execution.

# svcs_restarted
#   Occurs as a result of changing/adding/deleting a share.
#   The network services are started and may be exporting different share(s).

# stopping_svcs
#   Occurs during cmdStop execution.
#   Nothing has actually been stopped yet, about to stop network services.

# unmounting_disks
#   Occurs during cmdStop execution.
#   The network services have been stopped, about to unmount the disks and user shares.
#   The disks have been spun up and a "sync" executed, but no disks un-mounted yet.

# stopping_array
#   Occurs during cmdStop execution.
#   The disks and user shares have been unmounted, about to stop the array.

# stopped
#   Occurs at end of cmdStop execution.
#   The array has been stopped.

# Log the event (more: add way to enable/disable event logging?)
logger -t emhttp_event $1

# Invoke all 'any_event' scripts that might exist
for Dir in /usr/local/emhttp/plugins/*
do
  if [ -x $Dir/event/any_event ]; then
    $Dir/event/any_event $1
  fi
done

# Invoke specific event scripts that might exist for this event
for Dir in /usr/local/emhttp/plugins/*
do
  if [ -x $Dir/event/$1 ]; then
    $Dir/event/$1 $1
  fi
done
