#!/bin/bash

prefix="@prefix@"
exec_prefix="@exec_prefix@"

# first check for critical backups and execute the first one
for b in "@sysconfdir@"/cron.{hourly,daily,monthly,weekly}/backup-*; do
    if [ -e "$b" ]; then
        p=${b#"@sysconfdir@"/cron.}; p=${p%/*};
        h=${b#"@sysconfdir@"/cron.*/backup-}; h=${h//_/.};
        "@libdir@/nagios/plugins/check_backup" $h $p \
            | grep -q "backup is running"
        if [ ${PIPESTATUS[1]} -eq 2 -a ${PIPESTATUS[0]} -ne 0 ]; then
            # backup is faulty and not running
            # immediate backup required start it now
            $b
            exit
        fi
    fi
done

# first check for warning backups and execute the first one
for b in "@sysconfdir@"/cron.{hourly,daily,monthly,weekly}/backup-*; do
    if [ -e "$b" ]; then
        p=${b#"@sysconfdir@"/cron.}; p=${p%/*};
        h=${b#"@sysconfdir@"/cron.*/backup-}; h=${h//_/.};
        "@libdir@/nagios/plugins/check_backup" $h $p \
            | grep -q "backup is running"
        if [ ${PIPESTATUS[1]} -eq 1 -a ${PIPESTATUS[0]} -ne 0 ]; then
            # backup is faulty and not running
            # immediate backup required start it now
            $b
            exit
        fi
    fi
done
