#!/bin/bash

# General ./configure; make wrapper using Gentoo options

# by teknohog at iki.fi

for FILE in /etc/portage/make.conf /etc/make.conf; do
    if [ -f $FILE ]; then
	MAKECONF=$FILE
	break
    fi
done

for i in CFLAGS FEATURES MAKEOPTS; do
    # There must be a more elegant way to see if $i is set, but
    # this will do for now
    if [ -z "`set | grep $i=`" ]; then
	eval "`grep ^$i= $MAKECONF`"
    fi
done

export CFLAGS
export CXXFLAGS="$CFLAGS"

CCACHE=""
DISTCC=""
if [ -n "`echo $FEATURES | grep ccache`" ]; then CCACHE="ccache"; fi
if [ -n "`echo $FEATURES | grep distcc`" ]; then DISTCC="distcc"; fi

CC="$CCACHE $DISTCC $MACHTYPE-gcc"
CXX="$CCACHE $DISTCC $MACHTYPE-g++"

export CC
export CXX

./configure "$@"

make clean
make $MAKEOPTS CC="$CC" CXX="$CXX" CFLAGS="$CFLAGS" CXXFLAGS="$CFLAGS"
