#! /bin/sh
#
# initscripts preinst
#

set -e

# Remove a no-longer used conffile
#
# $1: conffile
#
# If the argument was not listed as a conffile, silently do nothing.
# Adapted from code obtained from http://wiki.debian.org/DpkgConffileHandling
eliminate_conffile() {
	PKGNAME="initscripts"
	CONFFILE="$1"

	if [ -e "$CONFFILE" ]; then
		CURRENT_MD5SUM="`md5sum \"$CONFFILE\" | sed -e \"s/ .*//\"`"
		FACTORY_MD5SUM="`dpkg-query -W -f='${Conffiles}' $PKGNAME | sed -n -e \"\\\\' $CONFFILE'{s/ obsolete$//;s/.* //p}\"`"
		if [ "$CURRENT_MD5SUM" != "$FACTORY_MD5SUM" ]; then
			echo "Obsolete conffile $CONFFILE has been modified by you."
			echo "Saving as $CONFFILE.dpkg-old ..."
			mv -f "$CONFFILE" "$CONFFILE".dpkg-old
		else
			echo "Removing unmodified and obsolete conffile $CONFFILE ..."
			rm -f "$CONFFILE"
		fi
	fi
}

# Compares a file to the "factory md5sum", and if it matches, removes it.
# This is useful for when converting from to a conffile *and* changing
# its contents at the same time.
convert_to_conffile() {
       CONFFILE="$1"
       FACTORY_MD5SUM="$2"

       if [ -e "$CONFFILE" ]; then
	       CURRENT_MD5SUM="`md5sum \"$CONFFILE\" | sed -e \"s/ .*//\"`"
	       if [ "$CURRENT_MD5SUM" = "$FACTORY_MD5SUM" ]; then
		       rm -f "$CONFFILE"
	       fi
       fi
}


case "$1" in
  install|upgrade)
	#
	# /etc/init.d/stop-bootlogd used to be a symlink to bootlogd;
	# now it is a separate script.  We need to remove the symlink here,
	# before dpkg installs the /etc/init.d/stop-bootlogd file.
	#
	[ -L /etc/init.d/stop-bootlogd ] && rm -f /etc/init.d/stop-bootlogd
	#
	# Remove obsolete conffiles
	#
	if [ "$2" ] && dpkg --compare-versions "$2" lt "2.86.ds1-10" ; then
		eliminate_conffile "/etc/init.d/bootclean.sh"
	fi
	#
	# The /etc/init.d/bootclean script fragment was moved to
	# /lib/init/ in version 2.86.ds1-39
	#
	if [ "$2" ] && dpkg --compare-versions "$2" lt "2.86.ds1-54" ; then
		eliminate_conffile "/etc/init.d/bootclean"
	fi
	#
	# Removed as we switched to Upstart
	#
	if [ "$2" ] && dpkg --compare-versions "$2" lt "2.87dsf-4ubuntu6"; then
		eliminate_conffile "/etc/network/if-up.d/mountnfs"
		eliminate_conffile "/etc/init.d/bootlogs"
		eliminate_conffile "/etc/init.d/checkfs.sh"
		eliminate_conffile "/etc/init.d/checkroot.sh"
		eliminate_conffile "/etc/init.d/mountkernfs.sh"
		eliminate_conffile "/etc/init.d/hostname.sh"
		eliminate_conffile "/etc/init.d/bootmisc.sh"
		eliminate_conffile "/etc/init.d/mountall.sh"
		eliminate_conffile "/etc/init.d/mountdevsubfs.sh"
		eliminate_conffile "/etc/init.d/rmnologin"
		eliminate_conffile "/etc/init.d/mtab.sh"
		eliminate_conffile "/etc/init.d/mountoverflowtmp"
		eliminate_conffile "/etc/init.d/mountnfs.sh"
		eliminate_conffile "/etc/init.d/mountnfs-bootclean.sh"
		eliminate_conffile "/etc/init.d/mountall-bootclean.sh"
	fi
	#
	# A final straggler from the upstart conversion
	#
	if [ "$2" ] && dpkg --compare-versions "$2" lt 2.88dsf-13.10ubuntu1
	then
		eliminate_conffile "/etc/default/tmpfs"
	fi
	#
	# Move conflicting log _file_ if present
	#
	[ -f /var/log/fsck ] && mv -f /var/log/fsck /var/log/fsck.dpkg-old
	;;
  abort-upgrade)
	exit 0
	;;
esac



:
