#
# configure script
#
CC=cc

compile='$CC -o conftest conftest.c >/dev/null 2>&1'
compiled_ok='test -s conftest && (./conftest) >/dev/null 2>/dev/null;'

DEFS=''
ANSI_OBJS=''
ANSI_SRCS=''
ENVIRON_H=./include/environ.h
ANSI_H=./include/ansi.h

# ------------------------------------------------------------
# This section generates ./include/environ.h
# ------------------------------------------------------------
echo Generating $ENVIRON_H
echo "/*
 * environ.h
 *
 * Generated by configure
 *
 */

#ifndef _TYPHOON_ENVIRON_H
#define _TYPHOON_ENVIRON_H

#define CREATMASK 0666
#define O_BINARY  0
#define DIR_SWITCH '/'
#ifndef offsetof
#define offsetof(s,m) ((int)&(((s *)0))->m)
#endif
#if defined(sparc) || defined(mips) || defined(HPUX) || defined(__alpha)
#define RISC 1
#endif
#define UNIX 1" > $ENVIRON_H
#
# Check endianess
#
echo Checking endianess
echo "main() 
{ 
short u=0;
*(char *)&u = 1;
exit(u); 
} 
" > conftest.c
eval $compile
if test -s conftest && (./conftest) >/dev/null 2>/dev/null ; then ENDIAN="BIG";
else ENDIAN="LITTLE"
fi
echo "#define "$ENDIAN"_ENDIAN 1" >> $ENVIRON_H
rm -f conftest*

#
# Check for prototypes
#
echo Checking for prototypes
echo " foo(int); 
foo(int a) { return ++a; } 
void main() { (void)foo(2); } " > conftest.c
eval $compile
if test -s conftest && (./conftest) >/dev/null 2>/dev/null ; then 
echo "
#define PRM(x)   ();
#define _PRM(x)  ()
#define ELLIPSIS /**/" >> $ENVIRON_H ;
else echo "
#define PROTOTYPES 1
#define PRM(x)   x
#define _PRM(x)  x
#define ELLIPSIS ,...

" >> $ENVIRON_H
fi
rm -f conftest*

#
# Check for uchar, ushort and ulong
#
for type in char short long 
do
	echo Checking for u$type
	echo "
#include <sys/types.h>
void main() { u$type a; } " > conftest.c
	eval $compile
	if test -s conftest && (./conftest) >/dev/null 2>/dev/null ; then :
	else echo "typedef unsigned $type u$type;" >> $ENVIRON_H
	fi
	rm -f conftest*
done

echo "
#endif
/* end-of-file */" >> $ENVIRON_H

# ------------------------------------------------------------
# This section generates ./include/ansi.h
# ------------------------------------------------------------
echo Generating $ANSI_H
echo "/*
 * ansi.h
 *
 * Generated by configure
 *
 */

#ifndef _TYPHOON_ANSI_H
#define _TYPHOON_ANSI_H
" > $ANSI_H

#
# Check ansi C functions
#
for func in strstr memmove
do
	echo Checking for $func
	echo "
#include <sys/types.h>
void main() {} foo() { $func(); } " > conftest.c
	eval $compile
	if test -s conftest && (./conftest) >/dev/null 2>/dev/null ; then :
	echo "#define `echo $func|tr '[a-z]' '[A-Z]'`_MISSING" >> $ANSI_H
	fi
	rm -f conftest*
done
echo "
#endif

/* end-of-file */" >> $ANSI_H


# ------------------------------------------------------------
# This section generates Makefile
# ------------------------------------------------------------
#
# Check for ranlib
#
echo Checking for ranlib
if test -z "$RANLIB" && type ranlib >/dev/null 2>/dev/null ; then
	RANLIB=ranlib
else
	RANLIB=true
fi

#
# Check for cc
#
echo Checking for cc
if test -z "$CC" && type cc >/dev/null 2>/dev/null ; then
	CC=cc
	CFLAGS="-g $(DEFINES)"
else
	CC=gcc
	CFLAGS="-g -W -Wunused -Wpointer-arith -Wswitch \$(DEFINES)"
fi

#
# Check for bison/yacc
#
echo Checking for bison/yacc
if test -z "$YACC" && type yacc >/dev/null 2>/dev/null ; then
	YACC=yacc
else
	YACC="bison -y"
fi

#
# Now generate Makefile from Makefile.in by substituting the @xxx@ names.
#

for dir in src util examples
do
echo Generating $dir/Makefile
echo "# Makefile generated by configure" > $dir/Makefile
cat $dir/Makefile.in | sed -e "
s/@defs@/$DEFS/
s/@ranlib@/$RANLIB/
s/@yacc@/$YACC/
s/@cc@/$CC/
s/@cflags@/$CFLAGS/
" >> $dir/Makefile ;
done


# echo "DEFS=$DEFS"
