#!/bin/sh

# shellcheck source=/dev/null
. /lib/functions/keepalived/hotplug.sh
. /lib/functions/keepalived/ns.sh

set_service_name ns-plug

set_restart_if_master
set_stop_if_backup

RESTORE_LIST="/usr/share/keepalived/rsync/tmp/restore_list"

if [ "$ACTION" == "NOTIFY_BACKUP" ]; then
    update_cron "disable" "send-heartbeat send-backup send-inventory" "ns-push-reports"
elif [ "$ACTION" == "NOTIFY_MASTER" ]; then
    update_cron "enable" "send-heartbeat send-backup send-inventory" "ns-push-reports"
elif [ "$ACTION" == "NOTIFY_SYNC" ]; then
    # Remove backup.pass if it has been unset on the primary
    if ! grep -q /etc/backup.pass /usr/share/keepalived/rsync/tmp/restore_list && [ -f /etc/backup.pass ]; then
        rm -f /etc/backup.pass
    fi
fi

keepalived_hotplug
