#! /bin/bash
# $Id: configure 164 2007-07-25 19:21:00Z fwest $

### Default Values
# (change here or use configure option interface (the clean way))

prefix=/usr

# version
MAJOR_REL=0
MINOR_REL=1


# compiler flags

WARNINGS="-Wall -W -Wwrite-strings -Wsign-compare \
-Wpointer-arith -Wcast-qual -Wcast-align \
-Wstrict-prototypes -Wmissing-prototypes \
-Wnested-externs -Winline -Wshadow -Wformat=2"
 






### Stop Editing Here  (expect you know what you are doing)

# Developer Notes:
#
# o Take a look at the end of this file of this configure-script
#   for the script execution order
# 
# o If you add an test-case note the order (e.g. check_for_alloca)
#   1. STDOUT output
#   2. config.h output
#
# o Script improvements, hints or anything else is welcome!
#   send email to the netsend developers
#   (http://netsend.berlios.de/deve.html)


# internal values

c_red="\033[31,1m"
c_reset="\033[0m"

cc_flags=""

# function declarations

usage()
{
  cat << EOF
  Usage: configure [options]
  Options: [defaults in brackets after descriptions]
  Configuration:
    --help                  print this message
    --prefix=PREFIX         install architecture-independent files in PREFIX (/usr)
    --enable-debug          enable debug information (#define DEBUG and add CFLAGS -g)
    --verbose               enable verbose configure output (hacker-mode[tm])
EOF
}

parse_args()
{
	ac_prev=

	for ac_option
	do
		if test -n "$ac_prev"; then
			eval "$ac_prev=\$ac_option"
			ac_prev=
			continue
		fi

		case "$ac_option" in
			-*=*) ac_optarg=`echo "$ac_option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
			*) ac_optarg= ;;
		esac

		case "$ac_option" in

			--enable-debug | -enable-debug)
			debug="yes"
			;;

			-help | --help | -h)
			usage;
			exit 0;
			;;

			-prefix | --prefix | --prefi | --pref | --pre | --pr | --p)
			ac_prev=prefix
			;;
			-prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*)
			prefix="$ac_optarg"
			;;

			-*) { echo "configure: error: $ac_option: invalid option; use --help to show usage" 1>&2; exit 1; }
			;;

			*)
			;;

		esac
	done
}

print_config_header()
{
	# stdout stuff
	echo "*** configure-ng ***"

	# Make.Rules stuff
	echo -e "# Make.Rules.  Generated by configure script\n"  > Make.Rules

	# config.h stuff
	echo -n "/* Generated config.h see configure script for"  > config.h
	echo    " further information */"                        >> config.h
}

create_make_rules()
{
	cflags_save="$CFLAGS"

	echo "prefix =  $prefix"             >> Make.Rules
	echo "exec_prefix = \${prefix}"      >> Make.Rules
	echo                                 >> Make.Rules
	echo  "bindir = \${exec_prefix}/bin" >> Make.Rules

	# version information
	echo "MAJOR_REL=$MAJOR_REL"            >> Make.Rules
	echo "MINOR_REL=$MINOR_REL"            >> Make.Rules
	echo                                   >> Make.Rules
	
	# compiler flags
	if [ "$debug" = yes ] ; then
		echo "XCFLAGS=-g -O0 -DDEBUG"          >> Make.Rules
		cc_flags="-g -O0 -DDEBUG"
	else
		echo "XCFLAGS=$cflags_save"            >> Make.Rules
		cc_flags="$cflags_save"
	fi
	echo "WARNINGS = $WARNINGS"              >> Make.Rules
	echo "CFLAGS = \$(WARNINGS) \$(XCFLAGS)" >> Make.Rules

	cc_flags="$WARNINGS $cc_flags"
}



check_for_alloca()
{
	echo -n "checking for alloca..."
	TMPDIR=`mktemp -d`
	cat > "$TMPDIR"/alloca.c <<EOF
#include <alloca.h>
int main(int argc, char **argv){
alloca(23);
return 0;
}
EOF
	gcc -o /dev/null "$TMPDIR"/alloca.c >/dev/null 2>&1
	if [ $? -eq 0 ];then
		echo " yes"
		echo "#define HAVE_ALLOCA 1" >>config.h
	else
		echo " no"
		echo "#undef HAVE_ALLOCA" >>config.h

	fi
	rm -f "$TMPDIR"/alloca.c
	rmdir "$TMPDIR"
}

check_for_rdtscll()
{
	echo -n "checking for rdtscll..."
	TMPDIR=`mktemp -d`
	cat > "$TMPDIR"/rdtscll.c <<EOF
#define rdtscll(val) \
     __asm__ __volatile__("rdtsc" : "=A" (val))

int main(int argc, char **argv){
long long cnt;
rdtscll(cnt);
return 0;
}
EOF
	gcc -o /dev/null "$TMPDIR"/rdtscll.c >/dev/null 2>&1
	if [ $? -eq 0 ];then
		echo " yes"
		echo "/* uncomment if you know TSC to be stable on this system */" >>config.h
		echo "/* #define HAVE_RDTSCLL 1 */" >>config.h
	else
		echo " no"
		echo "#undef HAVE_RDTSCLL" >>config.h

	fi
	rm -f "$TMPDIR"/rdtscll.c
	rmdir "$TMPDIR"
}


check_for_af_tipc()
{
	echo -n "checking for TIPC support..."
	TMPDIR=`mktemp -d`
	cat > "$TMPDIR"/tipc.c <<EOF
#include <stdio.h>
#include <linux/tipc.h>
int main(int argc, char **argv){
struct sockaddr_tipc s;
int af = AF_TIPC;
return 0;
}
EOF
	gcc -o /dev/null "$TMPDIR"/tipc.c >/dev/null 2>&1
	if [ $? -eq 0 ];then
		echo " yes"
		echo "#define HAVE_AF_TIPC 1" >>config.h
	else
		echo " no"
		echo "#undef HAVE_AF_TIPC" >>config.h

	fi
	rm -f "$TMPDIR"/tipc.c
	rmdir "$TMPDIR"
}


check_for_splice()
{
	echo -n "checking for splice..."
	TMPDIR=`mktemp -d`
	cat > "$TMPDIR"/splice.c <<EOF
#define _GNU_SOURCE
#include <fcntl.h>
#include <stddef.h>
int main(void) {
	return splice(0, NULL, 1, NULL, 42, SPLICE_F_MOVE|SPLICE_F_MORE);
}
EOF
	gcc -o /dev/null "$TMPDIR"/splice.c >/dev/null 2>&1
	if [ $? -eq 0 ];then
		echo " yes"
		echo "#define HAVE_SPLICE 1" >>config.h
	else
		echo " no"
		echo "#undef HAVE_SPLICE" >>config.h

	fi
	rm -f "$TMPDIR"/splice.c
	rmdir "$TMPDIR"
}





print_config()
{
	echo
	echo -e "\nConfigure picked following defaults, options and paths:"
	echo -e "Please validate this values (at least it isn't my box ;-)"
	echo
	echo   "  Prefix:         $prefix"
	if [ "$debug" = yes ] ; then
		echo "  Debug:          yes"
	else
		echo "  Debug:          no"
	fi
	echo   "  Compiler Flags: $cc_flags"
	echo
}


parse_args $*
print_config_header
create_make_rules

check_for_alloca
check_for_rdtscll
check_for_splice
check_for_af_tipc


print_config


# vim:set ts=2 sw=2 tw=78 noet:
