#!/bin/sh -e
#
# Automatically find and install man pages. However, do not install any man 
# pages listed on the command line.
# This is a little bit DWIMish, but still very handy.

PATH=debian:$PATH:/usr/lib/debhelper
. dh_lib

for PACKAGE in $DH_DOPACKAGES; do
        TMP=`tmpdir $PACKAGE`

	# Find all filenames that look like man pages.
	# .ex files are examples installed by deb-make, we don't want those, or
	# .in files, which are from configure.
	# We also need to exclude all debian/tmp type dirs.
	EXCLUDE=`grep ^Package: debian/control | cut -d " " -f 2 | tac | tr "\n" "|"`
	for file in `find * -name "*.[1-9]*" ! -name "*.ex" ! -name "*.in" | egrep -v "^debian/(${EXCLUDE}tmp)"`; do
		# Make sure file thinks they are man pages.
		if file $file|grep -q roff; then
			if echo $file|grep -q /; then
				NAME=`expr $file : '.*/\(.*\)'`
			else
				NAME=$file
			fi
			# Look at the command line and check if we should
			# install the file.
			install=1
			for notinstall in $@; do
				if [ "$NAME" = "$notinstall" -o \
				     "$file" = "$notinstall" ]; then
					install=""
				fi
			done
			if [ "$install" ]; then
				SECTION=man`expr $NAME : '.*\.\([123456789]\)'`
				if [ ! -e $TMP/usr/man/$SECTION/$NAME -a \
				     ! -e $TMP/usr/X11*/man/$SECTION/$NAME ]; then
					if [ ! -d $TMP/usr/man/$SECTION ]; then
						doit "install -d $TMP/usr/man/$SECTION"
					fi
					doit "install -p -m644 $file $TMP/usr/man/$SECTION/$NAME"
				fi
			fi
		fi
	done
done
