#!/bin/sh
# postrm script for di-netboot-assistant

set -eu
DL_CACHE=/var/cache/di-netboot-assistant/
STATUS_LIB=/var/lib/di-netboot-assistant
TFTP_ROOT=/var/lib/tftpboot
N_A_DIR=d-i/n-a

if [ -e /etc/di-netboot-assistant/di-netboot-assistant.conf ] ; then
    . /etc/di-netboot-assistant/di-netboot-assistant.conf
fi
if [ -f $STATUS_LIB/.old-conf-for-purge ]; then
	# shellcheck source=/dev/null
	. $STATUS_LIB/.old-conf-for-purge
fi

#This function should be kept in sync with function "uninstall_repo" in di-netboot-assistant
uninstall_repo() {
	dist_conf="$1"			# Repository's .conf file
	local s metadatabasename expand_dir dist_dir

	metadatabasename="${dist_conf%.conf}"
	expand_dir="$(grep -E "^[[:blank:]]*expand_dir=" "$dist_conf" | sed -e 's/^[[:blank:]]*expand_dir=//')"
	[ "$expand_dir" != "/" ] && [ -d "$expand_dir" ] && rm -Rf "$expand_dir"

	dist_dir=$(dirname "$expand_dir")
	rmdir --ignore-fail-on-non-empty "$dist_dir"

	s="$metadatabasename.pxelinux.menu.fragment"
	[ -f "$s" ] && rm "$s"

	s="$metadatabasename.uboot.menu.fragment"
	[ -f "$s" ] && rm "$s"

	s="$metadatabasename.grub.menu.fragment"
	[ -f "$s" ] && rm "$s"

	s="$metadatabasename.pxelinux.menu.serial-9600.fragment"
	[ -f "$s" ] && rm "$s"

	rm "$dist_conf"
}

#This function should be kept in sync with function "url2filename" in di-netboot-assistant
url2filename() {
	sed -e 's#//\+#/#g' -e 's#[^[:alnum:]@+_~\.-]#_#g'
}

#This function should be kept in sync with function "remove_repocache" in di-netboot-assistant
remove_repocache() {
	local metadatafile="$1"		# repository to uncache
	local base file

	base="${metadatafile%~~*}"

	sed -n -e 's/^[[:blank:]]*dl_file=[[:blank:]]*//p' "$metadatafile" | while read -r file ; do
		rm "${base}_$(echo "$file" | url2filename)"
	done

	#Purge remaing files (MD5SUMs...) if there are no more cached
	#distribution from the same repository.
        if ! echo "${base}~~"*.meta | grep -qv "$metadatafile" ; then
		rm "${base}"_*
	fi

	[ -f "$metadatafile" ] && rm "$metadatafile"
}

case "$1" in
    purge)
	if [ -d $STATUS_LIB ]; then
		for x in "$STATUS_LIB"/*.conf ; do
			[ -f "$x" ] || continue
			uninstall_repo "$x"
		done

		if [ -d "$DL_CACHE" ]; then
			for metadatafile in "$DL_CACHE"/*~~*--*.meta ; do
				remove_repocache "$metadatafile"
			done
		fi

		for lnk in grubx64.efi grub ; do
			if [ "$TFTP_ROOT/$lnk" -ef "$TFTP_ROOT/$N_A_DIR/$lnk" ] ; then
				rm "$TFTP_ROOT/$lnk" || true
			fi
		done

		for x in pxelinux.0 elilo.conf elilo.efi README.txt boot.ipxe \
			pxelinux.cfg/default pxelinux.cfg/default.mig-bak \
			pxelinux.cfg/menu.c32 pxelinux.cfg/vesamenu.c32 \
			ldlinux.c32 libcom32.c32 libutil.c32 \
			bootnetx64.efi grubx64.efi \
			; do
			f="$TFTP_ROOT/$N_A_DIR/$x"
			if [ -f "$f" ] || [ -L "$f" ] ; then
				rm "$f" || true
			fi
		done

		# shellcheck disable=SC2015
		[ -d "$TFTP_ROOT/$N_A_DIR/grub" ] && rm -r "$TFTP_ROOT/$N_A_DIR/grub" || true

		for x in $N_A_DIR/pxelinux.cfg $N_A_DIR ; do
			f="$TFTP_ROOT/$x"
			# shellcheck disable=SC2015
			[ -d "$f" ] && rmdir --ignore-fail-on-non-empty "$f" || true
		done

		if [ -f $STATUS_LIB/.old-conf-for-purge ]; then
		       	rm $STATUS_LIB/.old-conf-for-purge || true
		fi
		rmdir --ignore-fail-on-non-empty $STATUS_LIB
	fi
    ;;
    remove)
	if [ -f /etc/di-netboot-assistant/di-netboot-assistant.conf ]; then
		[ ! -d $STATUS_LIB ] && mkdir $STATUS_LIB
		sed -e "1s/^/#BACKUP, to purge our TFTP stuff when purged\n/" \
			< /etc/di-netboot-assistant/di-netboot-assistant.conf \
			> $STATUS_LIB/.old-conf-for-purge
	fi
    ;;
    upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
    ;;

    *)
	echo "postrm called with unknown argument \`$1'" >&2
	exit 1
    ;;
esac

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

#DEBHELPER#

exit 0
