#!/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
# All the following parameters are documentation files to be installed.
# (which is obsolete since the doc files can also be listed in debian/docs)

# Global Variables

DATE="`822-date`"

if [ "$1" = "-m" ]; then
	NOAUTOMAN=1
	shift
fi

PACKAGE=$1

# Subroutines

# addscript customizes a given script and adds it to one of the regular
# installation scripts. If the script does not exist then it is generated.
# Parameters:
# $1=scriptname to be appended to in debian/tmp/DEBIAN
function addscript()
{
	# If there is no script yet then put in the proper header
	if [ ! -f tmp/DEBIAN/$1 ]; then
		install /usr/lib/deb-make/emptyscript tmp/DEBIAN/$1
	fi
	# Make sure the files given are separated by blanks!
	FILES=`echo $FILES | tr "\n" " "`
	# Customize the script and add to the script indicated
	sed >>tmp/DEBIAN/$1  -e "s/#PACKAGE#/$PACKAGE/g" \
                -e "s/#DATE#/$DATE/g" \
		-e "s/#INITPARAMS#/$INITPARAMS/g" \
		-e "s/#SCRIPT#/$SCRIPT/g" \
		-e "s/#DESCR#/\"$DESCR\"/g" \
		-e "s/#SECTION_NAME#/\"$SECTION_NAME\"/g" \
		-e "s/#SECTION_MATCH#/\"$SECTION_MATCH\"/g" \
		-e "s&#FILE#&$FILE&g" \
		-e "s&#FILES#&$FILES&g"
}


# debinit handles the installation of an init.d script
# Parameters:
# $1= name in /etc/init.d
# $2 = scriptname
function debinit()
{
	SCRIPT=$1
	install -d tmp/etc/init.d
	install $2 tmp/etc/init.d/$SCRIPT
	INITPARAMS=`grep "^FLAGS=" $2`
	if [ "$INITPARAMS" != "" ]; then
		INITPARAMS=`expr "$INITPARAMS" : 'FLAGS="\(.*\)"'`
	fi
	if [ "$INITPARAMS" = "" ]; then
		INITPARAMS="defaults"
	fi
	echo "-- Adding /etc/init.d maintenance commands for $SCRIPT $INITPARAMS"
	if grep -q NO_RESTART_ON_UPGRADE $2; then
		addscript postrm </usr/lib/deb-make/init.postrm
		addscript postinst </usr/lib/deb-make/init.postinst.norestart
	else
		# Add the scripts necessary to manage the init.d script
		addscript postrm </usr/lib/deb-make/init.postrm
		addscript postinst </usr/lib/deb-make/init.postinst
		addscript prerm </usr/lib/deb-make/init.prerm
	fi
}

function dive()
{
	addscript preinst <<EOF
dpkg-divert --package $3 --add --rename \\
                --divert $2 $1
EOF
	addscript postrm <<EOF
dpkg-divert --package $3 --remove --rename \\
                --divert $2 $1
EOF
}

function diversion()
{
   addscript postrm <<EOF
# diversion added by debstd $DATE
if [ "\$1" = "remove" ]; then
EOF
   while [ 1 ]; do
	read X
   	if [ "$X" = "" ]; then
		echo "fi"|addscript postrm
		return
	   else
		dive $X
   	fi
   done
}

#
# Main Routine
#

cd debian

install -d -m755 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

# Record the Info of the machine the package was build on
dpkg -l gcc 'libc*' binutils ldso make dpkg-dev | awk '$1 == "ii" { printf("%s-%s\n", $2, $3) }' >tmp/usr/doc/$PACKAGE/buildinfo.Debian

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

# Check up on stuff in etc directory
if [ -d tmp/etc ]; then
	if [ -f conffiles ]; then
		cd tmp
		for i in `find etc -type f`; do
			if ! grep -q $i ../conffiles; then
				echo "Warning: File $i provided in binary but is not listed as a conffile!"
			fi
		done
		cd ..
	else
		echo "Warning: Files installed in etc and no conffile!"
	fi
fi

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

if [ -f init.d ]; then
	debinit $PACKAGE init.d
fi

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

# Purging files/directories on "purge" ?

if [ -f purge ]; then
	echo "-- Adding purge actions to postrm"
	FILES="`cat purge`"
	addscript postrm </usr/lib/deb-make/purge.postrm
fi

# etc files that could need some tweaking
for i in services inittab crontab protocols profile shells rpc shells \
	syslog.conf conf.modules modules \
	X11/Xresources X11/config X11/window-managers X11/xinit ; do
	if [ -f $i ]; then
		FILE=$i
		cat /usr/lib/deb-make/etc.first.postinst $i \
			/usr/lib/deb-make/etc.last.postinst | addscript postinst
		addscript postrm </usr/lib/deb-make/etc.postrm
	fi
done

if [ -f diversions ]; then
	diversion <diversions
fi

