#!/bin/sh

#configure - FireMake configuratin script
#Copyright (C) 2002 Ian Gulliver
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of version 2 of the GNU General Public License as
#published by the Free Software Foundation.
#
#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

VERSION="0.9.5"

ECHO=echo
if test -f /usr/ucb/echo; then
	ECHO=/usr/ucb/echo
fi

$ECHO "FireMake v$VERSION starting...."
$ECHO "FireMake v$VERSION starting...." > configure.log

$ECHO -n > firemake.h

$ECHO
$ECHO "	=== Enviroment Variables ==="

$ECHO -n "	checking for \$CC..."
if test "$CC" = ""; then
	JUNK=`which gcc 2>&1`
	if test "$?" = "0"; then
		$ECHO "not found, assuming \"gcc\""
		CC="gcc"
	else
		$ECHO "not found, assuming \"cc\""
		CC="cc"
	fi
else
	$ECHO "found: \"$CC\""
fi

$ECHO -n "	checking for \$RANLIB..."
if test "$RANLIB" = ""; then
	$ECHO "not found, assuming \"ranlib\""
	RANLIB="ranlib"
else
	$ECHO "found: \"$RANLIB\""
fi

$ECHO -n "	checking for \$CFLAGS..."
if test "$CFLAGS" = ""; then
	$ECHO "not found, assuming \"-O\""
	CFLAGS="-O"
else
	$ECHO "found: \"$CFLAGS\""
fi


$ECHO
$ECHO "	=== Package Variables ==="

$ECHO -n "	checking for firemake.libs..."
if test -f "firemake.libs"; then
	$ECHO -n "found: "
	INLIBS=`cat firemake.libs`
	if test "$?" != "0"; then
		$ECHO
		$ECHO "	ERROR: Unable to read firemake.libs"
		exit 1
	else
		$ECHO $INLIBS
	fi
else
	INLIBS=""
	$ECHO "not found"
fi

$ECHO -n "	checking for firemake.sharedlibs..."
if test -f "firemake.sharedlibs"; then
	$ECHO -n "found: "
	INSHAREDLIBS=`cat firemake.sharedlibs`
	if test "$?" != "0"; then
		$ECHO
		$ECHO "	ERROR: Unable to read firemake.sharedlibs"
		exit 1
	else
		$ECHO $INSHAREDLIBS
	fi
else
	INSHAREDLIBS=""
	$ECHO "not found"
fi

$ECHO -n "	checking for firemake.cflags..."
if test -f "firemake.cflags"; then
	$ECHO -n "found: "
	INCFLAGS=`cat firemake.cflags`
	if test "$?" != "0"; then
		$ECHO
		$ECHO "	ERROR: Unable to read firemake.cflags"
		exit 1
	else
		$ECHO $INCFLAGS
	fi
else
	INCFLAGS=""
	$ECHO "not found"
fi

$ECHO -n "	checking for firemake.ldflags..."
if test -f "firemake.ldflags"; then
	$ECHO -n "found: "
	INLDFLAGS=`cat firemake.ldflags`
	if test "$?" != "0"; then
		$ECHO
		$ECHO "	ERROR: Unable to read firemake.ldflags"
		exit 1
	else
		$ECHO $INLDFLAGS
	fi
else
	INLDFLAGS=""
	$ECHO "not found"
fi

$ECHO -n "	checking for firemake.sharedflags..."
if test -f "firemake.sharedflags"; then
	$ECHO -n "found: "
	INSHAREDFLAGS=`cat firemake.sharedflags`
	if test "$?" != "0"; then
		$ECHO
		$ECHO "	ERROR: Unable to read firemake.shareadflags"
		exit 1
	else
		$ECHO $INSHAREDFLAGS
	fi
else
	INSHAREDFLAGS=""
	$ECHO "not found"
fi



$ECHO
$ECHO "	=== Program Features ==="

