#!/bin/sh
set -e

# Run before dpkg deletes package files (remove|upgrade|deconfigure).
PKG="calamares-settings-suryaos"
BASE_DIR="/etc/calamares/modules"

divert_remove() {
  orig="$1"
  alt="$2"
  dpkg-divert --package "$PKG" --list "$orig" 2>/dev/null | grep -q . || return 0
  if [ -e "$orig" ] && [ -e "$alt" ]; then
    rm -f "$orig"
  fi
  if [ -e "$alt" ] && [ ! -e "$orig" ]; then
    mv -f "$alt" "$orig"
  fi
  dpkg-divert --package "$PKG" --remove --rename --quiet \
    --divert "$alt" "$orig" 2>/dev/null || true
}

case "$1" in
  remove|upgrade|deconfigure)
    FILES="
    shellprocess_install_translations.conf
    before_bootloader_context.conf
    after_bootloader_context.conf
    shellprocess_logs.conf
    shellprocess@boot_deploy.conf
    shellprocess@boot_reconfigure.conf
    shellprocess@nomodeset.conf
    shellprocess@aptsources.conf
    "
    for f in $FILES; do
      ORIG="$BASE_DIR/$f"
      ALT="$BASE_DIR/$f.distrib"
      divert_remove "$ORIG" "$ALT"
    done
    ;;
esac

exit 0
