#! /bin/bash
# postinst script for open-xchange-munin-scripts
#
# see: dh_installdeb(1)

set -e

prevver="$2"

# summary of how this script can be called:
#        * <postinst> 'configure' <most-recently-configured-version>
#        * <old-postinst> 'abort-upgrade' <new version>
#        * <conflictor's-postinst> 'abort-remove' 'in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> 'abort-deconfigure' 'in-favour'
#          <failed-install-package> <version> 'removing'
#          <conflicting-package> <version>
# for details, see /usr/share/doc/packaging-manual/
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the 'postinst' is called with 'abort-upgrade',
#     'abort-remove' or 'abort-deconfigure'.

init_plugins() {
  TMPFILE=`mktemp /tmp/munin-node.configure.XXXXXXXXXX`
  echo "Initializing plugins.."
  munin-node-configure --shell > $TMPFILE || true 
  if [ -f $TMPFILE ] ; then
    sh < $TMPFILE
  else
    echo "nothing to do...."
  fi
  find -L /etc/munin/plugins -name 'ox_*' -type l -delete
  echo "done."
  rm -f $TMPFILE
  #init-system-helpers decides which init to use for restart
  service munin-node restart
}

function contains() { grep $@ >/dev/null 2>&1; return $?; }

postFix() {
  . /opt/open-xchange/lib/oxfunctions.sh
  GLOBIGNORE='*'

  # SoftwareChange_request-3870
  PFILE=/etc/munin/plugin-conf.d/ox
  if test -f ${PFILE} && ! contains env.oxJolokiaUrl ${PFILE}; then
    sed -i '$ a env.oxJolokiaUrl http://localhost:8009/monitoring/jolokia' ${PFILE}
  fi

  if test -f ${PFILE} && ! contains env.oxJolokiaUser ${PFILE}; then
    sed -i '$ {
      a ### oxJolokiaUser must be the same as com.openexchange.jolokia.user inside jolokia.properties
      a env.oxJolokiaUser changeMe!Now
    }' ${PFILE}
  fi

  if test -f ${PFILE} && ! contains env.oxJolokiaPassword ${PFILE}; then
    sed -i '$ {  
      a ### oxJolokiaPassword must be the same as com.openexchange.jolokia.password inside jolokia.properties
      a env.oxJolokiaPassword s3cr3t!toBeChanged
    }' ${PFILE}
  fi

  # The admin has to actively configure and start jolokia and munin
  PFILE=/opt/open-xchange/etc/jolokia.properties
  jolokia_enabled=$(ox_read_property com.openexchange.jolokia.start ${PFILE})
  if [[ ! ${jolokia_enabled//[[:space:]]/} = true ]]
  then
    echo -e "\n\e[31mWARNING\e[0m: You have to properly configure and activate jolokia and munin for working monitoring! \n"
  fi
}

case "$1" in
  configure)
    init_plugins
    test -n "$2" && {
    # we are in update mode, run postFix to apply fixes
    postFix "$2"
  }
  ;;

abort-upgrade|abort-deconfigure|abort-remove)
  :
  ;;

*)
  echo "postinst called with unknown argument \`$1'" >&2
  exit 0
  ;;
esac

# SoftwareChange_request-3870
dpkg-maintscript-helper rm_conffile /etc/munin/plugin-conf.d/ox_cache 7.8.4~  -- "$@"

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