$ECHO -n "	Checking for crypt() usage: "
$ECHO "RUN: grep crypt\( *.c" >> configure.log
JUNK=`grep crypt\( *.c 2>&1`

if test "$?" = "0"; then
	USES_CRYPT="yes"
else
	USES_CRYPT="no"
fi
$ECHO $USES_CRYPT

$ECHO -n "	Checking for socket() usage: "
$ECHO "RUN: grep socket\( *.c" >> configure.log
JUNK=`grep socket\( *.c 2>&1`

if test "$?" = "0"; then
	USES_SOCKET="yes"
else
	USES_SOCKET="no"
fi
$ECHO $USES_SOCKET

$ECHO -n "	Checking for inet_*() usage: "
$ECHO "RUN: grep inet_ *.c" >> configure.log
JUNK=`grep inet_ *.c 2>&1`

if test "$?" = "0"; then
	USES_RESOLVE="yes"
else
	USES_RESOLVE="no"
fi
$ECHO $USES_RESOLVE

if test "$USES_RESOLVE" = "no"; then
	$ECHO -n "	Checking for gethostby*() usage: "
	$ECHO "RUN: grep gethostby *.c" >> configure.log
	JUNK=`grep gethostby *.c 2>&1`

	if test "$?" = "0"; then
		USES_RESOLVE="yes"
	else
		USES_RESOLVE="no"
	fi
	$ECHO $USES_RESOLVE
fi

$ECHO -n "	Checking for sendfile() usage: "
$ECHO "RUN: grep sendfile\( *.c" >> configure.log
JUNK=`grep sendfile\( *.c 2>&1`

if test "$?" = "0"; then
	USES_SENDFILE="yes"
else
	USES_SENDFILE="no"
fi
$ECHO $USES_SENDFILE

$ECHO
$ECHO "	=== Operating Systems & Architectures ==="

$ECHO "RUN: uname -s" >> configure.log
SYSNAME=`uname -s 2>>configure.log`

if test "$?" != "0"; then
	$ECHO "ERROR: bad return value" >> configure.log
	$ECHO "	ERROR: Unable to retrieve operating system name (uname -s)"
	exit 1
fi

$ECHO -n "	checking for Linux..."
if test "$SYSNAME" = "Linux"; then
	$ECHO "compiling on Linux:"
	if test "$USES_CRYPT" = "yes"; then
		$ECHO "		\$LIBS add \"-lcrypt\""
		INLIBS="$INLIBS -lcrypt"
		$ECHO "		firemake.h add \"#include <crypt.h>\""
		$ECHO "#include <crypt.h>" >> firemake.h
	fi
else
	$ECHO "no"
fi

$ECHO -n "	checking for FreeBSD..."
if test "$SYSNAME" = "FreeBSD"; then
	$ECHO "compiling on FreeBSD:"
	if test "$USES_CRYPT" = "yes"; then
		$ECHO "		\$LIBS add \"-lcrypt\""
		INLIBS="$INLIBS -lcrypt"
	fi
else
	$ECHO "no"
fi

$ECHO -n "	checking for Darwin..."
if test "$SYSNAME" = "Darwin"; then
	$ECHO "compiling on Darwin:"
	$ECHO "		\$SHAREDFLAGS add \"-dynamiclib\""
	$ECHO "		\$SHAREDLIBS add \"-lcc_dynamic\""
	INSHAREDFLAGS="$INSHAREDFLAGS -dynamiclib"
	INSHAREDLIBS="$INSHAREDLIBS -lcc_dynamic"
else
	$ECHO "no"
fi

$ECHO -n "	checking for IRIX..."
if test "$SYSNAME" = "IRIX" -o "$SYSNAME" = "IRIX64"; then
	$ECHO "compiling on IRIX:"
	$ECHO "		\$RANLIB to \"no\""
	RANLIB="no"
else
	$ECHO "no"
fi

