#!/usr/bin/env bash
# $FreeWRT: src/share/misc/licence.template,v 1.20 2006/12/11 21:04:56 tg Rel $
#-
# Copyright (c) 2006
#	Thorsten Glaser <tg@mirbsd.de>
#
# Provided that these terms and disclaimer and all copyright notices
# are retained or reproduced in an accompanying document, permission
# is granted to deal in this work without restriction, including un-
# limited rights to use, publicly perform, distribute, sell, modify,
# merge, give away, or sublicence.
#
# Advertising materials mentioning features or use of this work must
# display the following acknowledgement:
#	This product includes material provided by Thorsten Glaser.
# This acknowledgement does not need to be reprinted if this work is
# linked into a bigger work whose licence does not allow such clause
# and the author of this work is given due credit in the bigger work
# or its accompanying documents, where such information is generally
# kept, provided that said credits are retained.
#
# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
# the utmost extent permitted by applicable law, neither express nor
# implied; without malicious intent or gross negligence. In no event
# may a licensor, author or contributor be held liable for indirect,
# direct, other damage, loss, or other issues arising in any way out
# of dealing in the work, even if advised of the possibility of such
# damage or existence of a defect, except proven that it results out
# of said person's immediate fault when using the work as intended.
#-
# Scan host-tool prerequisites of certain packages before building.

if test -z "$BASH_VERSION"; then
	echo FreeWRT requires GNU bash to be installed, sorry.
	exit 1
fi

shopt -s extglob
topdir=$(readlink -nf $(dirname $0)/..)
export PATH=$topdir/lbin:$PATH
out=0

. $topdir/.config

#-- start adding dependencies here --

if [[ -n $FWRT_COMPILE_SABLEVM_CLASSPATH ]]; then
	NEED_JIKES="$NEED_JIKES sablevm-classpath"
fi

if [[ -n $FWRT_COMPILE_SABLEVM_CLASSPATH ]]; then
	NEED_FASTJAR="$NEED_FASTJAR sablevm-classpath"
fi

if [[ -n $FWRT_COMPILE_AVAHI ]]; then
	NEED_PKGCONFIG="$NEED_PKGCONFIG avahi"
fi

if [[ -n $FWRT_COMPILE_POPT ]]; then
	NEED_GETTEXT="$NEED_GETTEXT popt"
fi

if [[ -n $FWRT_PACKAGE_PRIVOXY ]]; then
	NEED_AUTOCONF_2_59="$NEED_AUTOCONF_2_59 privoxy"
fi

#-- start checking dependencies here --

if [[ -n $NEED_JIKES ]]; then
	if ! which jikes >/dev/null 2>&1; then
		echo >&2 You need jikes to build $NEED_JIKES
		out=1
	fi
fi

if [[ -n $NEED_GETTEXT ]]; then
	if ! which xgettext >/dev/null 2>&1; then
		echo >&2 You need gettext to build $NEED_GETTEXT
		out=1
	fi
fi

if [[ -n $NEED_FASTJAR ]]; then
	if ! which fastjar >/dev/null 2>&1; then
		echo >&2 You need fastjar to build $NEED_FASTJAR
		out=1
	fi
fi

if [[ -n $NEED_PKGCONFIG ]]; then
	if ! which pkg-config >/dev/null 2>&1; then
		echo >&2 You need pkg-config to build $NEED_PKGCONFIG
		out=1
	fi
fi

if [[ -n $NEED_AUTOCONF_2_59 ]]; then
	if ! AUTOCONF_VERSION=2.59 autoconf --help 2>&1 | \
	    fgrep gnu.org >/dev/null 2>&1; then
		echo >&2 You need autoconf 2.59 to build $NEED_AUTOCONF_2_59
		out=1
	fi
fi

if [[ -n $FWRT_USE_CCACHE ]]; then
        if ! which ccache >/dev/null 2>&1; then
                echo >&2 You have selected to build with ccache, but ccache could not be found.
                out=1
        fi
fi


#-- end of dependency checks

exit $out
