#!/bin/sh

###############################################################################
#
#    AntiRight
#    (c) 2004-2007 Jeffrey Bedard
#    antiright@gmail.com
# 
#    This file is part of AntiRight.
#
#     AntiRight 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; either version 2 of the License, or
#     (at your option) any later version.
#
#     AntiRight 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 AntiRight; if not, write to the Free Software
#     Foundation, Inc., 51 Franklin Street, Fifth Floor, 
#     Boston, MA  02110-1301  USA
#
###############################################################################

VERSION=3.1.1

USE_DISTCC=YES
USE_CCACHE=YES

for ARG in $@; do
	if [ "$ARG" = "-d" ]; then
		echo 'Configuring with debug options...'
		CFLAGS="$CFLAGS -g3 -Wall -W -DDEBUG"
	elif [ "$ARG" = "-l" ]; then
		PREFIX=/usr/local/packages/antiright
	elif [ "$ARG" = "-s" ]; then
	        PREFIX=~/opt/stow/antiright
	elif [ "$ARG" = "-S" ]; then
		PREFIX=/usr/local/stow/antiright
	elif [ "$ARG" = "--disable-ccache" ]; then
		USE_CCACHE=NO
	elif [ "$ARG" = "--disable-distcc" ]; then
		USE_DISTCC=NO
	fi
done

# For development branch
CFLAGS="$CFLAGS -Wall -W -W"
CFLAGS="$CFLAGS -Wwrite-strings "
CFLAGS="$CFLAGS -Wsign-compare -Wunreachable-code"
CFLAGS="$CFLAGS -Wmissing-noreturn -Wmissing-declarations"


DEFS="$DEFS -DPACKAGE_STRING='\"AntiRight_$VERSION\"'"


if [ "$PREFIX" = "" ]; then
	PREFIX=/usr/local
fi
DEFS="$DEFS -DPREFIX='\"$PREFIX\"'"

if [ "$MAKE" = "" ]; then
	MAKE="$(which make)"
fi
if [ "$MAKE" = "" ]; then
	MAKE="$(which gmake)"
fi
if [ "$MAKE" = "" ]; then
	echo Make not found in path, defaulting to /usr/bin/make.
	MAKE=/usr/bin/make
fi

if [ "$CC" = "" ]; then

	CC=cc

	if [ "$USE_DISTCC" = "YES" ]; then
		DISTCC="$(which distcc)"
		if [ "$DISTCC" != "" ]; then
			CC=$DISTCC	
		fi
	fi

	if [ "$USE_CCACHE" = "YES" ]; then
		CCACHE="$(which ccache)"

		if [ "$CCACHE" != "" ]; then
			CC="$CCACHE $CC"		
		fi
	fi
fi

INCLUDE="$INCLUDE -I/usr/pkg/include -I/usr/local/include -I/usr/X11R6/include"
LIBDIR="$LIBDIR -L/usr/pkg/lib -L/usr/local/lib -L/usr/X11R6/lib"

DEFS="$DEFS -D_GNU_SOURCE"

INSTALL=/usr/bin/install

echo Prefix is $PREFIX
echo "# Local Build Configuration" > config.mk
echo "VERSION=$VERSION" >> config.mk
echo "PREFIX=$PREFIX" >> config.mk
echo "INCLUDE=$INCLUDE" >> config.mk
echo "LIBDIR=$LIBDIR" >> config.mk
echo "DEFS=$DEFS" >> config.mk
echo "MAKE=$MAKE" >> config.mk
echo "INSTALL=$INSTALL" >> config.mk
echo "CFLAGS=$CFLAGS" >> config.mk
echo "CC=$CC" >> config.mk

