#!/bin/bash

# Warn the user when they have customized xivo_globals.conf directly.
# Custom global variables must now be set in a dedicated file inside
# /etc/asterisk/xivo_globals.d/ instead of editing xivo_globals.conf.
#
# When dpkg replaces a conffile the user had modified, it keeps the previous
# version as a ".dpkg-old" file. We use its presence to detect a customization
# and show the user exactly what they had changed. This runs before
# xivo-check-conffiles (which may remove the .dpkg-old file), so the file is
# still available here.

GLOBALS_CONF="/etc/xivo/asterisk/xivo_globals.conf"
GLOBALS_CONF_OLD="${GLOBALS_CONF}.dpkg-old"
CUSTOM_GLOBALS_CONF="/etc/asterisk/xivo_globals.d/custom_globals.conf"

cmn_echo_important() {
  local yellow=$(tput setaf 3)
  local reset=$(tput sgr0)
  echo -e "${yellow}$@${reset}"
}

if [ -f "$GLOBALS_CONF_OLD" ]; then
    cmn_echo_important "WARNING: you have customized ${GLOBALS_CONF} directly."
    cmn_echo_important "Custom global variables must now be set in a dedicated file:"
    cmn_echo_important "    ${CUSTOM_GLOBALS_CONF}"
    cmn_echo_important "Editing ${GLOBALS_CONF} directly is no longer supported and your"
    cmn_echo_important "changes will be lost on the next upgrade."
    echo
    cmn_echo_important "Below are the differences between the new packaged file and your"
    cmn_echo_important "previous (customized) one (--- new packaged file / +++ your old file):"
    echo
    diff -u "$GLOBALS_CONF" "$GLOBALS_CONF_OLD"
    echo
    cmn_echo_important "Please report your customizations into ${CUSTOM_GLOBALS_CONF}."
    cmn_echo_important "To review the differences interactively, run:"
    cmn_echo_important "    vimdiff ${GLOBALS_CONF} ${GLOBALS_CONF_OLD}"
fi
