#!/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`"

PDIR="`pwd`"

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
}

#
# Move a list of files given in a file $1 into the tmp archive
# at directory $2. Only do it if $1 exists
#
function copyfiles()
{
	if [ -f $1 ]; then
		install -d tmp/$2
		X=`cat $1`
		(cd $PDIR;tar cf - $X ) | (cd tmp/$2;tar xpf -)
	fi
}

# 
# Package specific things
#
# Called with one parameter which is the package name
function package()
{
	install -d tmp/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/$1
		fi
	done

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

	# The case of a daemon without the final d
	if [ -f init ]; then
		debinit `expr $1 : '\(.*\)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

	copyfiles docs usr/doc/$PACKAGE

	copyfiles examples usr/doc/$PACKAGE/examples

	if [ -d tmp/usr/doc ]; then
		chown -R root.root tmp/usr/doc

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

	# Info files?
	if [ -f info ]; then
		echo "-- Installing info files"
		FILES=""
		. ./info
		if [ "$FILES" ]; then
			install -d tmp/usr/info
			echo $FILES | (cd $PDIR;cpio -o) | (cd tmp/usr/info;cpio -imd)
		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
	fi


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


	# Check for executables to run dpkg-shlibdeps
	echo "-- Checking for executable binaries in package"
	X=`find 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 nodeps ]; then
					echo $i >>/tmp/$$
				else
					if grep -q $i 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 -Tsubstvars $X
		fi
	fi

	cd 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
	if [ -d usr/doc ]; then
		X="`find usr/doc -type f -size +4k`"
		if [ "$X" != "" ]; then
			echo "-- Compressing Documentation"
			gzip -9 $X
		fi
	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
	cd ..
}


function packages()
{
	BASE=$1
	shift
	for i in $*; do
		if [ -d debian/$i ]; then
			echo "** Package $i"
			if [ -f debian/$i/files ]; then
				echo "-- Moving files from debian/tmp into $i"
	(cd debian/tmp;tar --create --remove-files --file - `cat ../$i/files`) \
	| (cd debian/$i/tmp;tar xpf -)
			fi
			(cd debian/$i;package $i)
			dpkg-gencontrol -p$i -Pdebian/$i/tmp -Tdebian/$i/substvars
			chown -R root.root debian/$i/tmp
			chmod -R go=rX debian/$i/tmp
			if [ -x debian/$i/prebuild ]; then
				(cd debian/$i/prebuild;debian/$i/prebuild)
			fi
			dpkg --build debian/$i/tmp ..
		else
			echo "Directory debian/$i for subpackage missing"
		fi
	done
	echo "** Main Package $BASE"
	(cd debian;package $BASE)
}

#
# Things uniquely done to the base package
# 
cd debian

install -d -m755 tmp/usr/doc/$PACKAGE
# 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

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

# Manpage scan
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

# Create the binary packages
PACKAGES=`grep "Package:" debian/control | sed -e 's/Package: //g'|tr "\n" " "`

if [ "$PACKAGES" ]; then
	packages $PACKAGES
else
	echo "No Packages specified in debian/control"
fi

exit 0
