# $FreeWRT: src/share/misc/licence.template,v 1.7 2006/04/09 22:08:49 tg Rel $ */
#-
# Copyright (c) 2006
#	Thorsten Glaser <tg@mirbsd.de>
#
# Licensee is hereby permitted to deal in this work without restric-
# tion, including unlimited rights to use, publicly perform, modify,
# merge, distribute, sell, give away or sublicence, provided all co-
# pyright notices above, these terms and the disclaimer are retained
# in all redistributions or reproduced in accompanying documentation
# or other materials provided with binary redistributions.
#
# All advertising materials mentioning features or use of this soft-
# ware 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 documentation. Specifically, re-using this code in any work
# covered by the GNU General Public License version 1 or Library Ge-
# neral Public License (any version) is permitted.
#
# Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind,
# express, or implied, to the maximum extent permitted by applicable
# law, without malicious intent or gross negligence; in no event may
# licensor, an author or contributor be held liable for any indirect
# or other damage, or direct damage except proven a consequence of a
# direct error of said person and intended use of this work, loss or
# other issues arising in any way out of its use, even if advised of
# the possibility of such damage or existence of a nontrivial bug.

GMAKE?=		$(PWD)/lbin/gmake
GMAKE_FMK=	${GMAKE} -f $(PWD)/build.mk
GMAKE_INV=	${GMAKE_FMK} --no-print-directory

all: .prereq_done
	@${GMAKE_INV} all

v: .prereq_done
	(echo; echo "Build started on $$(LC_ALL=C TZ=UTC date)"; set -x; \
	    ${GMAKE_FMK} all V=99) 2>&1 | tee -a make.log

verbose: .prereq_done
	@${GMAKE_FMK} all V=99

clean: .prereq_done
	@${GMAKE_INV} clean

config: .prereq_done
	@${GMAKE_INV} config

cleandir: .prereq_done
	@${GMAKE_INV} cleandir
	@-rm -rf lbin
	@-rm -f make.log .prereq_done

distclean: .prereq_done
	@${GMAKE_INV} distclean
	@-rm -rf lbin
	@-rm -f make.log .prereq_done

image_clean: .prereq_done
	@${GMAKE_INV} image_clean

menuconfig: .prereq_done
	@${GMAKE_INV} menuconfig

oldconfig: .prereq_done
	@${GMAKE_INV} oldconfig

package_index: .prereq_done
	@${GMAKE_INV} package_index

source: .prereq_done
	@${GMAKE_INV} source

target_clean: .prereq_done
	@${GMAKE_INV} target_clean

world: .prereq_done
	@${GMAKE_INV} world

prereq:
	@rm -f .prereq_done
	@${MAKE} .prereq_done

prereq-noerror:
	@rm -f .prereq_done
	@${MAKE} .prereq_done NO_ERROR=1

NO_ERROR=0
.prereq_done:
	@-rm -rf .prereq_done lbin
	@if ! bash --version 2>&1 | fgrep 'GNU bash' >/dev/null 2>&1; then \
		echo "FreeWRT requires GNU bash to be installed, sorry."; \
		exit 1; \
	fi
	@mkdir lbin
	@if which gmake >/dev/null 2>&1; then \
		ln -s $$(which gmake) lbin/gmake; \
	else \
		ln -s $$(which make) lbin/gmake; \
	fi
	@if which gtar >/dev/null 2>&1; then \
		ln -s $$(which gtar) lbin/tar; \
	else \
		ln -s $$(which tar) lbin/tar; \
	fi
	@if ! which md5sum >/dev/null 2>&1; then \
		cp scripts/md5sum.bsd lbin/md5sum; \
		chmod 555 lbin/md5sum; \
	fi
	@if test x"$$(uname)" = x"MirBSD"; then \
		sed -e 's!@@FromOS@@!MirBSD!g' -e 's!@@ToOS@@!OpenBSD!g' \
		    -e "s!@@PROG@@!$$(which uname)!g" \
		    <scripts/uname.fake >lbin/uname; \
		chmod 555 lbin/uname; \
	fi
	@echo "BASH=$$(which bash)" >>lbin/prereq.mk
	@echo "GNU_HOST_NAME=$$(${CC} -dumpmachine | sed -e 's!mirbsd!openbsd!g')" >>lbin/prereq.mk
	@echo 'HOSTCC=${CC}' >>lbin/prereq.mk
	@x="$$(echo 'f:\n\techo P:$${CFLAGS}\n.include <bsd.prog.mk>' | \
	    ${MAKE} -f - f EXPERIMENTAL=yes 2>/dev/null | grep '^P:' \
	    2>/dev/null | sed 's/^P://')"; \
	    echo "HOSTCFLAGS=$${x:--O2 ${CFLAGS}}" | sed 's/ *$$//' \
	    >>lbin/prereq.mk
	@echo 'LC_ALL=C' >>lbin/prereq.mk
	@echo 'MAKE=${GMAKE}' >>lbin/prereq.mk
	@echo "OStype=$$(env NOFAKE=yes uname)" >>lbin/prereq.mk
	@echo "_PATH=$$PATH" >>lbin/prereq.mk
	@echo "PATH=$$(pwd)/lbin:$$PATH" >>lbin/prereq.mk
	@echo "SHELL=$$(which bash)" >>lbin/prereq.mk
	@echo "export BASH HOSTCC HOSTCFLAGS LC_ALL MAKE OStype PATH SHELL" \
	    >>lbin/prereq.mk
	@env NO_ERROR=${NO_ERROR} bash scripts/scan-tools.sh
	@ln -s $$(which ${CC}) lbin/gcc
	@echo '===> Prerequisites checked successfully.'
	@touch $@

.PHONY: prereq prereq-noerror