$ECHO -n "	checking for SunOS..."
if test "$SYSNAME" = "SunOS"; then
	$ECHO "compiling on SunOS:"
	if test "$USES_SOCKET" = "yes"; then
		$ECHO "		\$LIBS add \"-lnsl -lsocket\""
		INLIBS="$INLIBS -lnsl -lsocket"
	fi
	if test "$USES_RESOLVE" = "yes"; then
		$ECHO "		\$LIBS add \"-lresolv\""
		INLIBS="$INLIBS -lresolv"
	fi
	if test "$USES_SENDFILE" = "yes"; then
		$ECHO "		\$LIBS add \"-lsendfile\""
		INLIBS="$INLIBS -lsendfile"
		$ECHO "		firemake.h add \"#include <sys/sendfile.h>\""
		$ECHO "#include <sys/sendfile.h>" >> firemake.h
	fi
else
	$ECHO "no"
fi



$ECHO
$ECHO "	=== Construct Makefile ==="

$ECHO -n "	checking for old Makefile..."
if test -f Makefile; then
	$ECHO -n "found, removing..."
	$ECHO "RUN: rm -f Makefile" >> configure.log
	rm -f Makefile 2>>configure.log
	if test "$?" != "0"; then
		$ECHO "ERROR: bad return value" >> configure.log
		$ECHO
		$ECHO "	ERROR: Unable to remove old Makefile"
		exit 1;
	else
		$ECHO "done"
	fi
else
	$ECHO "not found"
fi

$ECHO -n "	writing top of Makefile..."
$ECHO "# This Makefile autogenerated by FireMake v$VERSION" >> Makefile
$ECHO >> Makefile
$ECHO "CC := $CC" >> Makefile
$ECHO "RANLIB ?= $RANLIB" >> Makefile
$ECHO "LIBS ?= " >> Makefile
$ECHO "SHAREDLIBS ?= " >> Makefile
$ECHO "CFLAGS ?= -O" >> Makefile
$ECHO "LDFLAGS ?= " >> Makefile
$ECHO "SHAREDFLAGS ?= -shared" >> Makefile
$ECHO "PREFIX ?= /usr/local" >> Makefile
$ECHO "BINDIR ?= \$(PREFIX)/bin" >> Makefile
$ECHO "LIBDIR ?= \$(PREFIX)/lib" >> Makefile
$ECHO "INCLUDEDIR ?= \$(PREFIX)/include" >> Makefile
$ECHO "MANDIR ?= \$(PREFIX)/man" >> Makefile
$ECHO "CONFDIR ?= \$(PREFIX)/etc" >> Makefile
$ECHO "INSTALL_GROUP ?= root" >> Makefile
$ECHO "INSTALL_USER ?= root" >> Makefile

$ECHO "LIBS := \$(LIBS) $INLIBS" >> Makefile
$ECHO "SHAREDLIBS := \$(SHAREDLIBS) $INSHAREDLIBS" >> Makefile
$ECHO "CFLAGS := \$(CFLAGS) $INCFLAGS" >> Makefile
$ECHO "LDFLAGS := \$(LDFLAGS) $INLDFLAGS" >> Makefile
$ECHO "SHAREDFLAGS := \$(SHAREDFLAGS) $INSHAREDFLAGS" >> Makefile

$ECHO >> Makefile
$ECHO ".PHONY: all clean distclean \
binaries static shared install install-binaries install-conf \
install-headers install-shared install-static \
install-man1 install-man2 install-man3 \
install-man4 install-man5 install-man6 \
install-man7 install-man8 install-man9" \
>> Makefile
$ECHO >> Makefile
$ECHO "all: binaries static shared" >> Makefile
$ECHO >> Makefile
$ECHO "install: install-binaries install-headers install-shared install-conf \
install-static install-man1 install-man2 install-man3 \
install-man4 install-man5 install-man6 install-man7 \
install-man8 install-man9" >> Makefile
$ECHO >> Makefile
$ECHO "distclean: clean" >> Makefile
$ECHO "	rm -f Makefile firemake.h install.sh" >> Makefile
$ECHO >> Makefile
$ECHO "done"

