#! /bin/sh

# Copyright (C) 2004,2005,2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
#  
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#  
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#  
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

set -e

BDIR=Build-$(hostname)
J=-j8

confflags="-C --enable-maintainer-mode --prefix=/usr --sysconfdir=/etc --localstatedir=/var $CONFIGURE_FLAGS"
configure="`pwd`/configure $confflags"
make=eval\ "${CLEAN:+make $J silent.clean;} \
	    echo -e \"== executing 'make all'...\" && \
	    make $J ${SILENT:+-s silent.}all && \
	    echo -e \"\n== executing 'make check'...\" && \
	    make $J ${SILENT:+-s silent.}check"

LANG=C

case $(arch) in
  (x86_64)	ARCH_CFLAGS=' -m64';;
  (i?86)	ARCH_CFLAGS=' -m32';;
esac

CPPFLAGS=-D_FORTIFY_SOURCE=99$ARCH_CFLAGS

## Usage: xtermTitle <title>
function xtermTitle
{
    tty -s || return 0
    echo -ne "\e]0;$@\007"
}

## Usage: operate <dir> <configure-opts>*
function operate()
{
    d=$1
    shift
    
    echo  "******************"
    echo $"** Operating in directory '$d'..."
    xtermTitle "Operating in directory '$d'... ($@)"
    
    mkdir -p $d && cd $d

    test -z "$CLEAN"                         || rm -f config.cache
    test -e Makefile -a -z "$CONFIGURE_FLAGS"|| CCACHE_DISABLE=1 $configure "$@"
    $make
    cd -
}

case "$1" in
    dist)
	test -e Makefile || \
	    env \
	    CFLAGS="-O0 -g3$ARCH_CFLAGS"   \
	    CXXFLAGS="-O0 -g3$ARCH_CFLAGS" \
	    CPPFLAGS="$CPPFLAGS" \
	    ./configure $confflags
	$make
	exit
	;;
    debian-woody|debian-sarge|fc-1-90|fc-1|fc-2|fc-3)
	cat util-vserver-"$2".tar.bz2 | \
	    ssh $1 "cd /tmp && rm -rf /tmp/util-vserver-* && \
		    tar xjf - && cd util-vserver-$2 && \
		    export PATH=/usr/lib/ccache:/usr/lib/ccache/bin:\$PATH && \
			./configure && \
		    make ${SILENT:+-s silent.}all && \
		    echo -e '\n\n\n' &&
		    make ${SILENT:+-s silent.}check"
	exit
	;;
    *)  test -z "$1" || {
	    echo $"Unknown option '$1'" >&2
	    exit 1
	}
esac

test -d "$BDIR" || {
    d=$(mktemp -t -d build-ensc.XXXXXX)
    rm -f $BDIR
    ln -sf $d $BDIR
}

noopt_flags=-O0\ -g3
opt_flags=-O2\ -g
ldflags=$ARCH_CFLAGS

operate $BDIR/diet-noopt                                       CPPFLAGS="$CPPFLAGS" CFLAGS="$noopt_flags" CXXFLAGS="$noopt_flags" LDFLAGS="$ldflags"
operate $BDIR/diet-opt     --enable-release                    CPPFLAGS="$CPPFLAGS" CFLAGS="$opt_flags"   CXXFLAGS="$opt_flags"   LDFLAGS="$ldflags"
operate $BDIR/nodiet-noopt --disable-dietlibc                  CPPFLAGS="$CPPFLAGS" CFLAGS="$noopt_flags" CXXFLAGS="$noopt_flags" LDFLAGS="$ldflags"
operate $BDIR/nodiet-opt   --enable-release --disable-dietlibc CPPFLAGS="$CPPFLAGS" CFLAGS="$opt_flags"   CXXFLAGS="$opt_flags"   LDFLAGS="$ldflags"
#operate $BDIR/gcc32-opt    --enable-release --disable-dietlibc CPPFLAGS="$CPPFLAGS" CC='ccache gcc32'  CXX='ccache g++' CFLAGS="$opt_flags"   CXXFLAGS="$opt_flags" LDFLAGS="$ldflags"

operate $BDIR/diet-opt-api   --enable-release --enable-apis=NOLEGACY                    CPPFLAGS="$CPPFLAGS" CFLAGS="$opt_flags"   CXXFLAGS="$opt_flags" LDFLAGS="$ldflags"
operate $BDIR/nodiet-opt-api --enable-release --enable-apis=NOLEGACY --disable-dietlibc CPPFLAGS="$CPPFLAGS" CFLAGS="$opt_flags"   CXXFLAGS="$opt_flags" LDFLAGS="$ldflags"
