#!/bin/bash
# Check and remind users to disable monitoring if any
# otherwise upgrade can be affected by 'apt-get update', etc

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 disabled on all servers during the upgrade!"

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

  while true; do
    echo -n "Did you disable monitoring? (yes/no): "
    read -r a
    case "${a,,}" in
      yes) echo "Thank you! Continue upgrade..." ; break ;;
      no) echo "Continue upgrade with enabled monitoring on your onw risk!" ; break ;;
      * ) echo "Please answer 'yes' or 'no'." ;;
    esac
    unset a
  done

fi