$ECHO "RUN: ls *.c" >> configure.log
LS=`ls *.c 2>>configure.log`
if test "$LS" = ""; then
	$ECHO "ERROR: bad return value" >> configure.log
	$ECHO "	ERROR: Unable to find any C files"
	exit 1
fi

for CFILE in `ls *.c`; do
	$ECHO "RUN: $ECHO \"$CFILE\" | sed s/.c\$/.o/" >> configure.log
	OBJECT=`$ECHO "$CFILE" | sed s/.c\$/.o/ 2>>configure.log`
	if test "$?" != "0"; then
		$ECHO "ERROR: bad return value" >> configure.log
		$ECHO "	ERROR: error finding C files; check configure.log"
	fi
	$ECHO -n "	writing rules to make $OBJECT from $CFILE..."
	$ECHO "RUN: $CC $CFLAGS $INCFLAGS -M $CFILE" >> configure.log
	$CC $CFLAGS $INCFLAGS -M $CFILE >> Makefile 2>>configure.log
	if test "$?" != "0"; then
		$ECHO "ERROR: bad return value" >> configure.log
		$ECHO
		$ECHO "	ERROR: error while generating dependencies; check configure.log"
		exit
	fi
	$ECHO "	\$(CC) \$(CFLAGS) -DCONFDIR=\\\"\$(CONFDIR)\\\" -DBINDIR=\\\"\$(BINDIR)\\\" -o $OBJECT -c $CFILE" >> Makefile
	$ECHO >> Makefile
	$ECHO "done"
done



BINARIES=""
$ECHO
$ECHO "	=== Make binaries ==="

if test -f "firemake.binaries"; then
	$ECHO "RUN: cat firemake.binaries | sed s/:.*//" >> configure.log
	for BINARY in `cat firemake.binaries | sed s/:.*// 2>>configure.log`; do
		BINARIES="$BINARIES $BINARY"
		$ECHO "RUN: grep \"^$BINARY: \" firemake.binaries | sed \"s/.*: //\"" >> configure.log
		OBJS=`grep "^$BINARY:" firemake.binaries | sed "s/.*: //" 2>>configure.log`;
		$ECHO -n "	adding rule to make $BINARY..."
		$ECHO "$BINARY: $OBJS" >> Makefile
		$ECHO "	\$(CC) \$(LDFLAGS) -o $BINARY $OBJS \$(LIBS)" >> Makefile
		$ECHO >> Makefile
		$ECHO "done"
	done;
else
	$ECHO "	no binaries to be built, skipping"
fi

$ECHO "binaries: $BINARIES" >> Makefile
$ECHO >> Makefile




SHARED=""
STATIC=""
$ECHO
$ECHO "	=== Make libraries ==="

if test -f "firemake.libraries"; then
	$ECHO "RUN: cat firemake.libraries | sed s/:.*//" >> configure.log
	for LIBRARY in `cat firemake.libraries | sed s/:.*// 2>>configure.log`; do
		SHARED="$SHARED $LIBRARY.so"
		STATIC="$STATIC $LIBRARY.a"
		$ECHO "RUN: grep \"^$LIBRARY:\" firemake.libraries | sed \"s/.*: //\"" >> configure.log
		OBJS=`grep "^$LIBRARY:" firemake.libraries | sed "s/.*: //" 2>>configure.log`;
		$ECHO -n "	adding rule to make $LIBRARY.so..."
		$ECHO "$LIBRARY.so: $OBJS" >> Makefile
		$ECHO "	\$(CC) \$(LDFLAGS) \$(SHAREDFLAGS) -o $LIBRARY.so $OBJS \$(SHAREDLIBS) \$(LIBS)" >> Makefile
		$ECHO >> Makefile
		$ECHO "done"
		$ECHO -n "	adding rule to make $LIBRARY.a..."
		$ECHO "$LIBRARY.a: $OBJS" >> Makefile
		$ECHO "	rm -f $LIBRARY.a" >> Makefile
		$ECHO "	ar cru $LIBRARY.a $OBJS" >> Makefile
		if test "$RANLIB" != "no"; then
			$ECHO "	\$(RANLIB) $LIBRARY.a" >> Makefile
		fi
		$ECHO >> Makefile
		$ECHO "done"
	done;