if [ -f inetd.conf ]; then
	echo "update-inetd --add \"`cat inetd.conf`\"" | addscript postinst
	echo "update-inetd --remove \"^`awk '{ print $1; }' inetd.conf`\"" | addscript postrm
fi

if [ -f conffiles ]; then
	install -m644 conffiles tmp/DEBIAN
	for i in `cat conffiles`; do
		if [ ! -e tmp/$i ]; then
			echo "Warning: Configuration file $i not provided in binary package!"
		fi
	done
fi

cd ..

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

if [ -f debian/docs ]; then
	cp -a `cat debian/docs` debian/tmp/usr/doc/$PACKAGE
fi

# Example files ?
if [ -f debian/examples ]; then
	echo "-- Installing examples"
	install -d debian/tmp/usr/doc/$PACKAGE/examples
	cp -a `cat debian/examples` debian/tmp/usr/doc/$PACKAGE/examples
fi

chown -R root.root debian/tmp/usr/doc

chmod 644 `find debian/tmp/usr/doc -type f`
chmod 755 `find debian/tmp/usr/doc -type d`

# Info files?
if [ -f debian/info ]; then
	echo "-- Installing info files"
	cd debian
	FILES=""
	. ./info
	if [ "$FILES" ]; then
		install -d tmp/usr/info
		cp ../$FILES tmp/usr/info
	fi
	if [ "$SECTION_MATCH" = "" ]; then
		addscript postinst </usr/lib/deb-make/info.postinst
	else
		addscript postinst </usr/lib/deb-make/info.regexp.postinst
	fi
	addscript postrm </usr/lib/deb-make/info.postrm
	cd ..
fi


# Check for libraries provided by the package
X=`find debian/tmp/lib debian/tmp/usr/lib debian/tmp/usr/X11R6/lib -type f -name "lib*.so.*" 2>/dev/null`
if [ "$X" != "" ]; then
	echo "-- Processing provided sharable libraries"
	>debian/tmp/DEBIAN/shlibs
	for i in $X; do
		echo "Library $i"
		LIBRARY=`expr $i : ".*/\(.*\)\.so\.,*"`
		VERSION=`expr $i : ".*/.*\.so\.\(.*\)"`
		echo "$LIBRARY $VERSION $PACKAGE" >>debian/tmp/DEBIAN/shlibs
	done
	echo "ldconfig" | (cd debian;addscript postinst )
fi


# Check for executables to run dpkg-shlibdeps
echo "-- Checking for executable binaries in package"
X=`find debian/tmp -type f -perm +111| tr "\n" " "`
if [ "$X" = "" ]; then
	echo "Warning: No executables in the package!"
else
	>/tmp/$$
	for i in $X; do
		if file $i|grep -q "ELF"; then
			if [ ! -f debian/nodeps ]; then
				echo $i >>/tmp/$$
			else
				if grep -q $i debian/nodeps; then
					echo -n "No Dependencies for "
				else
					echo $i >>/tmp/$$
				fi
			fi
			if file $i|grep -q "stripped"; then
				echo "$i ELF Binary"
			else
				echo "$i ELF Binary NOT STRIPPED."
			fi
			strip --remove-section=comment --remove-section=note $i
		else
			echo "$i Not ELF Binary"
		fi
	done
	X="`cat /tmp/$$`"
	rm /tmp/$$
	if [ "$X" = "" ]; then
		echo "No ELF binaries in the package"
	else
		echo "-- Running dpkg-shlibdeps on ELF Binaries"
		dpkg-shlibdeps $X
	fi
fi
if [ "$NOAUTOMAN" = "" ]; then
	echo "-- Looking for manpages in sourcepackage"
	# Install manpages that might be somewhere in the source 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 -o -e debian/tmp/usr/X11*/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
fi

cd debian/tmp

# Compress manpages. Convert manpages that have just .so in them to symlinks
if [ -d usr/man -o -d usr/X11*/man ]; then
	echo "-- Compressing/Converting Manpages"
	X=`find usr/man usr/X11*/man -type f -size -50c 2>/dev/null`
	if [ "$X" != "" ]; then
		for i in $X; do
			Y=`cat $i`
			Y=`expr "$Y" : '\.so \(.*\)'`			
			if [ "$Y" != "" ]; then
				# Convert it to a symlink
				rm $i
				ln -s ../$Y $i
				echo "Manpage $i converted into symlink (../$Y)"
			else
				echo "Manpage $i does not seem to contain .so although smaller than 50 bytes."
				echo "Something might be wrong here!"
			fi
		done
	fi
	gzip -9 `find usr/man usr/X11*/man -type f 2>/dev/null`
fi

# Compress Info
if [ -d usr/info ]; then
	echo "-- Compressing Info files"
	gzip -9 `find usr/info -type f`
fi

# Compress Documentation
X="`find usr/doc -type f -size +4k`"
if [ "$X" != "" ]; then
	echo "-- Compressing Documentation"
	gzip -9 $X
fi

# Ensure that the symlinks survived the cure and point them to the gzipped
# file if necessary
echo "-- Checking Symlinks"
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
