#!/bin/sh
set -eu

PKGNAME="$1"
MODE="$2" # PRE-INSTALL, POST-INSTALL, DEINSTALL, POST-DEINSTALL

case "$MODE" in
POST-INSTALL)
	# try to replace the e2fsck and its wrapper, fsck_ext2fs, 
	# symbolic links by hard links if possible (pkg ships them as symlinks)
	d1=${PKG_ROOTDIR}/sbin/ ;              dev1=$(stat -Lf %Xd "$d1")
	d2=${PKG_ROOTDIR}${PKG_PREFIX}/sbin/ ; dev2=$(stat -Lf %Xd "$d2")
	if [ "$dev1" = "$dev2" ] && [ "$(realpath "$d1")" != "$(realpath "$d2")" ]; then
		for i in e2fsck fsck_ext2fs ; do
			ln -fhP ${d1}${i} ${d2}${i}
		done
		for i in ext2 ext3 ext4 ; do
			ln -fhP ${d2}e2fsck ${d2}fsck.$i
		done
		echo >&2 "NOTE: replaced e2fsprogs symlinks by hard links, will recalculate checksums with a root at(1) job."
		pid=$$
		echo /bin/sh -c "\"set -x ; { while sleep 2 </dev/null ; do kill -0 $pid || break ; done ; \
			pkg check -r "${PKG_NAME}" ; } </dev/null 2>&1 | logger -t pkg.e2fsprogs.POST-INSTALL \"" \
			| at now
	fi
	#
	# install configuration file and update config files from
	# old "ext4dev" to current "ext4" name.
	#
	if test -s ${PKG_PREFIX}/etc/mke2fs.conf; then
		if cmp -s ${PKG_PREFIX}/etc/mke2fs.conf.dist \
				${PKG_PREFIX}/etc/mke2fs.conf; then
			true
		else
			rc=0
			grep -q ext4dev ${PKG_PREFIX}/etc/mke2fs.conf || rc=$?
			# !!! the echo messages below deliberately contain spacing errors
			# so that the file names can be double-clicked in a typical console
			# window for copying and pasting without keen targeting with the mouse.
			case $rc in
			1) # ext4dev not found (old name)
				cp -f -p ${PKG_PREFIX}/etc/mke2fs.conf.dist \
					${PKG_PREFIX}/etc/mke2fs.conf.e2fsprogs-new
				echo "==========================================================================="
				echo "Warning: installing mke2fs.conf in ${PKG_PREFIX}/etc/mke2fs.conf.e2fsprogs-new"
				echo "Check to see if you need to update your ${PKG_PREFIX}/etc/mke2fs.conf ."
				echo "==========================================================================="
				;;
			0) # ext4dev found (old name)
				mv ${PKG_PREFIX}/etc/mke2fs.conf \
					 ${PKG_PREFIX}/etc/mke2fs.conf.e2fsprogs-old
				cp -f -p ${PKG_PREFIX}/etc/mke2fs.conf.dist \
					${PKG_PREFIX}/etc/mke2fs.conf
				echo "==========================================================================="
				echo "Your mke2fs.conf is too old.  Backing up old version in"
				echo "${PKG_PREFIX}/etc/mke2fs.conf.e2fsprogs-old .  Please check to see"
				echo "if you have any local customizations that you wish to preserve."
				echo "==========================================================================="
				;;
			*) # grep failed
				exit $rc
				;;
			esac
		fi
	else
		# missing -> install
		cp -p   ${PKG_PREFIX}/etc/mke2fs.conf.dist \
			${PKG_PREFIX}/etc/mke2fs.conf
	fi

	;;
DEINSTALL)
	# !!! the echo messages below deliberately contain spacing errors
	# so that the file names can be double-clicked in a typical console
	# window for copying and pasting without keen targeting with the mouse.
	if cmp -s ${PKG_PREFIX}/etc/mke2fs.conf \
		${PKG_PREFIX}/etc/mke2fs.conf.dist
	then
		rm -f ${PKG_PREFIX}/etc/mke2fs.conf
	else
		echo "If and only if you are deleting e2fsprogs forever,"
		echo "remember to delete ${PKG_PREFIX}/etc/mke2fs.conf ."
	fi
	# e2fsck.conf is no longer part of the distribution, but still supported,
	# => no pkg-list @sample line possible
	#    and no reference e2fsck.conf.sample or e2fsck.conf.dist is available 
	if test -f ${PKG_PREFIX}/etc/e2fsck.conf
	then
		echo "If and only if you are deleting e2fsprogs forever,"
		echo "remember to delete ${PKG_PREFIX}/etc/e2fsck.conf ."
	fi
	;;
esac