else
	$ECHO "	no libraries to be built, skipping"
fi

$ECHO "shared: $SHARED" >> Makefile
$ECHO >> Makefile

$ECHO "static: $STATIC" >> Makefile
$ECHO >> Makefile


$ECHO
$ECHO "	=== Make installation ==="

$ECHO -n "	adding rule to make clean..."
$ECHO "clean:" >> Makefile
$ECHO "	rm -f *.o *.so *.a $BINARIES $SHARED $STATIC" >> Makefile
$ECHO "done"

NOINSTALL=""
$ECHO -n "	reading noinstall rules..."
if test -f "firemake.noinstall"; then
	$ECHO "RUN: cat firemake.noinstall" >> configure.log
	for NO in `cat firemake.noinstall 2>>configure.log`; do
		NOINSTALL="$NOINSTALL $NO"
	done
	$ECHO "done"
	$ECHO -n "	pruning binaries based on noinstall..."
	OLDBINARIES=$BINARIES
	BINARIES=""
	for BINARY in $OLDBINARIES; do
		INSTALL="yes"
		for NO in $NOINSTALL; do
			if test "$NO" = "$BINARY"; then
				INSTALL="no"
			fi
		done
		if test "$INSTALL" = "yes"; then
			BINARIES="$BINARIES $BINARY"
		fi
	done
	$ECHO "done"
	$ECHO -n "	pruning static libraries based on noinstall..."
	OLDSTATIC=$STATIC
	STATIC=""
	for LIB in $OLDSTATIC; do
		INSTALL="yes"
		for NO in $NOINSTALL; do
			if test "$NO" = "$LIB"; then
				INSTALL="no"
			fi
		done
		if test "$INSTALL" = "yes"; then
			STATIC="$STATIC $LIB"
		fi
	done
	$ECHO "done"
	$ECHO -n "	pruning shared libraries based on noinstall..."
	OLDSHARED=$SHARED
	SHARED=""
	for LIB in $OLDSHARED; do
		INSTALL="yes"
		for NO in $NOINSTALL; do
			if test "$NO" = "$LIB"; then
				INSTALL="no"
			fi
		done
		if test "$INSTALL" = "yes"; then
			SHARED="$SHARED $LIB"
		fi
	done
	$ECHO "done"
else
	$ECHO "not found, skipping"
fi

$ECHO -n "	adding rule to install binaries..."
$ECHO "install-binaries: $BINARIES" >> Makefile
$ECHO "done"
for BINARY in $BINARIES; do
	$ECHO -n "	adding rule to install binary $BINARY..."
	$ECHO "	./install.sh $BINARY \$(BINDIR) \$(INSTALL_USER) \$(INSTALL_GROUP) 0755" >> Makefile
	$ECHO "done"
done;
$ECHO >> Makefile

$ECHO -n "	adding rule to install static libraries..."
$ECHO "install-static: $STATIC" >> Makefile
$ECHO "done"
for LIBRARY in $STATIC; do
	$ECHO -n "	adding rule to install static library $LIBRARY..."
	$ECHO "	./install.sh $LIBRARY \$(LIBDIR) \$(INSTALL_USER) \$(INSTALL_GROUP) 0755" >> Makefile
	$ECHO "done"
done;
$ECHO >> Makefile

