#!/bin/sh

# Files and Directories
GUEST_DAEMON_CRON_D="/etc/daemon.d/cron.d"
HOST_DAEMON_CRON_D="${MEDIACASTER_DIR}${GUEST_DAEMON_CRON_D}"

# Settings and state
CRON_TICK_PERIOD="60"
CRON_RAN_UPTIME="-${CRON_TICK_PERIOD}"

daemon_cron_tick () {
    local CURRENT_UPTIME=`daemon_utils_uptime`
	local DELTA_UPTIME=$((CURRENT_UPTIME - CRON_RAN_UPTIME))

    if [ ${DELTA_UPTIME} -lt ${CRON_TICK_PERIOD} ]; then
        return 1
    fi

    daemon_log_info "Executing cron jobs"

    if [ "`ls -A ${HOST_DAEMON_CRON_D}/* 2>/dev/null`" != "" ]; then
        for each in ${HOST_DAEMON_CRON_D}/*; do
            daemon_log_debug "Running cron part: ${each}"
            sh ${each} ${CURRENT_UPTIME}
        done
    fi

    CRON_RAN_UPTIME=${CURRENT_UPTIME}
}
