#!/bin/bash

set -e

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

#
# Remove the old duff cronjob if it exists
#
if [ -e /etc/cron.d/bytemark-service-monitor ] ; then
  rm -f /etc/cron.d/bytemark-service-monitor
fi

#
#  Cronjob needs to be removed on upgrade.
#
if [ -e /etc/cron.d/bytemark-vhost-monit ]; then
  rm -f  /etc/cron.d/bytemark-vhost-monit
fi

#
#  Remove old packaging mistake, if it still exists.
#
if [ -e /etc/bytemark-vhost/cron.d/bytemark-service-monitor ]; then
  rm -f /etc/bytemark-vhost/cron.d/bytemark-service-monitor
fi

if [ -d /etc/bytemark-vhost/cron.d ]; then
    rm -rf /etc/bytemark-vhost/cron.d
fi

if [ -d /etc/bytemark-vhost/monit.d ]; then
    rm -rf /etc/bytemark-vhost/monit.d
fi


# 
# Add var/lib directory for the state DB
#
mkdir -p /var/lib/symbiosis/

#DEBHELPER#

#
# Add symlinks for the monit script
# 
monit_dir="/etc/symbiosis/monit.d"
mkdir -p "$monit_dir"

for i in cron sshd; do
  monit_script="/usr/share/symbiosis/monit/checks/$i"
  link_target="$monit_dir/$i"

  if [ -x "$monit_script" ] && [ ! -e "$link_target" ]; then
    echo "I: Adding symlink for Symbiosis Monit script for $i"
    ln -s "$monit_script" "$link_target" || true
  fi
done

exit 0
