#!/bin/sh
# Two jobs, both about not surprising a machine that already works.
set -e

case "$1" in
  install|upgrade)
    # 1. Refuse a release this archive has no packages for. site/setup.sh makes
    #    the same check for the same reason: a signed, empty index for the
    #    wrong suite makes `apt update` succeed and `apt install` then say
    #    "Unable to locate package" with nothing to explain it. Failing here is
    #    louder than shipping a source file that quietly offers nothing.
    codename=""
    if [ -r /etc/os-release ]; then
      codename=$(. /etc/os-release && echo "${VERSION_CODENAME:-}")
    fi
    if [ "$codename" != "resolute" ]; then
      echo "error: the NorviTech archive has packages only for 'resolute';" >&2
      echo "       this machine reports '${codename:-unknown}'." >&2
      exit 1
    fi

    # 2. Adopt a hand-written sources file instead of colliding with it.
    #    Machines set up by the older setup.sh have this exact path, owned by
    #    nobody, with Signed-By pointing at /etc/apt/keyrings. Left in place,
    #    dpkg would treat it as a locally modified conffile and ask questions
    #    on a non-interactive install. Two copies of the same source with
    #    different Signed-By values would also make `apt update` fail. Moving
    #    it aside keeps the install silent and keeps the old file. .dpkg-old is
    #    a name apt skips without a notice.
    src=/etc/apt/sources.list.d/norvi.sources
    if [ -e "$src" ] && ! dpkg-query -S "$src" >/dev/null 2>&1; then
      mv "$src" "$src.dpkg-old"
      echo "note: kept your previous $src as $src.dpkg-old"
    fi
    ;;
esac

#DEBHELPER#
exit 0
