#!/bin/sh
#
#	$NetBSD: createbuildlink,v 1.21 2004/03/18 10:47:04 jlam 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 or buildlink3.mk from a package's Makefile
# and PLIST
#

REV=`echo '$Revision: 1.21 $' | sed 's/\\$//g'`
tmpdir=/tmp
spacesintab=8
makefile=Makefile
sedrules=$tmpdir/sedrules.buildlink.$$
PLIST=PLIST
blver=2
otherver=3

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

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

if [ $blver = 2 -a ! -f "$PLIST" ]; then
	echo "===> Incomplete package! To create a buildlink file <==="
	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@@
# XXX
# XXX This file was created automatically using createbuildlink-@PKGVERSION@.
# XXX After this file as been verified as correct, the comment lines
# XXX beginning with "XXX" should be removed.  Please do not commit
# XXX unverified buildlink[23].mk files.
# XXX
# XXX Packages that only install static libraries or headers should
# XXX include the following line:
# XXX
# XXX	BUILDLINK_DEPMETHOD.$PKGNOVER?=	build

EOF

##
## buildlink 2
##
if [ $blver = 2 ]; then

sed -f $sedrules <<EOF
.if !defined(${PKGUPPER}_BUILDLINK2_MK)
${PKGUPPER}_BUILDLINK2_MK=	# defined

EOF

else	# buildlink 3

gap="	"
for i in 1 2 3; do
	n=`expr $i \* $spacesintab`
	if [ ${#PKGUPPER} -ge $n ]; then
		gap="	$gap"
	fi
done

sed -f $sedrules <<EOF
BUILDLINK_DEPTH:=${gap}\${BUILDLINK_DEPTH}+
${PKGUPPER}_BUILDLINK3_MK:=	\${${PKGUPPER}_BUILDLINK3_MK}+
EOF

sed -f $sedrules <<EOF

.if !empty(BUILDLINK_DEPTH:M+)
BUILDLINK_DEPENDS+=	${PKGNOVER}
.endif

BUILDLINK_PACKAGES:=	\${BUILDLINK_PACKAGES:N${PKGNOVER}}
BUILDLINK_PACKAGES+=	$PKGNOVER

.if !empty(${PKGUPPER}_BUILDLINK3_MK:M+)
EOF

fi	# buildlink 2/3

if [ $blver = 2 ]; then

sed -f $sedrules <<EOF
BUILDLINK_PACKAGES+=		$PKGNOVER
BUILDLINK_DEPENDS.$PKGNOVER?=	$PKGNOVER>=$PKGVER
EOF

else # [ $blver = 3 ]

sed -f $sedrules <<EOF
BUILDLINK_DEPENDS.$PKGNOVER+=	$PKGNOVER>=$PKGVER
EOF

fi

sed -f $sedrules <<EOF
BUILDLINK_PKGSRCDIR.$PKGNOVER?=	../../$CURDIR
EOF

if [ $blver = 3 ]; then

sed -f $sedrules <<EOF
.endif	# ${PKGUPPER}_BUILDLINK3_MK
EOF

fi

##
## buildlink2-only part
##
if [ $blver = 2 ]; then
sed -f $sedrules <<EOF

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

##
## buildlinked includes (buildlink2 only)
##
if [ "$join" = "YES" ]; then
	for i in `egrep "^include/|/Headers/.*\.h" $PLIST \
	| sed -e 's|[^/]*\.h\([Hhpx+]*\)$|*.h\1|' | sort -u`; 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 (buildlink2 only)
##
for i in \
`egrep '^lib/|/Libraries/.*/lib[^\.]*\.a$|/Libraries/.*/lib[^\.]*\.so' $PLIST \
| egrep -v '\.pc$' | sed -e 's/\.a$/.*/' -e 's/\.la$/.*/' -e 's/\.so.*$/.*/'  \
| sort -u`; do
	echo "BUILDLINK_FILES.$PKGNOVER+=	$i"
done

fi	## end of buildlink2-only part

##
## buildlinked dependencies
##
grep -l '^.include.*\.\.\/.*\/.*/buildlink[23].mk\"' $makefile $commons \
	>/dev/null 2>&1 && echo ""	# Be careful not to print duplicate \n
for i in $makefile $commons ; do
	[ ! -f $i ] || grep '^.include.*\.\.\/.*\/.*/buildlink[23].mk\"' $i |
		sed "s/buildlink$otherver/buildlink$blver/"		|
		egrep -v '/devel/pkgconfig/|/textproc/intltool/'
done
if [ $blver = 2 ]; then
grep -l '^.include.*\.\.\/.*\/.*/buildlink[23].mk\"' $makefile $commons \
	>/dev/null 2>&1 && echo ""	# Be careful not to print duplicate \n
fi

##
## buildlink2-only part
##
if [ $blver = 2 ]; then
##
## main buildlink target for this package
##
sed -f $sedrules <<EOF
BUILDLINK_TARGETS+=	${PKGNOVER}-buildlink

EOF

##
## buildlink targets for this package
##
sed -f $sedrules <<EOF
${PKGNOVER}-buildlink: _BUILDLINK_USE

.endif	# ${PKGUPPER}_BUILDLINK2_MK
EOF

else	# buildlink 3

sed -f $sedrules <<EOF

BUILDLINK_DEPTH:=     \${BUILDLINK_DEPTH:S/+\$//}
EOF

fi	# buildlink 2/3

rm -f $sedrules
