#!/bin/sh -
#	$Id: s_include,v 1.12 2001/07/30 22:31:07 bostic Exp $
#
# Build the automatically generated function prototype files.

msgc="/* DO NOT EDIT: automatically built by dist/s_include. */"

. ./RELEASE

cxx_if="#if defined(__cplusplus)"
cxx_head="extern \"C\" {"
cxx_foot="}"
cxx_endif="#endif"

e1=/tmp/__db_e1.$$
e2=/tmp/__db_e2.$$
i1=/tmp/__db_i1.$$
i2=/tmp/__db_i2.$$
trap 'rm -f $e1 $e2 $i1 $i2; exit 0' 0 1 2 3 13 15

(echo "" &&
    echo "$msgc" &&
    echo "$cxx_if" &&
    echo "$cxx_head" &&
    echo "$cxx_endif") > $e1
(echo "" &&
    echo "$msgc" &&
    echo "$cxx_if" &&
    echo "$cxx_head" &&
    echo "$cxx_endif") > $e2

# Extract externals and build include files.
for i in db btree clib common env hash \
    lock log mp mutex os qam rep rpc_client rpc_server tcl txn xa; do
	(echo "$msgc" &&
	    echo "#ifndef	_${i}_ext_h_" &&
	    echo "#define	_${i}_ext_h_" &&
	    echo "$cxx_if" &&
	    echo "$cxx_head" &&
	    echo "$cxx_endif") > $i1
	(echo "$msgc" &&
	    echo "#ifndef	_${i}_ext_h_" &&
	    echo "#define	_${i}_ext_h_" &&
	    echo "$cxx_if" &&
	    echo "$cxx_head" &&
	    echo "$cxx_endif") > $i2

	f="../$i/*.c"
	[ $i = os ] && f="$f ../os_win32/*.c"
	awk -f gen_inc.awk \
	    -v db_version_unique_name=$DB_VERSION_UNIQUE_NAME \
	    -v ext_file=$e1 -v uext_file=$e2 \
	    -v inc_file=$i1 -v uinc_file=$i2 $f

	(echo "$cxx_if" &&
	echo "$cxx_foot" &&
	echo "$cxx_endif" &&
	echo "#endif /* _${i}_ext_h_ */") >> $i1
	(echo "$cxx_if" &&
	echo "$cxx_foot" &&
	echo "$cxx_endif" &&
	echo "#endif /* _${i}_ext_h_ */") >> $i2

	f=../include_auto/${i}_ext.h
	cmp $i1 $f > /dev/null 2>&1 ||
	    (echo "Building $f" && rm -f $f && cp $i1 $f && chmod 444 $f)
	f=../include_auto/${i}_ext.in
	cmp $i2 $f > /dev/null 2>&1 ||
	    (echo "Building $f" && rm -f $f && cp $i2 $f && chmod 444 $f)
done

# Extract externals (no include files to build).
for i in dbm hsearch; do
	awk -f gen_inc.awk \
	    -v db_version_unique_name=$DB_VERSION_UNIQUE_NAME \
	    -v ext_file=$e1 -v uext_file=$e2 ../$i/*.c
done

# There are a few globals in DB -- fix them up as well.
(echo "#define __db_global_values __db_global_values@DB_VERSION_UNIQUE_NAME@" &&
    echo "#define __db_jump __db_jump@DB_VERSION_UNIQUE_NAME@" &&
    echo "#define db_xa_switch db_xa_switch@DB_VERSION_UNIQUE_NAME@") >> $e2

(echo "$cxx_if" &&
    echo "$cxx_foot" &&
    echo "$cxx_endif" &&
    echo ""
    echo "#endif /* !_DB_H_ */") >> $e1
(echo "$cxx_if" &&
    echo "$cxx_foot" &&
    echo "$cxx_endif" &&
    echo ""
    echo "#endif /* !_DB_H_ */") >> $e2

f=../include_auto/global_ext.in
cmp $e1 $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $e1 $f && chmod 444 $f)
f=../include_auto/global_uext.in
cmp $e2 $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $e2 $f && chmod 444 $f)

# DB185 compatibility support.
(echo "$msgc" &&
    echo "$cxx_if" &&
    echo "$cxx_head" &&
    echo "$cxx_endif") > $e1
(echo "$msgc" &&
    echo "$cxx_if" &&
    echo "$cxx_head" &&
    echo "$cxx_endif") > $e2

awk -f gen_inc.awk \
    -v db_version_unique_name=$DB_VERSION_UNIQUE_NAME \
    -v ext_file=$e1 -v uext_file=$e2 ../db185/*.c

(echo "$cxx_if" &&
    echo "$cxx_foot" &&
    echo "$cxx_endif" &&
    echo ""
    echo "#endif /* _DB_185_H_ or _DB185_INT_H_ */") >> $e1
(echo "$cxx_if" &&
    echo "$cxx_foot" &&
    echo "$cxx_endif" &&
    echo ""
    echo "#endif /* _DB_185_H_ or _DB185_INT_H_ */") >> $e2

f=../include_auto/db185_ext.in
cmp $e1 $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $e1 $f && chmod 444 $f)
f=../include_auto/db185_uext.in
cmp $e2 $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $e2 $f && chmod 444 $f)

# RPC uses rpcgen to generate a header file; post-process it.
f=../include_auto/db_server.in
sed -e '/extern bool_t xdr___/{' \
    -e 's/.* //' \
    -e 's/();//' \
    -e 's/.*/#define & &@DB_VERSION_UNIQUE_NAME@\
extern bool_t &();/' \
    -e '}' < ../include_auto/db_server.h > $i1
cmp $i1 $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $i1 $f && chmod 444 $f)
