#!/bin/sh

# Script to be called from debian/rules to setup all the debian specifc
# required files
# Christoph Lameter, <clameter@debian.org> October 10, 1996
#
# Script is called with one parameter which is the package name

PACKAGE=$1

cd debian

install -d tmp/usr/doc/$PACKAGE tmp/DEBIAN
# Required files in /usr/doc/package
install -m644 copyright tmp/usr/doc/$PACKAGE
install -m644 changelog tmp/usr/doc/$PACKAGE/changelog.Debian

# Special README
if [ -f README.debian ]; then
	install -m644 README.debian tmp/usr/doc/$PACKAGE
fi

# Installation scripts
for i in postinst preinst prerm postrm; do
	if [ -f $i ]; then
		install $i tmp/DEBIAN
	fi
done

if [ -f conffiles ]; then
	install -m644 conffiles tmp/DEBIAN
fi

# Deal with scripts in etc directories
for i in cron.daily cron.weekly cron.monthy init.d rc.boot; do
	if [ -f $i ]; then
		install -d tmp/etc/$i
		install $i tmp/etc/$i/$PACKAGE
	fi
done

# The case of a daemon without the final d
if [ -f init ]; then
	install -d tmp/etc/init.d
	install init tmp/etc/init.d/`expr $PACKAGE : '\(.*\)d$'`
fi

if [ -x ../$PACKAGE ]; then
	cd ..
	dpkg-shlibdeps $PACKAGE
	cd debian
fi

cd ..

# Install manpages that might be somewhere in the package
for i in `find * -name "*.[1-8]"`; do
	if ! expr $i : 'debian/tmp/.*' >/dev/null; then
	if file $i|grep -q troff; then
	  	if echo $i|grep -q /; then
			NAME=`expr $i : '.*/\(.*\)'`
		else
			NAME=$i
		fi
		SECTION=man`expr $NAME : '.*\.\([12345678]\)'`
		if [ -e debian/tmp/usr/man/$SECTION/$NAME ]; then
			echo "Manpage $NAME exists.";
		else
			if [ ! -d debian/tmp/usr/man/$SECTION ]; then
				install -d debian/tmp/usr/man/$SECTION
			fi
			install -m644 $i debian/tmp/usr/man/$SECTION/$NAME
			echo "Manpage $NAME added."
		fi
	fi
	fi
done

# Install Documentation files specified on the commandline
shift
while [ "$1" != "" ]; do
	install -m644 $1 debian/tmp/usr/doc/$PACKAGE
	shift
done

# Compress manpages and documentation files which are bigger than 1K
cd debian/tmp
gzip -9 `find usr/doc usr/man usr/info -type f -size +1k`

# Ensure that the symlinks survived the cure and point them to the gzipped
# file if necessary
for i in `find . -type l`; do
	DIRECTORY=`expr $i : "\(.*\)/[^/]*"`
	NAME=`expr $i : ".*/\([^/]*\)"`
	LINKVAL=`ls -l $DIRECTORY/$NAME | awk '{ print $11;}'`
#	echo "I=$i D=$DIRECTORY N=$NAME L=$LINKVAL"
	if [ ! -e $DIRECTORY/$LINKVAL ]; then
		if [ -f $DIRECTORY/$LINKVAL.gz ]; then
			rm $DIRECTORY/$NAME
			ln -s $LINKVAL.gz $DIRECTORY/$NAME.gz
			echo "Changed symlink $i to $i.gz"
		else
			echo "Dangling symlink $i pointing to $LINKVAL"
		fi
	fi
done


exit 0
