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

set -e
DL_CACHE=/var/cache/di-netboot-assistant/
STATUS_LIB=/var/lib/di-netboot-assistant
TFTP_ROOT=/var/lib/tftpboot
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$//' )"

	#remove di-netboot-assistant < 0.37 cached files.
	tarfile="$(grep -E "^[[:blank:]]*dl_file=" "$dist_conf" | sed -e 's/^[[:blank:]]*dl_file=//')"
	if [ "$(echo $tarfile | sed -n -e 's/^\(.\).*/\1/p')" = "/" ]; then
		[ -f "$tarfile" ] && rm "$tarfile"
	fi

	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.pxelinux.menu.serial-9600.fragment"
	[ -f "$s" ] && rm "$s"

	s="$metadatabasename.elilo.conf.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 $rm_verbosity ${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 $rm_verbosity ${base}_*
	fi

	[ -f $metadatafile ] && rm $metadatafile

}

remove_pre037_md5sums() {
	#remove di-netboot-assistant < 0.37 checksum files.
	find "$DL_CACHE" -type f ! -name '*_MD5SUMS' \
		| grep -q "" \
		|| find "$DL_CACHE" -type f -name '*_MD5SUMS' \
		| xargs -r rm 
}

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

			remove_pre037_md5sums
		fi

		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 \
			; do
			f="$TFTP_ROOT/debian-installer/$x"
			if [ -f "$f" -o -L "$f" ]; then
				rm "$f" || true
			fi
		done

		for x in debian-installer/pxelinux.cfg debian-installer; 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 TFT 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
