#!/usr/bin/env bash
# $FreeWRT: src/share/misc/licence.template,v 1.20 2006/12/11 21:04:56 tg Rel $
#-
# Copyright (c) 2006, 2007
#	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 for prerequisite host tools.

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

shopt -s extglob
topdir=$(pwd)
export PATH=$topdir/lbin:$PATH
out=0

if [[ $NO_ERROR != @(0|1) ]]; then
	echo Please do not invoke this script directly!
	exit 1
fi

set -e
rm -rf $topdir/lbin/tmp
mkdir -p $topdir/lbin/tmp
cd $topdir/lbin/tmp

os=$(uname)
case $os in
Linux)
	# supported with no extra quirks at the moment
	;;
OpenBSD)
	# supported with no extra quirks at the moment
	# although some packages' autoconf scripts may
	# not properly recognise OpenBSD
	;;
MirBSD)
	# needs a little quirk, because the autoconf
	# version of some packages doesn't recognise
	# it as valid OS, and as build (not host) OS
	# faking OpenBSD is close enough
	rm -f $topdir/lbin/uname
	sed -e 's!@@FromOS@@!MirBSD!g' -e 's!@@ToOS@@!OpenBSD!g' \
	    -e "s!@@PROG@@!$($topdir/lbin/which uname)!g" \
	    <$topdir/scripts/uname.fake >$topdir/lbin/uname
	chmod 555 $topdir/lbin/uname
	;;
*)
	# unsupported
	echo "Building FreeWRT on $os is currently unsupported."
	echo "Sorry."
	echo
	case $os in
	Darwin|Cyg*)
		echo "The most prominent issue is that filesystems"
		echo "can be case-insensitive. Some macros are missing."
		echo
		;;
	NetBSD|FreeBSD|DragonFly)
		echo "Building should succeed with relatively few"
		echo "patches, but perl must live in /usr/bin for"
		echo "now in FreeWRT, which should indeed be fixed."
		echo
		;;
	esac
	echo "If you need FreeWRT building on $os, please contact"
	echo "the development team; there may be contractors available"
	echo "for this task. If you intend on turning $os into one of"
	echo "the supported build OSes, please contact us as well so"
	echo "that we can feed back your enhancements into FreeWRT."
	exit 1
	;;
esac

set +e
cat >Makefile <<'EOF'
include $(TOPDIR)/lbin/prereq.mk
all: run-test

test: test.c
	${HOSTCC} ${HOSTCFLAGS} -o $@ $^ ${LDADD}

run-test: test
	./test
EOF
cat >test.c <<-'EOF'
	#include <stdio.h>
	int
	main()
	{
		printf("Yay! Native compiler works.\n");
		return (0);
	}
EOF
X=$(gmake TOPDIR=$topdir 2>&1)
if [[ $X != *@(Native compiler works)* ]]; then
	echo Cannot compile a simple test programme.
	echo You must install GNU make and a host C compiler,
	echo usually GCC, to proceed.
	out=1
fi
rm test*

X=$(gmake --version 2>&1 | grep '^GNU Make' | \
    sed -e 's/GNU Make //' -e 's/version //' -e 's/, by.*$//')