$ECHO -n "	adding rule to install shared libraries..."
$ECHO "install-shared: $SHARED" >> Makefile
$ECHO "done"
for LIBRARY in $SHARED; do
	$ECHO -n "	adding rule to install shared library $LIBRARY..."
	$ECHO "	./install.sh $LIBRARY \$(LIBDIR) \$(INSTALL_USER) \$(INSTALL_GROUP) 0755" >> Makefile
	$ECHO "done"
done;
$ECHO >> Makefile

HEADERS=""
$ECHO -n "	adding rule to install headers..."
if test -f "firemake.headers"; then
	$ECHO "RUN: cat firemake.headers" >> configure.log
	for HEADER in `cat firemake.headers 2>>configure.log`; do
		HEADERS="$HEADERS $HEADER"
	done
	$ECHO "done"
else
	$ECHO "no firemake.headers found"
fi
$ECHO "install-headers: $HEADERS" >> Makefile
for HEADER in $HEADERS; do
	$ECHO -n "	adding rule to install header $HEADER..."
	$ECHO "	./install.sh $HEADER \$(INCLUDEDIR) \$(INSTALL_USER) \$(INSTALL_GROUP) 0644" >> Makefile
	$ECHO "done"
done;
$ECHO >> Makefile

for SECTION in 1 2 3 4 5 6 7 8 9; do
	$ECHO -n "	adding rule to install man$SECTION..."
	$ECHO -n "install-man$SECTION: " >> Makefile
	$ECHO "RUN: ls man/*.$SECTION" >> configure.log
	LS=`ls man/*.$SECTION 2>>configure.log`
	if test "$LS" != ""; then
		$ECHO $LS >> Makefile
		for MANPAGE in $LS; do
			$ECHO "	./install.sh $MANPAGE \$(MANDIR)/man$SECTION \$(INSTALL_USER) \$(INSTALL_GROUP) 0644" >> Makefile
		done;
		$ECHO "done"
	else
		$ECHO >> Makefile
		$ECHO "no manpages found"
	fi
	$ECHO >> Makefile
done;

CONFS=""
$ECHO -n "	adding rule to install conf files..."
$ECHO -n "install-conf: " >> Makefile
$ECHO "RUN: ls conf/*.conf" >> configure.log
LS=`ls conf/*.conf 2>>configure.log`
if test "$LS" != ""; then
	cd conf/
	$ECHO "RUN: ls *.conf" >> ../configure.log
	for CONF in `ls *.conf 2>>../configure.log`; do
		CONFS="$CONFS $CONF"
	done
	cd ..
	for CONF in $CONFS; do
		$ECHO -n "conf/$CONF " >> Makefile
	done
	$ECHO >> Makefile
	for CONF in $CONFS; do
		$ECHO "	if test ! -f \$(CONFDIR)/$CONF; then ./install.sh conf/$CONF \$(CONFDIR) \$(INSTALL_USER) \$(INSTALL_GROUP) 0644; fi" >> Makefile
	done
	$ECHO "done"
else
	$ECHO >> Makefile
	$ECHO "no conf files found"
fi
$ECHO >> Makefile

$ECHO

$ECHO "	=== Creating install.sh ==="
$ECHO -n "	building install script..."
# install.sh takes:
# <file> <directory> <user> <group> <mode>
$ECHO "#!/bin/sh" > install.sh
$ECHO "FILENAME=\"\$2/\`basename \$1\`\"" >> install.sh
$ECHO "mkdir -p \$2" >> install.sh
$ECHO "cp -f \$1 \$FILENAME" >> install.sh
$ECHO "chown \$3:\$4 \$FILENAME" >> install.sh
$ECHO "chmod \$5 \$FILENAME" >> install.sh
chmod 0755 install.sh
$ECHO "done"

$ECHO
$ECHO "Finished configuring. Now just run \"make\""
rm -f configure.log
