#!/bin/sh
# $Id: setup-build-all,v 1.42 2014/03/01 12:51:39 tom Exp $
# vi:ts=4 sw=4

# common functions for "build-all-XXX" scripts, which know about the compiler
# variations that are useful on each platform/host.

# calling script must override $PROG and $FILE
PROG=
FILE=

failed() {
	echo "? $*"
	exit 1
}

rename_icc_logs()
{
	for name in LOGS/*-icc-*.log
	do
		mv $name `echo "$name" | sed -e 's/-icc-/-icc'$1'-/'`
	done
}

rename_icc9_logs()
{
	rename_icc_logs 9
}

rename_icc12_logs()
{
	rename_icc_logs 12
}

build_all() {
	ME=`partition 2>/dev/null`
	test -z "$ME" && ME=`hostname`
	test -z "$ME" && ME=`uname -a`
	export ME

	if test -z "$PROG" ; then
		failed "you did not give a value for PROG"
	elif test -z "$FILE" ; then
		failed "you did not give a value for FILE"
	elif test ! -f $FILE ; then
		failed "not in the build-directory"
	fi

	test -d bin &&
	test ! -f Makefile.in &&
	test ! -f makefile.in &&
	rm -rf bin

	USE=cc
	TST=`path cc|head -n 1`
	if test -n "$TST"
		then
		if $USE --version 2>/dev/null >/dev/null
		then
			TST=`$USE --version 2>&1|fgrep ignored`
			test -n "$TST" || USE=gcc
		fi
		CC=$USE build-$PROG "$@"
	fi

	case $ME in
	irix*)
		CC64=cc-irix64
		;;
	sunos-5_7)
		CC64=cc-solaris64
		;;
	sunos-5_8)
		CC64=cc-solaris64
		;;
	sunos-5_9)
		CC64=cc-solaris64
		;;
	sunos-5_10)
		CC64=cc-solaris64
		;;
	*)
		CC64=none
		;;
	esac

	if test "$CC64" != none ; then
		CC=$CC64 build-$PROG "$@"
	fi

	if ( rpm --version 2>&1 >/dev/null )
	then
		HAVE_RPM=yes
		rpmbuild-$PROG
	else
		HAVE_RPM=no
		echo "** RPM not found"
	fi

	if ( dpkg --version 2>&1 >/dev/null )
	then
		HAVE_DEB=yes
		debbuild-$PROG
	else
		HAVE_DEB=no
		echo "** DPKG not found"
	fi

	# handle nonportable packages here, e.g., Solaris and *BSD's
	case `uname -s` in
	SunOS)
		HOST=solaris
		;;
	*)
		HOST=
		;;
	esac
	if test -n "$HOST"
	then
		mkpkg-$HOST-$PROG 2>/dev/null || mkpkg-$HOST
	fi

	if ( clang --version 2>/dev/null >/dev/null )
	then
		with-clang build-$PROG
	else
		echo "** CLANG not found"
	fi

	if ( cfg-mingw --version 2>/dev/null >/dev/null )
	then
		mingwbuild-$PROG && run-makensis */*.nsi || rmdir BUILD-W32
		test "$HAVE_RPM" = yes && rpmbuild-mingw-$PROG
		test "$HAVE_DEB" = yes && debbuild-mingw-$PROG
	else
		echo "** MINGW-32 not found"
	fi

	if ( cfg-mingw64 --version 2>/dev/null >/dev/null )
	then
		mingw64build-$PROG && run-makensis */*.nsi || rmdir BUILD-W64
		test "$HAVE_RPM" = yes && rpmbuild-mingw64-$PROG
		test "$HAVE_DEB" = yes && debbuild-mingw64-$PROG
	else
		echo "** MINGW-64 not found"
	fi

	MYCC=`which gcc 2>/dev/null`
	case $ME in
	*bsd*)
		for port in /usr/local/bin/gcc[1-9]*
		do
			PORT=`basename $port`
			if test -d $port && test -f $port/$PORT
			then
				PATH=$port:$PATH CC=$PORT build-$PROG
			elif test -f $port
			then
				MYCC=$port
			fi
		done
		;;
	esac

	case "x$MYCC" in
	x/*)
		if test -f "$MYCC"
		then
			ACTUAL_GCC=`basename $MYCC`
			export ACTUAL_GCC
		fi
		;;
	esac

	case $ME in
	stable-9|par-debian31)
		with-icc9 build-$PROG
		rename_icc9_logs
		with-icc build-$PROG
		;;
	testing-9|par-debian50-32|par-debian60-32)
		with-icc9 build-$PROG
		rename_icc9_logs
		;;
	par-fedora15-64|vmw-debian6-64)
		with-icc12 build-$PROG
		rename_icc12_logs
		;;
	esac

	case $ME in
	xen-solaris|par-solaris0906)
		with-suncc build-$PROG
		CC=gcc-stricter build-$PROG
		CC=gcc-normal build-$PROG
		CC=gcc-normal-64 build-$PROG
		;;
	vbx-*|vmw-*|xen-*|par-*|*-9|ss90|callisto|marshall|michener|cygwin64)
		CC=gcc-stricter build-$PROG
		CC=gcc-normal build-$PROG
		;;
	*)
		if with-gcc gcc --version 2>/dev/null ; then
			CC=gcc-normal with-gcc build-$PROG "$@"
			if test "$CC64" != none ; then
				CC=gcc-normal-64 with-gcc build-$PROG "$@"
			fi
		fi
		;;
	esac
	tput bel
}
