#!/bin/sh -e

rm_conffile() {
    PKGNAME="$1"
    CONFFILE="$2"

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

case "$1" in
    install|upgrade)
	# Check for config file in the old place and remove link(s) to it
	for conf in masterkey-gui-default-apache-directory.conf \
	    masterkey-gui-default-apache-alias.conf \
	    masterkey-gui-default-apache-virtual.conf; do		    
	  if [ -e /etc/$conf ]; then
	      WEBSERVERS="apache apache-ssl apache2"
	      for apache in $WEBSERVERS; do
		  if [ -L /etc/$apache/conf.d/$conf ]; then
		      echo "Removing obsolete configuration file $conf for $apache.."
		      rm -f /etc/$apache/conf.d/$conf
		  fi
	      done
	  fi
	  rm_conffile masterkey-gui-default /etc/$conf
	done
	conf=masterkey-gui-default.conf
	if [ -e /etc/$conf ]; then
	    rm_conffile masterkey-gui-default /etc/$conf
	    if [ -L /usr/share/masterkey-gui-default/config.masterkey ]; then
		echo "Removing symlink config.masterkey.."
	    fi
	fi
        ;;
esac



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



exit 0

