#!/bin/sh
#
#	$NetBSD: createbuildlink,v 1.11 2003/01/09 02:21:30 uebayasi Exp $
#
# Copyright (c) 2002 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Rene Hexel.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
# This product includes software developed by the NetBSD
# Foundation, Inc. and its contributors.
# 4. Neither the name of The NetBSD Foundation nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Create an initial buildlink2.mk from a package's Makefile and PLIST
#

REV=`echo '$Revision: 1.11 $' | sed 's/\\$//g'`
tmpdir=/tmp
makefile=Makefile
sedrules=$tmpdir/sedrules.buildlink.$$
PLIST=PLIST

args=`getopt j $*`
if [ $? != 0 ]; then
	echo "Usage: $0 [-j] > buildlink2.mk"
	exit 2
fi
set -- $args
while [ $# -gt 0 ]; do
	case "$1" in
	-j)
		join=YES
		;;
	--)
		shift; break
		;;
	esac
	shift
done

##
## some simple integrity checking
##
if [ ! -f $makefile ]; then
	echo "===> Incomplete package! To create a buildlink2.mk <==="
	echo "===> a working $makefile is required!               <==="
	exit 1
fi

if [ ! -f "$PLIST" ]; then
	echo "===> Incomplete package! To create a buildlink2.mk <==="
	echo "===> a working PLIST is required!                  <==="
	exit 1
fi

##
## try to find any included Makefile.common's
## 
commons=`grep '^.include.*Makefile.common\"' $makefile |		\
	sed 's/^.*"\(.*\)".*/\1/'`

##
## package specific variables
##
CURDIR=`pwd | sed 's|^.*/\([^/]*/[^/]*\)$|\1|'`
PKGNAME=`make show-var VARNAME=PKGNAME`
PKGVER=`echo $PKGNAME | sed -e 's/^.*-//'`
PKGNOVER=`echo $PKGNAME | sed -e 's/-[^-]*$//'`
PKGUPPER=`echo $PKGNOVER | tr '[:lower:]' '[:upper:]' | tr - _`
PREFIX=X11PREFIX
USE_X11BASE=`make show-var VARNAME=USE_X11BASE`
if [ -z "$USE_X11BASE" ]; then
	if ! grep -q "^USE_X11BASE" $makefile $commons ; then
		PREFIX=LOCALBASE
	fi
fi

##
## create sed rules
##
echo  >$sedrules "s|@@CURDIR@@|$CURDIR|g"
echo >>$sedrules "s|@@ID@@|\$NetBSD\$|g"
echo >>$sedrules "s|@@PKGNAME@@|$PKGNAME|g"
echo >>$sedrules "s|@@PKGNOVER@@|$PKGNOVER|g"
echo >>$sedrules "s|@@PKGUPPER@@|$PKGUPPER|g"
echo >>$sedrules "s|@@PKGVER@@|$PKGVER|g"
echo >>$sedrules "s|@@PREFIX@@|$PREFIX|g"
echo >>$sedrules "s|@@REV@@|$REV|g"
echo >>$sedrules "s|@@PKGVERSION@@|@PKGVERSION@|g"

#
# buildlink header
#
sed -f $sedrules <<EOF
# @@ID@@
#
# This Makefile fragment is included by packages that use $PKGNOVER.
#
# This file was created automatically using createbuildlink @PKGVERSION@.
#

.if !defined(${PKGUPPER}_BUILDLINK2_MK)
${PKGUPPER}_BUILDLINK2_MK=	# defined

BUILDLINK_PACKAGES+=			$PKGNOVER
BUILDLINK_DEPENDS.$PKGNOVER?=		$PKGNOVER>=$PKGVER
BUILDLINK_PKGSRCDIR.$PKGNOVER?=		../../$CURDIR

EVAL_PREFIX+=	BUILDLINK_PREFIX.$PKGNOVER=$PKGNOVER
BUILDLINK_PREFIX.${PKGNOVER}_DEFAULT=	\${$PREFIX}
EOF

##
## buildlinked includes
##
if [ "$join" = "YES" ]; then
	for i in `egrep "^include/|/Headers/.*\.h" $PLIST \
	| sed -e 's|[^/]*\.h$|*.h|' | sort | uniq`; do
		echo "BUILDLINK_FILES.$PKGNOVER+=	$i"
	done
else
	for i in `egrep "^include/|/Headers/.*\.h" $PLIST` ; do
		echo "BUILDLINK_FILES.$PKGNOVER+=	$i"
	done
fi

##
## buildlinked libraries
##
for i in \
`egrep '^lib/|/Libraries/.*/lib[^\.]*\.a$|/Libraries/.*/lib[^\.]*\.so' $PLIST \
|sed -e 's/\.a$/.*/' -e 's/\.la$/.*/' -e 's/\.so.*$/.*/' | sort | uniq`; do
	echo "BUILDLINK_FILES.$PKGNOVER+=	$i"
done

echo ""

##
## buildlinked dependencies
##
for i in $makefile $commons ; do
	[ ! -f $i ] || grep '^.include.*\.\.\/.*\/.*/buildlink2.mk\"' $i
done
grep -l '^.include.*\.\.\/.*\/.*/buildlink2.mk\"' $makefile $commons \
	>/dev/null 2>&1 && echo ""	# Be careful not to print duplicate \n

##
## main buildlink target for this package
##
echo "BUILDLINK_TARGETS+=	${PKGNOVER}-buildlink"
echo ""

##
## buildlink targets for this package
##
echo "${PKGNOVER}-buildlink: _BUILDLINK_USE"
echo ""

echo ".endif	# ${PKGUPPER}_BUILDLINK2_MK"

rm -f $sedrules
