#!/bin/bash

: "${AUTOSYSADMIN_LIB:=/usr/local/lib/autosysadmin}"
source "${AUTOSYSADMIN_LIB}/common.sh"  || exit 1
source "${AUTOSYSADMIN_LIB}/restart.sh" || exit 1

# shellcheck disable=SC2034
RUNNING="nwh-fr"

## Possible values for RUNNING :
##   never    => disabled
##   always   => enabled
##   nwh-fr   => enabled during non-working-hours in France
##   nwh-ca   => enabled during non-working-hours in Canada (not supported yet)
##   custom   => enabled if `running_custom()` function returns 0, otherwise disabled.

## Uncomment and customize this method if you want to have a special logic :
##
##   return 1 if we should not run
##   return 0 if we should run
##
## Some available functions :
##   is_weekend()  : Saturday or Sunday
##   is_holiday()  : holiday in France (based on `gcal(1)`)
##   is_workday()  : not weekend and not holiday
##   is_worktime() : work day between 9-12h and 14-18h
#
# running_custom() {
#     # implement your own custom method to decide if we should run or not
# }

## The name of the service, mainly for logging
service_name="example"
## The SysVinit script name
sysvinit_script="${service_name}"
## The systemd service name
systemd_service="${service_name}.service"

is_service_alive() {
    ## this must return 0 if the service is alive, otherwise return 1
    ## Example:
    pgrep -u USER PROCESS_NAME > /dev/null
}

## Action for SysVinit system
sysvinit_action() {
    # Save service status before restart
    timeout 2 "/etc/init.d/${sysvinit_script}" status | save_in_log_dir "${service_name}.before.status"

    # Try to restart
    timeout 20 "/etc/init.d/${sysvinit_script}" restart > /dev/null
    rc=$?
    if [ "${rc}" -eq "0" ]; then
        log_action "Restart ${service_name}: OK"
    else
        log_action "Restart ${service_name}: failed"
    fi

    # Save service status after restart
    timeout 2 "/etc/init.d/${sysvinit_script}" status | save_in_log_dir "${service_name}.after.status"
}

## Action for systemd system
systemd_action() {
    # Save service status before restart
    systemctl status "${systemd_service}" | save_in_log_dir "${service_name}.before.status"

    # Try to restart
    # systemctl (only for NRPE ?) sometimes returns 0 even if the service has failed to start
    # so we check the status explicitly
    timeout 20 systemctl restart "${systemd_service}" > /dev/null \
        && sleep 1 \
        && systemctl status "${systemd_service}" > /dev/null
    rc=$?
    if [ "${rc}" -eq "0" ]; then
        log_action "Restart ${service_name}: OK"
    else
        log_action "Restart ${service_name}: failed"
    fi

    # Save service status after restart
    systemctl status "${systemd_service}" | save_in_log_dir "${service_name}.after.status"
}

# Should we run?
if ! is_supposed_to_run; then
    # log_global "${PROGNAME} is not supposed to run (RUNNING=${RUNNING})."
    exit 0
fi
if is_service_alive; then
    # log_global "${service_name} process alive. Aborting"
    exit 0
fi

# Yes we do, so check for sysvinit or systemd
if is_debian_version "8" "<="; then
    if ! is_sysvinit_enabled "*${sysvinit_script}*"; then
        # log_global "${service_name} not enabled. Aborting"
        exit 0
    fi

    # Let's finally do the action
    pre_restart
    sysvinit_action
    post_restart
else
    if ! is_systemd_enabled "${systemd_service}"; then
        # log_global "${service_name} is disabled (or missing), nothing left to do."
        exit 0
    fi
    if is_systemd_active "${systemd_service}"; then
        # log_global "${service_name} is active, nothing left to do."
        exit 0
    fi

    # Let's finally do the action
    pre_restart
    systemd_action
    post_restart
fi
