#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2026 The Dodobian authors
#
# Before our /usr/lib/os-release is unpacked, the one from base-files is diverted to
# /usr/lib/os-release.debian, where base-files keeps updating it. postrm puts it back.
# base-files is Essential, so on install its file is copied, never moved: /etc/os-release, a link
# to it, must not point at nothing. dpkg then replaces it with ours in one rename.
#
# A first install (or one after a remove) also leaves a mark for postinst to make Dodobian dpkg's
# vendor; an upgrade leaves no mark, so the vendor stays as the administrator has it.
set -eu

pending=/var/lib/dodobian-desktop/vendor-pending

case "$1" in
	install)
		mkdir -p "${pending%/*}"
		: > "$pending"
		;;
esac

case "$1" in
	install|upgrade)
		diverted=$(dpkg-divert --list /usr/lib/os-release)
		case $diverted in
			*" by dodobian-desktop") ;;
			*)
				cp -p /usr/lib/os-release /usr/lib/os-release.debian
				dpkg-divert --package dodobian-desktop --add --no-rename \
					--divert /usr/lib/os-release.debian /usr/lib/os-release
				;;
		esac
		;;
esac

#DEBHELPER#

exit 0
