#!/bin/sh -
#	$Id: s_brew_posix,v 12.2 2006/11/09 14:11:49 bostic Exp $
#
# Build the BREW files on a POSIX system.

s=/tmp/__db_a
t=/tmp/__db_b

trap 'rm -f $s $t ; exit 0' 0
trap 'rm -f $s $t ; exit 1' 1 2 3 13 15

# Temporary directory to build in.
test -d ../build_brew_x || mkdir ../build_brew_x

# Copy in db_config.h.
#
# We do have system include files in the POSIX build.
f=../build_brew_x/db_config.h
cat <<ENDOFSEDTEXT > $s
/HAVE_SYSTEM_INCLUDE_FILES/{
	a\\
#define HAVE_SYSTEM_INCLUDE_FILES 1
}
ENDOFSEDTEXT
sed -f $s < ../build_brew/db_config.h > $t
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

# Copy in db.h.
f=../build_brew_x/db.h
sed -e '/typedef.*[	 ]FILE;/s/.*/#define IFile FILE/' \
    -e '/typedef.*[	 ]off_t;/d' \
    -e '/typedef.*[	 ]time_t;/d' \
    -e '/typedef.*[	 ]uintptr_t;/d' \
    -e '/#include.<AEEFile.h>/d' \
    < ../build_brew/db.h > $t

cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

# Copy in brew_db.h
f=../build_brew_x/brew_db.h
cmp brew/brew_posix.h $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp brew/brew_posix.h $f && chmod 444 $f)

# Copy in clib_port.h
#
# Delete all references to isalpha, isdigit, isprint, isspace.
f=../build_brew_x/clib_port.h
sed -e '/HAVE_ISALPHA/,/#endif/d' \
    -e '/HAVE_ISDIGIT/,/#endif/d' \
    -e '/HAVE_ISPRINT/,/#endif/d' \
    -e '/HAVE_ISSPACE/,/#endif/d' < ../build_brew/clib_port.h > $t
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

# Copy in remaining files.
for i in db_int.h errno.h; do
	f=../build_brew_x/$i
	cmp ../build_brew/$i $f > /dev/null 2>&1 ||
	    (echo "Building $f" &&
	    rm -f $f && cp ../build_brew/$i $f && chmod 444 $f)
done

# Build a Makefile for testing on a POSIX system.
# $1 is srcfiles keyword
# $2 is Makefile name
build_make()
{
	f=../build_brew_x/$2

	(cat brew/brew_make.in &&
	    echo &&
	    echo '###################################################' &&
	    echo '# EVERYTHING BELOW THIS LINE IS GENERATED BY s_brew' &&
	    echo '##################################################' &&
	    echo 'OBJS=\' &&
	    grep -w $1 srcfiles.in |
	    awk '{print $1}' |
	    sed -e '/isalpha/d' \
		-e '/isdigit/d' \
		-e '/isprint/d' \
		-e '/isspace/d' \
		-e 's/.*\//	/' \
		-e 's/\.c$/.o/' \
		-e '$!s/$/\\/' &&
	    echo &&
	    grep -w $1 srcfiles.in |
	    awk '{print $1}' |
	    sed -e '/isalpha/d' \
		-e '/isdigit/d' \
		-e '/isprint/d' \
		-e '/isspace/d' \
		-e 's/\.c$//' \
		-e 's/.*/&.o: ..\/&.c/' \
		-e 's/^[^\/]*\///' &&
	    echo &&
	    echo 'libdb.a: $(OBJS)' &&
	    echo '	ar cr $@ $(OBJS)') > $t

	cmp $t $f > /dev/null 2>&1 ||
	    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)
}

build_make brew Makefile

exit 0
