#!/bin/sh

#
# configure script
# $Id: configure,v 1.1 2001/02/12 17:54:23 mammon_ Exp $
#

#
# Check for cc
#

echo Checking for gcc

#
# typhoon uses a mixture of POSIX, BSD and System V interfaces
#

STANDARDS="-D_POSIX_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE"

if type gcc >/dev/null 2>/dev/null ; then
	CC=gcc
	CARGS="-pedantic $STANDARDS"
	CFLAGS="-g -Wall -pedantic -Wstrict-prototypes -Wmissing-prototypes $STANDARDS \$(DEFINES)"
else
	CC=cc
	CARGS="$STANDARDS"
	CFLAGS="-g $STANDARDS \$(DEFINES)"
fi

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

DEFS=''	# additional -D compiler defines go here
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

#include <sys/types.h>
#include <stddef.h>
#define CONFIG_CREATMASK 0666
#define CONFIG_O_BINARY  0
#define CONFIG_DIR_SWITCH '/'
#ifndef offsetof
#define offsetof(s,m) ((int)&(((s *)0))->m)
#endif
#if defined(sparc) || defined(mips) || defined(HPUX) || defined(__alpha)
#define CONFIG_RISC 1
#endif
#define CONFIG_UNIX 1" > $ENVIRON_H
#
# Check endianess
#
echo Checking endianess
echo "main() 
{ 
short u=0;
*(char *)&u = 1;
return u;
} 
" > conftest.c
eval $compile
if test -s conftest && (./conftest) >/dev/null 2>/dev/null ; then ENDIAN="BIG";
else ENDIAN="LITTLE"
fi
echo "#define CONFIG_${ENDIAN}_ENDIAN 1" >> $ENVIRON_H

#
# Check for prototypes
#
echo Checking for prototypes

rm -f conftest*
echo "int foo(int); 
int foo(int a) { return ++a; } 
int main(void) { (void)foo(2); return 0; } " > conftest.c
eval $compile
if ! test -s conftest || ! (./conftest) >/dev/null 2>/dev/null ; then 
echo "#define PRM(x)   ();
#define CONFIG_ELLIPSIS /**/" >> $ENVIRON_H ;
else echo "#define CONFIG_PROTOTYPES 1
#define PRM(x)   x
#define CONFIG_ELLIPSIS ,..." >> $ENVIRON_H
fi

# 
# Check for ANSI C const keyword
#

echo Checking for const keyword

rm -f conftest*
cat << END > conftest.c
int main()
{
    const int x = 24;
    return 0;
}
END

eval $compile
if test -s conftest && (./conftest) >/dev/null 2>/dev/null ; then
    echo "#define CONFIG_CONST const" >> $ENVIRON_H
else
    echo "#define CONFIG_CONST /**/" >> $ENVIRON_H
fi

#
# Check for size_t type
#

echo Checking for size_t type

rm -f conftest*
cat << END > conftest.c
#include <stddef.h>

int main()
{
    size_t x = 42;
    return 0;
}
END

eval $compile
if ! test -s conftest || ! (./conftest) >/dev/null 2>/dev/null ; then
    echo "typedef unsigned int size_t;" >> $ENVIRON_H
fi

#
# Check for off_t type
#

echo Checking for off_t type

rm -f conftest*
cat << END > conftest.c
#include <sys/types.h>
#include <unistd.h>

int main()
{
    off_t x = 42;
    return 0;
}
END

eval $compile
if ! test -s conftest || ! (./conftest) >/dev/null 2>/dev/null ; then
    echo "typedef unsigned long off_t;" >> $ENVIRON_H
fi

#
# Check for mode_t type
#

echo Checking for mode_t type

rm -f conftest*
cat << END > conftest.c
#include <sys/types.h>
#include <sys/stat.h>

int main()
{
    mode_t x = 42;
    return 0;
}
END

eval $compile
if ! test -s conftest || ! (./conftest) >/dev/null 2>/dev/null ; then
    echo "typedef unsigned mode_t;" >> $ENVIRON_H
fi


#
# Check for uchar, ushort and ulong
#

for type in char short long 
do
	echo Checking for u$type
	rm -f conftest*
	echo "#include <sys/types.h>
int main() { u$type a; return 0; } " > 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
done

#
# Check for locking via fcntl().
#


echo Checking for POSIX 'fcntl()' with F_SETLK and F_GETLK

rm -f conftest*
cat << END > conftest.c
#include <unistd.h>
#include <fcntl.h>

int main()
{
    struct flock flk;
    int fd = creat("conftest.data", O_RDWR);
    char testdata[] = "aaaaaaaaaaaaaaaa";

    if (fd == -1)
	return 1;

    if (write(fd, testdata, sizeof testdata) != sizeof testdata)
	return 1;

    flk.l_type = F_WRLCK;
    flk.l_whence = SEEK_SET;
    flk.l_start = 0;
    flk.l_len = 16;

    if (fcntl(fd, F_SETLK, &flk) == -1)
	return 1;

    return 0;
}
END

eval $compile
if test -s conftest || ! (./conftest) >/dev/null 2>/dev/null ; then
    echo "#define CONFIG_USE_FLOCK 1" >> $ENVIRON_H
fi

rm -f conftest*

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
	rm -f conftest*
	echo "#include <sys/types.h>
int main() {} foo() { $func(); return 0; } " > conftest.c
	eval $compile
	if test -s conftest && (./conftest) >/dev/null 2>/dev/null ; then :
	echo "#define `echo CONFIG_${func}|tr '[a-z]' '[A-Z]'`_MISSING" >> $ANSI_H
	fi
done

rm -f conftest*
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 bison/yacc, giving preference to bison
#
echo Checking for bison/yacc
if test -z "$YACC" && type bison >/dev/null 2>/dev/null ; then
	YACC="bison --yacc"
else
	YACC=yacc
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
