#!/bin/sh -e
case "$1" in
	configure)
		# This is a sanity check to make sure our diversions arn't
		# messed up. (I have a history of getting this wrong. :-( )
		#
		# What it looks like on the disk is probably that
		# we have one of 3 situations:
		#
		# 1. filename and filename.real are both non-symlinks
		#    To fix, remove filename.real. (This happens if
		#    filename is upgraded)
		# 2. filename is a symlink, filename.real not.
		#    This is not a problem.
		# 3. filename is a symlink, filename.real does not
		#    exist. The package providing filename was 
		#    installed and then removed.
		for file in `dpkg-divert --list |grep "by xaw-wrappers"  |cut -f 3 -d " "`
		do
			if [ ! -L "$file" -a ! -L "$file.real" ]; then
				# 1. 
				echo -n "	$file: "
				rm -f "$file.real"
				echo fixed.
				dpkg-divert --quiet --package xaw-wrappers --remove "$file"
			elif [ -L "$file" -a -e "$file.real" -a ! -L "$file.real" ]; then
				# 2.
				true
			elif [ -L "$file" -a ! -e "$file.real" ]; then
				# 3.
				echo -n "	$file: "
				rm -f "$file"
				echo "fixed."
				dpkg-divert --quiet --package xaw-wrappers --remove "$file"
			fi
		done

		# There is another error, which should now be fixed that 
		# makes the file and the .real both be real files. Thus, I 
		# added this --fixup switch to update-xaw-wrappers, which 
		# makes it detect the condition and fix it.
		# As I said, it should be fixed, but just in case...
		/usr/sbin/update-xaw-wrappers --fixup
	;;
esac
