#!/bin/sh -
# BTW, I've inherited this and don't like it much but I've not havad
# enough tuits to fix it. -DS

set -e

PHDIR=/usr/lib/perl5/i486-linux/5.002

makeheaders () {
	echo "Creating Perl header files.  This may take a while..."
	echo "\nIf possible, you should use Socket and/or some of the other modules."
	echo "h2ph has always been somewhat broken."
	(cd /usr/include; find . -type d | (cd $PHDIR; xargs install -d))
	(cd $PHDIR; install -d linux; install -d asm)
	(cd /usr/include; find . -name '*.h' | xargs h2ph ) > /dev/null
	(cd /usr/include; h2ph linux/* asm/* ) > /dev/null
	if [ -d /usr/local/include ]; then
		(cd /usr/local/include; find . -type d | (cd $PHDIR; xargs install -d))
		(cd /usr/local/include; 
			for i in `find . -name '*.h'`; do
				cat $i | h2ph > $PHDIR/`echo $i | sed -e 's/\.h$/\.ph/'`;
			done;
		) > /dev/null
	fi
	# The next few lines are to fix a case where h2ph doesn't work very well
	# on __WAIT_INT
	cd $PHDIR/sys
	MATCHES=`fgrep '* &' wait.ph | wc -l`
	if [ $MATCHES -ne 1 ]; then
		echo "Found $MATCHES occurances of '* &' where I expected 1."
		echo -n "Please report this to debian-bugs@pixar.com.  Please see"
		echo "/usr/doc/debian-0.93/bug-reporting.txt for instructions."
	else 
		sed -e 's/\* \&//' wait.ph > wait.ph.new
		mv wait.ph.new wait.ph
	fi

	echo "Done."
	echo "You can re-run this script at any time as 'perlconfig'."
	echo "You should do this any time you install new header files"
	echo "in /usr/include."
}

(cd /usr/bin; test -f perl.dist && mv perl.dist perl)
if [ -f /usr/include/linux/kernel.h ]; then
	makeheaders
else

	echo "You don't seem to have a kernel source tree.  This will"
	echo "prevent much of the functionality involving Perl header"
	echo "(.ph) files from working properly.  You can get this by"
	echo "installing the 'source' or 'include' debian package or"
	echo "installing a linux kernel yourself.  Once you have done"
	echo "this, you can re-run this script as 'perlconfig' as root."
	echo 
fi;
