#!/bin/bash

set -e

if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-remove" ]; then
  # Remove old 40-* config files (this hook should be removed in a while)
  for FILE in /etc/apt/preferences.d/40-pointlinux.pref /etc/apt/apt.conf.d/40-pointlinux.conf; do
    if [ -e $FILE ]; then
      ucf --purge $FILE
      rm $FILE
    fi
  done
  # Install new config files
  export UCF_FORCE_CONFFNEW=yes
  for FILE in /etc/default/grub /etc/apt/preferences.d/50pointlinux /etc/apt/apt.conf.d/99pointlinux; do
    if [ -e $FILE.new ]; then
      ucf $FILE.new $FILE
      ucf --purge $FILE.new
      rm $FILE.new
    fi
  done
  # Update grub
  if [ -e /boot/grub/grub.cfg ]; then
    update-grub
  fi
fi
