#!/bin/mksh

set -e

# This maintainer script can be called the following ways:
#
# * new-postinst "configure" [$most_recently_configured_version]
# The package is unpacked; all dependencies are unpacked and, when there
# are no circular dependencies, configured.
#
# * old-postinst "abort-upgrade" $new_version
# * old-postinst "abort-remove"
# * conflictors-postinst "abort-remove" "in-favour" $new_package
#	$new_version
# * deconfigureds-postinst "abort-deconfigure" "in-favour"
#	$failed_install_package $fip_version		# new-package
#	["removing" $conflicting_package $cp_version]	# old-package
# The package is unpacked; all dependencies are at least Half-Installed,
# previously been configured, and not removed. In some error situations,
# dependencies may not be even fully unpacked.
#
# * postinst "triggered" "${triggers[*]}"
# For trigger-only calls, i.e. if "configure" is not called.
#
# * new-postinst "reconfigure" [$most_recently_configured_version](?)
# Treat this as just like "configure" for a future extension by debconf.

function stripcomments {
	set -o noglob
	while read _line; do
		_line=${_line%%#*}
		[[ -z $_line ]] || print -r -- $_line
	done
}

didmigrate=0
function domigrate {
	local m m1=$1 m2=$2 p=$3

	[[ -e $p ]] || return 0
	(( didmigrate )) || print -ru2 "I: rng-tools: migrating old conffiles"
	m=$(md5sum <"$p")
	if [[ $m = "$m1"* || $m = "$m2"* ]]; then
		# identical to shipped files, 2-unofficial-mt.14-1 or 5-1
		(( didmigrate |= 1 ))
		rm -- "$p"
		return 0
	fi
	m=$(stripcomments <"$p")
	if [[ -z $m ]]; then
		# empty, other than, perhaps, comments
		(( didmigrate |= 1 ))
		rm -- "$p"
		return 0
	fi
	mv -- "$p" "$p.dpkg-bak"
	p=$p.dpkg-bak
	if [[ $p != /etc/default/rng-tools.dpkg-bak ]]; then
		(( didmigrate |= 2 ))
		print -ru2 "W: local changes saved as ${p@Q}"
		return 0
	fi
	if [[ $m = *--hrng=tpm* ]]; then
		(( didmigrate |= 4 ))
		print -ru2 "E: old configuration uses rng-tools5 feature"
		print -ru2 "E: cannot migrate"
		print -ru2 "N: local changes saved as ${p@Q}"
		return 0
	fi
	dfltconv <"$p" >/etc/default/rng-tools-debian
	rm -- "$p"
	(( didmigrate |= 8 ))
	print -ru2 "I: /etc/default/rng-tools migrated to /etc/default/rng-tools-debian"
}

function dfltconv {
	local state=0 bk ln
	while IFS= read -r ln; do
		if [[ $state$ln = '0# Configuration for the rng-tools initscript' || \
		    $state$ln = '1# $Id: rng-tools.default,v 1.1.2.5 2008-06-10 19:51:37 hmh Exp $' || \
		    $state$ln = '2' ||
		    $state$ln = '3# This is a POSIX shell fragment' ]]; then
			bk+=$ln$'\n'
			let ++state
			continue
		fi
		[[ $ln != '#RNGDOPTIONS="--hrng=tpm --fill-watermark=90% --feed-interval=1"' ]] || \
		    continue
		[[ $ln != '# for the viapadlock and tpm drivers.' ]] || \
		    ln='# for the viapadlock driver.'
		if (( state == 4 )); then
			print -r -- '# -*- mode: sh -*-'
			print -r -- '#-'
			print -r -- '# Configuration for the rng-tools-debian initscript'
		elif (( state > 0 && state < 5 )); then
			print -nr -- "$bk"
		fi
		if (( state < 5 )); then
			(( state != 4 )) || print
			print -r -- '# migrated from /etc/defaults/rng-tools'
			[[ -z $ln ]] || print
			state=5
		fi
		print -r -- "$ln"
	done
	print -r -- '# For TPM (also add tpm-rng to /etc/modules):'
	print -r -- '#RNGDOPTIONS="--fill-watermark=90% --feed-interval=1"'
	print -r -- ''
	print -r -- '# If you need to configure which RNG to use, do it here:'
	print -r -- '#HRNGSELECT="virtio_rng.0"'
	print -r -- '# Use this instead of sysfsutils, which starts too late.'
}

case $1 in
(configure|reconfigure)
	domigrate 29a6dfb91ef76469550026ea912f9307 80e82742d3612fbcc5b2fe28d9be198e \
	    /etc/default/rng-tools
	domigrate 46da7b710e34cbaaf4a40077975bd7cc 7c9474cbf0b1317efd82ce1cce1c1648 \
	    /etc/logcheck/ignore.d.server/rng-tools
	domigrate 46da7b710e34cbaaf4a40077975bd7cc 7c9474cbf0b1317efd82ce1cce1c1648 \
	    /etc/logcheck/violations.ignore.d/rng-tools
	if (( didmigrate == 1 )); then
		print -ru2 "I: rng-tools: all old conffiles were pruned"
	elif (( didmigrate == 9 )); then
		print -ru2 "I: rng-tools: all old conffiles were pruned or merged"
	elif (( didmigrate & 2 )); then
		print -ru2 "I: rng-tools: some old conffiles were moved aside for review"
	fi
	if (( didmigrate & 8 )) && ! invoke-rc.d rng-tools-debian restart; then
		print -ru2 "W: rng-tools-debian still cannot be started"
	fi
	;;

(abort-upgrade|abort-remove|abort-deconfigure)
	;;

(triggered)
	;;

(*)
	echo >&2 "E: postinst called with unknown subcommand '$1'"
	exit 1
	;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
