#!/bin/bash

set -e

#
# Skip, if we are not in "configure" state
#
if [ "$1" != "configure" ]; then
    echo "I: Skipping configuration"
    exit 0
fi


#
# This is the prefix of the jobs/hierarchy.
#
PREFIX=/etc/bytemark-healthcheck

#
#  Cleanup old SSD-tests
#
find $PREFIX -name '*_ssd_*' -delete

#
# If we don't have any of the expected numbered directories then make them.
#
if [ ! -d ${PREFIX}/60 ]; then
    for i in 5 10 15 30 60 120 ; do
        mkdir -p ${PREFIX}/$i || true
    done
fi


##
## Here is where we setup the default tests, and their frequencies.
##


# RAID checks run four times an hour
ln -s ../jobs.d/software_raid          ${PREFIX}/15 2>/dev/null || true
ln -s ../jobs.d/tw_hardware_raid       ${PREFIX}/15 2>/dev/null || true
ln -s ../jobs.d/hp_hardware_raid       ${PREFIX}/15 2>/dev/null || true
ln -s ../jobs.d/megaraid_hardware_raid ${PREFIX}/15 2>/dev/null || true

# MCE run the same.
ln -s ../jobs.d/machine_check_exceptions ${PREFIX}/15 2>/dev/null || true
ln -s ../jobs.d/bad_ipv6                 ${PREFIX}/15 2>/dev/null || true

# Filesystem
ln -s ../jobs.d/disk_space_checker ${PREFIX}/15 2>/dev/null || true

# mailq
ln -s ../jobs.d/exim4_mailq   ${PREFIX}/15 2>/dev/null || true
ln -s ../jobs.d/postfix_mailq ${PREFIX}/15 2>/dev/null || true

# Bonding mode, and setup.
ln -s ../jobs.d/bond_check  ${PREFIX}/30   2>/dev/null || true

# apt-get checks
ln -s ../jobs.d/apt_upgrade_check  ${PREFIX}/30  2>/dev/null || true

# permissions
ln -s ../jobs.d/perm_check  ${PREFIX}/30  2>/dev/null || true

# syslog
ln -s ../jobs.d/syslog  ${PREFIX}/30  2>/dev/null || true

# NTP
ln -s ../jobs.d/ntp    ${PREFIX}/30  2>/dev/null || true



#
# Removal of obsolete tests - this is mostly left here as a template
# to show how future removals can be carried out.
#
#
for i in apt_update_check nic_checker ; do
    find ${PREFIX} -name $i -delete || true
done

#
# Now ensure that we remove any previous heartbeat.
#
# We've had a previous release which populated this script with
# a --clear-only flag, such that we can remove it safely without
# worrying about a raise.
#
# The reason we use a glob here is to cope with each of the following
# cases:
#
#    /etc/cron.d/bytemark-heartbeat
#    /etc/cron.d/bytemark-heartbeat.dpkg-old
#    /etc/cron.d/bytemark-heartbeat.dpkg-dist
#
# We're safe to leave the files with a .dpkg-foo suffix, because they
# won't run, but they look nasty and deserve to be cleaned up.
#
for i in  /etc/cron.d/bytemark-heartbea*; do
    if [ -e $i ]; then
        rm -f $i || true
    fi
done


#DEBHELPER#
exit 0
