#!/bin/sh
# -*- indent-tabs-mode: nil; -*-
#
# @(#)@@SIMPLEVERSIONTAG@@
#
# Daily script to handle actions triggered by newly existing files.
# This is e.g. convenient to notify a running daemon to reload because
# of renewed certificates.
#

# If there is a global system configuration file, suck it in.
if [ -r /etc/defaults/periodic.conf ]
then
    . /etc/defaults/periodic.conf
    source_periodic_confs
fi

# Set it to "YES" to enable this script
: ${daily_local_triggered_action_enable:=NO}
# The readability of any of the given files triggers the action
: ${daily_local_triggered_action_files=}
#
# A condition to check also before executing an action.
#
# May be a Shell pipeline.
# E.g. "service nginx onestatus || service apache2 onestatus"
#
: ${daily_local_triggered_action_condition=}
#
# The action to execute.
#
# May be a Shell pipeline.
# E.g. "{ service nginx onereload && service apache2 onereload ; } || true"
#
: ${daily_local_triggered_action_action=}
#
# By default all files triggering the action are removed. Set to "NO" if
# the files should remain.
#
: ${daily_local_triggered_action_files_remove:=YES}
#
# If profiles are defined this script is re-executed once for for every
# profile with the profile as parameter.
# No global options above besides "daily_local_triggered_action_enable" are
# used.
# Instead profile level configurations are named
# daily_local_triggered_action_<profile>_<option> .
#
: ${daily_local_triggered_action_profiles=}


#:
#: Check whether a given profile is defined in the configuration
#:
#: Args:
#:  $1: The profile to check for
#:
#: Returns:
#:   0: If the profile is found
#:   1: If the profile is not found
#:
_is_profile() {
    local prof

    for prof in ${daily_local_triggered_action_profiles}; do
	if [ "${prof}" = "$1" ]; then
	    return 0
	fi
    done
    return 1
}


rc=0

if [ -n "${daily_local_triggered_action_profiles}" ]; then
    if [ $# -eq 1 ]; then
        profile="$1"
        if ! _is_profile "${profile}"; then
            echo "ERROR: no such profile: ${profile}" 1>&2
            exit 1
        fi
        profilevar="$(echo -n "${profile}" | /usr/bin/tr -- '-:.@/$*+~=!()|' '_')"
        eval daily_local_triggered_action_files="\"\${daily_local_triggered_action_${profilevar}_files-}\""
        eval daily_local_triggered_action_condition="\"\${daily_local_triggered_action_${profilevar}_condition-}\""
        eval daily_local_triggered_action_action="\"\${daily_local_triggered_action_${profilevar}_action-}\""
        eval daily_local_triggered_action_files_remove="\${daily_local_triggered_action_${profilevar}_files_remove:-YES}"
    elif [ $# -gt 1 ]; then
        echo "ERROR: usage" 1>&2
        exit 1
    else
        for _p in ${daily_local_triggered_action_profiles} ; do
            # Re-execute with profile
            $0 "${_p}"
            _tmprc=$?
            if [ ${_tmprc} -ne 0 ]; then
                rc=${_tmprc}
            fi
        done
        exit ${rc}
    fi
fi

case "${daily_local_triggered_action_enable}" in
    [Yy][Ee][Ss])
        if [ -z "${profile}" ]; then
            profilestr=
        else
            profilestr=" (${profile})"
        fi

        echo
        echo "Testing for newly triggered action${profilestr}"

        _do_action=""
        TRIGGER_FILES=""

        for _f in ${daily_local_triggered_action_files}; do
            if [ -r "${_f}" ]; then
                _do_action="yes"
                TRIGGER_FILES="${TRIGGER_FILES} ${_f}"
            fi
        done

        if [ "${_do_action}" = "yes" ]; then
            if [ -z "${daily_local_triggered_action_action}" ]; then
                echo "ERROR: no action defined${profilestr}" 1>&2
                exit 2
            fi

            echo "Executing action because valid trigger found${profilestr}"
            if [ -n "${daily_local_triggered_action_condition}" ]; then
                eval ${daily_local_triggered_action_condition}
                _tmprc=$?
                if [ ${_tmprc} -eq 0 ]; then
                    eval ${daily_local_triggered_action_action}
                    rc=$?
                    if [ ${rc} -ne 0 ] ; then
                        echo "ERROR: Action failed${profilestr}" 1>&2
                    fi
                else
                    rc=1
                fi
            else
                eval ${daily_local_triggered_action_action}
                rc=$?
            fi

            # Remove trigger files if configured to do so
            if [ ${rc} -eq 0 ]; then
                case "${daily_local_triggered_action_files_remove}" in
                    [Yy][Ee][Ss])
                        echo "Removing trigger files${profilestr} ..."
                        for _rf in ${TRIGGER_FILES}; do
                            rm -fv "${_rf}"
                        done
                        ;;
                esac
            fi
        else
            echo "No action triggers found${profilestr}"
        fi
        ;;
    *)
        rc=0
        ;;
esac

exit ${rc}
