#!/bin/bash
# Check and remind users to (re-)enable monitoring
# otherwise user can forget to (re-)enable it back

set -e

if [ -z "${FORCE_UPGRADE}" ] ; then
  echo "ERROR: Missing mandatory environment variable '\$FORCE_UPGRADE', exiting." >&2
  exit 1
fi

if dpkg-query -s "check-mk-agent" >/dev/null 2>&1 ; then
  echo
  echo "REMINDER: monitoring detected (package 'check-mk-agent' is installed)."
  echo "Please ensure monitoring is (re-)enabled back on all servers!"

  if [ "$FORCE_UPGRADE" = "true" ] ; then
    echo "Forcing as requested via environment variable FORCE_UPGRADE."
    exit 0
  fi

  while true; do
    echo -n "Did you (re-)enable monitoring? (yes/no): "
    read -r a
    case "${a,,}" in
      yes) echo "Thank you! Respect!" ; break ;;
      no) echo "I hope you know what you are doing, continue anyway..." ; break ;;
      * ) echo "Please answer 'yes' or 'no'." ;;
    esac
    unset a
  done

fi
