#!/bin/sh -e
#
# Automatically generate shlibs files.

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

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

	if [ -e "$TMP/DEBIAN/shlibs" ]; then
		error "$TMP/DEBIAN/shlibs already exists."
	fi

	for file in `find $TMP -type f -name "*.so.*" | tr "\n" " "` ; do
		LIBRARY=`expr $file : ".*/\(.*\)\.so\..*"` || true
		LIB_VERSION=`expr $file : ".*/.*\.so\.\(.*\)"` || true
		if [ -z "$DH_M_PARAMS" ]; then
			MAJOR=`expr $LIB_VERSION : "\([0-9]*\)\."` || true
		else
			MAJOR="$DH_M_PARAMS"
		fi
#		LIBSTUB=`expr $file : "\(.*\/.*\.so\)\..*"` || true
		if [ ! -d "$TMP/DEBIAN" ] ; then
			doit "install -d $TMP/DEBIAN"
		fi
		DEPS=$PACKAGE
		if [ "$DH_V_FLAG_SET" ]; then
			if [ "$DH_V_FLAG" ]; then
				DEPS="$DH_V_FLAG"
			else
				# Call isnative becuase it sets $VERSION
				# as a side effect.
				isnative || true
				DEPS="$PACKAGE (>= $VERSION)"
			fi
		fi
		if [ "$LIBRARY" -a "$MAJOR" -a "$DEPS" ]; then
			complex_doit "echo '$LIBRARY $MAJOR $DEPS' >>$TMP/DEBIAN/shlibs"
		fi
	done

	if [ -e "$TMP/DEBIAN/shlibs" ]; then
		doit "chmod 644 $TMP/DEBIAN/shlibs"
		doit "chown root.root $TMP/DEBIAN/shlibs"
	fi
done
