#!/bin/sh
set -e

PKG="calamares-settings-suryaos"
BASE_DIR="/etc/calamares/modules"

divert_remove() {
  orig="$1"
  alt="$2"
  # If the diverted file exists and no replacement remains, try to restore it
  if [ -e "$alt" ] && [ ! -e "$orig" ]; then
    mv -f "$alt" "$orig" || true
  fi
  dpkg-divert --package "$PKG" --remove --rename --quiet \
    --divert "$alt" "$orig" || true
}

case "$1" in
  purge)
    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
    divert_remove "/usr/share/applications/kubuntu-calamares.desktop" "/usr/share/applications/kubuntu-calamares.desktop.distrib"
    ;;
esac

exit 0