[[ $X = +([0-9]).+([0-9]).+([0-9]) ]] && X=${X%.*}
if [[ $X = +([0-9]).+([0-9]) ]]; then
	let major=${X%.*}
	let minor=${X#*.}
elif [[ $X = +([0-9]).+([0-9])beta* ]]; then
	# Beta version is not "the real thing"
	let major=${X%.*}
	X=${X%beta*}
	let minor=${X#*.}-1
else
	echo Cannot determine GNU make version number.
	out=1
fi
if (( (major < 3) || ((major == 3) && (minor < 81)) )); then
	echo GNU make $major.$minor too old.
	echo Please install GNU make 3.81 or higher to continue.
	if [[ $NO_ERROR = 1 || -s /etc/debian_version || \
	    -s /etc/fedora-release ]]; then
		echo WARNING: should abort here, continuing...
	else
		echo You can override this check, see http://www.freewrt.org/trac/wiki/Documentation/FAQ for details.
		out=1
	fi
fi

if ! which flex >/dev/null 2>&1; then
	echo You must install flex to continue.
	out=1
else
	echo '%%' | flex -
	if fgrep _POSIX_SOURCE lex.yy.c; then
		echo Your lexer \(flex\) contains a broken skeleton.
		if [[ $NO_ERROR = 1 ]]; then
			echo WARNING: continue at your own risk.
			echo Some packages may be broken.
		else
			echo You can continue the build by issuing \'make prereq-noerror\'
			echo However, several packages may faild to build correctly.
			out=1
		fi
	fi
fi

if ! which bison >/dev/null 2>&1; then
	echo You must install GNU Bison to continue.
	echo While you can install any version, it is '*STRONGLY*' suggested
	echo to install GNU Bison version 2.3 because of its bug fixes.
	out=1
fi
X=$(bison --version 2>&1 | fgrep 'GNU Bison' | sed 's/^[^0-9]*//')
V=old
if [[ $X = +([0-9]).+([0-9]) ]]; then
	let major=${X%.*}
	let minor=${X#*.}
	if (( (major > 2) || ((major == 2) && (minor >= 3)) )); then
		V=new
	fi
fi
if [[ $V = old ]]; then
	echo It is suggested to upgrade your copy of bison to
	echo GNU Bison 2.3 because of its bug fixes.
fi

if ! which gzip >/dev/null 2>&1; then
	echo You must install gzip to continue.
	out=1
fi

if ! which bzip2 >/dev/null 2>&1; then
	echo You must install bzip2 to continue.
	out=1
fi

if ! which unzip >/dev/null 2>&1; then
	echo You must install unzip to continue, sorry.
	out=1
fi

if ! which patch >/dev/null 2>&1; then
	echo You must install patch \(from Larry Wall\) to continue.
	out=1
fi

cat >test.c <<-'EOF'
	#include <stdio.h>
	#include <zlib.h>

	#ifndef STDIN_FILENO
	#define STDIN_FILENO 0
	#endif

	int
	main()
	{
		gzFile zstdin;
		char buf[1024];
		int i;

		zstdin = gzdopen(STDIN_FILENO, "rb");
		i = gzread(zstdin, buf, sizeof (buf));
		if ((i > 0) && (i < sizeof (buf)))
			buf[i] = '\0';
		buf[sizeof (buf) - 1] = '\0';
		printf("%s\n", buf);
		return (0);
	}
EOF
X=$(echo 'Yay! Native compiler works.' | gzip | \
    gmake TOPDIR=$topdir LDADD=-lz 2>&1)
if [[ $X != *@(Native compiler works)* ]]; then
	echo Cannot compile a libz test programme.
	echo You must install the zlib development package,
	echo usually called libz-dev, and the run-time library.
	out=1
fi

if [[ ! -s /usr/include/ncurses.h ]]; then
	echo Install ncurses header files, please.
	out=1
fi

if ! which g++ >/dev/null 2>&1; then
	echo You must install the host GNU C++ compiler to continue.
	out=1
fi

#if ! which perl >/dev/null 2>&1; then
if [[ "$(which perl 2>/dev/null)" != /usr/bin/perl ]]; then
	echo You must install Perl in /usr/bin to continue.
	out=1
fi

if ! which wget >/dev/null 2>&1; then
	echo You must install GNU wget to continue.
	out=1
fi

if ! which makeinfo >/dev/null 2>&1; then
	echo You must install GNU texinfo to continue.
	out=1
fi

if ! which ed >/dev/null 2>&1; then
	echo Why does your distribution not package the standard
	echo text editor, ed? Please install it to continue.
	out=1
elif [[ "$(which ed 2>/dev/null)" != /bin/ed ]]; then
	echo Your operating system installs ed, the standard text
	echo editor, not as /bin/ed. While you can build FreeWRT
	echo with this, ask your vendor to fix it, point to the
	echo FHS if needed.
fi


cd $topdir
rm -rf lbin/tmp
exit $out
