#!/bin/sh
update_config_files ()
{
  package="$1"
  config_file="$2"
  old_path="$3"
  new_path="$4"

  if [ -f "${config_file}" ] && /bin/grep -q "${old_path}" "${config_file}" > /dev/null 2>&1; then
    DATE=$(date +"%Y%m%d%H%M")
    #create backup if there wasn't one created this minute
    if [ ! -f "${config_file}_rpm_update-${DATE}" ]
    then
      cp "${config_file}" "${config_file}_rpm_update-${DATE}" ||
      { %{logmsg} "Could not create backup file
      ${config_file}_rpm_update-${DATE}" >&2; exit 1; }
    fi

    sed -i "s|${old_path}|${new_path}|g" "${config_file}" \
        || { %{logmsg} "Could not adapt ${config_file} - please do manually" >&2 ; exit 1; }
    %{logmsg} "WARNING: ${config_file} adapted to new configuration"
    %{logmsg} "         (see >rpm -q ${package} --changelog | less<)"
    %{logmsg} "         Backup file is: ${config_file}_rpm_update-$DATE"
  fi

  if [ -e "${old_path}" ]
  then
    %{logmsg} "WARNING: ${old_path} has been moved"
    %{logmsg} "         to ${new_path}"
    %{logmsg} "         Please check/adapt your configuration/scripts and move the still needed files to the new directory!"
  fi
}

move_file_to_new_location ()
{
  old_path="$1"
  new_path="$2"

  if [ -f "${old_path}" ]
  then
      cp -ua "${old_path}" "${new_path}" || \
      { %{logmsg} "Could not copy ${old_path} to ${new_path} \
      because a newer version already exists" >&2; exit 1; }
      rm "${old_path}"
  fi
}

# update icinga config files
if [ "$1" = "icinga" ]
then
  # determine if service was running to be able to restart it after the update
  [ -f %{icinga_lockfile} ] && service_was_running=true

  [ $service_was_running ] && rcicinga stop >/dev/null 2>&1 ||:

  #############################################
  # update paths to new  run directory
  update_config_files "icinga" "%{icinga_sysconfdir}/icinga.cfg" "/var/spool/icinga/icinga.cmd" "%{icinga_cmdfile}"
  # update paths to new  run directory
  #############################################

  #############################################
  # update paths to new  eventhandler directory
  update_config_files "icinga" "%{icinga_sysconfdir}/resource.cfg" "/usr/lib/nagios/plugins/eventhandlers" "%{icinga_eventhandlerdir}"
  # update paths to new  eventhandler directory
  #############################################

  #############################################
  # update paths to new  spool directory
  update_config_files "icinga" "%{icinga_sysconfdir}/icinga.cfg" "/var/lib/icinga" "%{icinga_spooldir}"
  update_config_files "icinga" "%{icinga_sysconfdir}/objects/commands.cfg" "/var/lib/icinga" "%{icinga_spooldir}"
  # update paths to new  spool directory
  #############################################

  #############################################
  # move necessary data to new spool directory
  move_file_to_new_location "/var/lib/icinga/retention.dat" "%{icinga_spooldir}/"
  move_file_to_new_location "/var/lib/icinga/idomod.tmp" "%{icinga_spooldir}/"
  move_file_to_new_location "/var/lib/icinga/objects.precache" "%{icinga_spooldir}/"
  # move necessary data to new  spool directory
  #############################################

  #############################################
  # cleanup remaining unneeded files
  test -e "/var/spool/icinga/icinga.cmd" && rm "/var/spool/icinga/icinga.cmd"
  test -f "/var/lib/icinga/status.dat" && rm "/var/lib/icinga/status.dat"
  test -f "/var/lib/icinga/objects.cache" && rm "/var/lib/icinga/objects.cache"
  # cleanup remaining unneeded files
  #############################################

  # start service if it was running before
  [ $service_was_running ] && rcicinga start >/dev/null 2>&1 ||:
fi

# update icinga-idoutils config files
if [ "$1" = "icinga-idoutils" ]
then
  # determine if service was running
  [ -f %{icinga_ido2db_lockfile} ] && service_was_running=true

  [ $service_was_running ] && rcido2db stop >/dev/null 2>&1 ||:

  #############################################
  # update paths to new run directory
  update_config_files "icinga-idoutils" "%{icinga_sysconfdir}/ido2db.cfg" "/var/spool/icinga/ido2db.sock" "%{icinga_ido2db_socketfile}"
  update_config_files "icinga-idoutils" "%{icinga_sysconfdir}/idomod.cfg" "/var/spool/icinga/ido2db.sock" "%{icinga_ido2db_socketfile}"

  # cleanup old socket file
  test -e "/var/spool/icinga/ido2db.sock" && rm "/var/spool/icinga/ido2db.sock"
  # update paths to new  spool directory
  #############################################

  # start service if it was running before and there wasn't a major upgrade
  [ $service_was_running ] && rcido2db start >/dev/null 2>&1 ||:
fi
