#!/bin/sh -e
#
# Do some general file permission fixups.

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

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

	# General permissions fixing.
	if [ ! "$DH_EXCLUDE_FIND" ]; then
		# It's much faster to do it this way, but we can only do
		# this if there is nothing to exclude.
		if [ -d $TMP ]; then
			doit "chown -R root.root $TMP"
			doit "chmod -R go=rX $TMP"
			doit "chmod -R u+rw $TMP"
		fi

		FIND_OPTIONS=""
	else
		# Do it the hard way.
		complex_doit "find $TMP ! \( $DH_EXCLUDE_FIND \) -print0 \
			2>/dev/null | xargs -0r chown root.root"
		complex_doit "find $TMP ! \($DH_EXCLUDE_FIND \) -print0 \
			2>/dev/null | xargs -0r chmod go=rX"
		complex_doit "find $TMP ! \( $DH_EXCLUDE_FIND \) -print0 \
			2>/dev/null | xargs -0r chmod u+rw"

		FIND_OPTIONS="! \( $DH_EXCLUDE_FIND \)"
	fi

	# Fix up premissions in usr/doc, setting everything to not exectable
	# by default, but leave examples directories alone.
	complex_doit "find $TMP/usr/doc -type f $FIND_OPTIONS ! -regex .\*/examples/.\* -print0 \
	        2>/dev/null | xargs -0r chmod 644"
	complex_doit "find $TMP/usr/doc -type d $FIND_OPTIONS -print0 \
		2>/dev/null | xargs -0r chmod 755"

	# Executable man pages are a bad thing..
	complex_doit "find $TMP/usr/man/ $TMP/usr/X11*/man/ -type f \
		$FIND_OPTIONS -print0 2>/dev/null | xargs -0r chmod 644"

	# ..and so are executable shared and static libraries 
	# (and .la files from libtool)
	complex_doit "find $TMP -perm -5 -type f \
		\( -name '*.so*' -or -name '*.la' -or -name '*.a' \) $FIND_OPTIONS -print0 \
		2>/dev/null | xargs -0r chmod a-X"
done
