#!/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
	. $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 tarfile expand_dir dist_dir

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

	dist_dir="$(echo  "$expand_dir" | sed -e 's,/[^/]\+$,,')"
	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=$(echo $metadatafile | sed -e 's/~~.*$//' )

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

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

	[ -f $metadatafile ] && rm $metadatafile

}

case "$1" in
    purge)
	if [ -d $STATUS_LIB ]; then
		for x in $( find $STATUS_LIB -type f -name '*.conf' ) ; do
			uninstall_repo "$x"
		done

		if [ -d "$DL_CACHE" ]; then
			for metadatafile in $(find $DL_CACHE -name "*~~*--*.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 \
			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" -o -L "$f" ]; then
				rm "$f" || true
			fi
		done

		[ -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"
			[ -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
