#!/bin/sh -
#	@(#)distrib	10.25 (Sleepycat) 5/2/98

# Build autoconf structure.
echo "Running autoheader"
autoheader 2>&1 | sed '/warning: AC_TRY_RUN called without default/d'
chmod 444 config.h.in
echo "Running autoconf"
autoconf 2>&1 | sed '/warning: AC_TRY_RUN called without default/d'
chmod 555 configure config.guess config.sub install-sh

msgc="/* DO NOT EDIT: automatically built by dist/distrib. */"
msgsh="# DO NOT EDIT: automatically built by dist/distrib."
msgcxx="// DO NOT EDIT: automatically built by dist/distrib."

# Build the automatically generated logging/recovery files.
for i in db btree hash log txn; do
	sh ./db_gen.sh ../$i/$i.src ../$i
done

# Build the automatically generated function prototype files.
for i in db btree clib common hash lock log mp mutex os txn; do
	f=../include/${i}_ext.h
	echo "Building $f"
	(echo "$msgc" &&
	    echo "#ifndef _${i}_ext_h_" &&
	    echo "#define _${i}_ext_h_" &&
	    sed -n "s/^ \* PUBLIC: \(.*\)/\1/p" ../$i/*.c &&
	    echo "#endif /* _${i}_ext_h_ */") > $f
	chmod 444 $f
done

# Build the Win32 files.
f=../build.win32/db.h
echo "Building $f"
(echo "$msgc" && sed \
    -e 's/@u_int8_decl@/typedef unsigned char u_int8_t;/' \
    -e 's/@int16_decl@/typedef short int16_t;/' \
    -e 's/@u_int16_decl@/typedef unsigned short u_int16_t;/' \
    -e 's/@int32_decl@/typedef int int32_t;/' \
    -e 's/@u_int32_decl@/typedef unsigned int u_int32_t;/' \
    -e '/@u_char_decl@/{' \
    -e		'i\' \
    -e		'#if !defined(_WINSOCKAPI_)' \
    -e		's/@u_char_decl@/typedef unsigned char u_char;/' \
    -e '}' \
    -e 's/@u_short_decl@/typedef unsigned short u_short;/' \
    -e 's/@u_int_decl@/typedef unsigned int u_int;/' \
    -e '/@u_long_decl@/{' \
    -e		's/@u_long_decl@/typedef unsigned long u_long;/' \
    -e	 	'a\' \
    -e		'#endif' \
    -e '}' \
    < ../include/db.h.src) > $f
chmod 444 $f

f=../build.win32/db_int.h
echo "Building $f"
(echo "$msgc" && sed \
    -e 's/\(PATH_SEPARATOR[^"]*"\)\/"/\1\\\\\/"/' \
    -e 's/@spin_line1@/typedef unsigned int tsl_t;/' \
    -e '/@spin_line2@/d' \
    -e '/@spin_line3@/d' \
    -e 's/@mutex_align@/sizeof(unsigned int)/' \
    < ../include/db_int.h.src) > $f
chmod 444 $f

f=../build.win32/libdb.rc
echo "Building $f"
major=`egrep DB_VERSION_MAJOR ../include/db.h.src | awk '{print $3}'`
minor=`egrep DB_VERSION_MINOR ../include/db.h.src | awk '{print $3}'`
patch=`egrep DB_VERSION_PATCH ../include/db.h.src | awk '{print $3}'`
sed -e "s/%MAJOR%/$major/" \
    -e "s/%MINOR%/$minor/" \
    -e "s/%PATCH%/$patch/" \
    < ../build.win32/libdb.rc.src > $f
chmod 444 $f

f=../build.win32/.dbtestrc
echo "Building $f"
(echo "$msgsh" && \
 cat ../test/test.tcl) > $f
chmod 444 $f

# Build the Win16 files.
f=../build.win16/db.h
echo "Building $f"
(echo "$msgc" && sed \
    -e 's/@u_int8_decl@/typedef unsigned char u_int8_t;/' \
    -e 's/@int16_decl@/typedef short int16_t;/' \
    -e 's/@u_int16_decl@/typedef unsigned short u_int16_t;/' \
    -e 's/@int32_decl@/typedef long int32_t;/' \
    -e 's/@u_int32_decl@/typedef unsigned long u_int32_t;/' \
    -e '/@u_char_decl@/{' \
    -e		'i\' \
    -e		'#if !defined(_WINSOCKAPI_)' \
    -e		's/@u_char_decl@/typedef unsigned char u_char;/' \
    -e '}' \
    -e 's/@u_short_decl@/typedef unsigned short u_short;/' \
    -e 's/@u_int_decl@/typedef unsigned int u_int;/' \
    -e '/@u_long_decl@/{' \
    -e		's/@u_long_decl@/typedef unsigned long u_long;/' \
    -e	 	'a\' \
    -e		'#endif' \
    -e '}' \
    -e 's/(\*\(db_errcall\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(tx_recover\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(db_malloc\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(bt_compare\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(bt_prefix\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(h_hash\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(close\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(cursor\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(del\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(fd\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(get\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(put\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(stat\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(sync\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(c_close\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(c_del\))/(DB_CALLBACK *\1)/' \
    -e 's/(\*\(c_get\))/(__cdecl __loadds *\1)/' \
    -e 's/(\*\(c_put\))/(__cdecl __loadds *\1)/' \
    < ../include/db.h.src) > $f
chmod 444 $f

f=../build.win16/db_int.h
echo "Building $f"
(echo "$msgc" && sed \
    -e 's/\(PATH_SEPARATOR[^"]*"\)\/"/\1\\\\\/"/' \
    -e 's/@spin_line1@/typedef unsigned int tsl_t;/' \
    -e '/@spin_line2@/d' \
    -e '/@spin_line3@/d' \
    -e 's/@mutex_align@/sizeof(unsigned int)/' \
    -e 's/off_t/u_int32_t/' \
    < ../include/db_int.h.src) > $f
chmod 444 $f

# Build the MacOS files.
#f=../build.macos/db.h
#echo "Building $f"
#(echo "$msgc" && sed \
#    -e 's/@u_int8_decl@/typedef unsigned char u_int8_t;/' \
#    -e 's/@int16_decl@/typedef short int16_t;/' \
#    -e 's/@u_int16_decl@/typedef unsigned short u_int16_t;/' \
#    -e 's/@int32_decl@/typedef int int32_t;/' \
#    -e 's/@u_int32_decl@/typedef unsigned int u_int32_t;/' \
#    -e '/@u_char_decl@/d' \
#    -e '/@u_short_decl@/d' \
#    -e '/@u_int_decl@/d' \
#    -e '/@u_long_decl@/d' \
#    < ../include/db.h.src) > $f
#chmod 444 $f

#f=../build.macos/db_int.h
#echo "Building $f"
#(echo "$msgc" && sed \
#    -e 's/^#include."db.h".*/#include "::build.macintosh:db.h"/' \
#    -e 's/\(PATH_SEPARATOR[^"]*"\)\/"/\1:"/' \
#    -e 's/@spin_line1@/typedef unsigned char tsl_t;/' \
#    -e '/@spin_line2@/d' \
#    -e 's/@mutex_align@/sizeof(unsigned char)/' \
#    < ../include/db_int.h.src) > $f
#chmod 444 $f

# Build the Java files.
f=../java/src/com/sleepycat/db/DbConstants.java
echo "Building $f"
(echo "$msgcxx" && \
 echo &&
 echo 'package com.sleepycat.db;' &&
 echo &&
 echo 'public class DbConstants' &&
 echo '{' &&
 grep '#define.DB_' ../include/db.h.src | \
 sed -e '/"/d' -e 's/[()]//g' | \
 awk '{ print "\tpublic static final int " $2 " = " $3 ";" }' && \
 echo '}') > $f
chmod 444 $f

# Build the Tcl test files.
f=../test/include.tcl.src
echo "Building $f"
rm -f $f
(echo "$msgsh"				&& \
 echo 'global dict'			&& \
 echo 'global testdir'			&& \
 echo ''				&& \
 echo 'set CP @db_cv_path_cp@'		&& \
 echo 'set DIFF @db_cv_path_diff@'	&& \
 echo 'set KILL @db_cv_path_kill@'	&& \
 echo 'set LS @db_cv_path_ls@'		&& \
 echo 'set MKDIR @db_cv_path_mkdir@'	&& \
 echo 'set MV @db_cv_path_mv@'		&& \
 echo 'set RM @db_cv_path_rm@'		&& \
 echo 'set SED @db_cv_path_sed@'	&& \
 echo 'set SLEEP @db_cv_path_sleep@'	&& \
 echo 'set SORT @db_cv_path_sort@'	&& \
 echo 'set TR @db_cv_path_tr@'		&& \
 echo ''				&& \
 echo 'set DB_LOCK_GET 0'		&& \
 echo 'set DB_LOCK_PUT 1'		&& \
 echo 'set DB_LOCK_PUT_ALL 2'		&& \
 echo 'set DB_LOCK_PUT_OBJ 3'		&& \
 echo 'set DB_LOCK_NG 0'		&& \
 echo 'set DB_LOCK_READ 1'		&& \
 echo 'set DB_LOCK_WRITE 2'		&& \
 echo 'set DB_LOCK_IREAD 3'		&& \
 echo 'set DB_LOCK_IWRITE 4'		&& \
 echo 'set DB_LOCK_IWR 5'		&& \
 grep '#define.DB_' ../include/db.h.src | sed -e '/"/d' -e 's/[()]//g' | \
     awk '{ print "set " $2 " " $3}') > $f
chmod 444 $f

f=../build.win32/include.tcl
echo "Building $f"
rm -f $f
(echo "$msgsh"				&& \
 echo 'global dict'			&& \
 echo 'global testdir'			&& \
 echo ''				&& \
 echo 'set CP c:/tools/cp.exe'		&& \
 echo 'set DIFF c:/tools/diff.exe'	&& \
 echo 'set KILL c:/tools/kill.exe'	&& \
 echo 'set LS c:/tools/ls.exe'		&& \
 echo 'set MKDIR c:/tools/mkdir.exe'	&& \
 echo 'set MV c:/tools/mv.exe'		&& \
 echo 'set RM c:/tools/rm.exe'		&& \
 echo 'set SED c:/tools/sed.exe'	&& \
 echo 'set SLEEP c:/tools/sleep.exe'	&& \
 echo 'set SORT c:/tools/sort.exe'	&& \
 echo 'set TR c:/tools/tr.exe'		&& \
 echo ''				&& \
 echo 'set DB_LOCK_GET 0'		&& \
 echo 'set DB_LOCK_PUT 1'		&& \
 echo 'set DB_LOCK_PUT_ALL 2'		&& \
 echo 'set DB_LOCK_PUT_OBJ 3'		&& \
 echo 'set DB_LOCK_NG 0'		&& \
 echo 'set DB_LOCK_READ 1'		&& \
 echo 'set DB_LOCK_WRITE 2'		&& \
 echo 'set DB_LOCK_IREAD 3'		&& \
 echo 'set DB_LOCK_IWRITE 4'		&& \
 echo 'set DB_LOCK_IWR 5'		&& \
 grep '#define.DB_' ../include/db.h.src | sed -e '/"/d' -e 's/[()]//g' | \
     awk '{ print "set " $2 " " $3}') > $f
chmod 444 $f

# Build tags files.
f=tags
echo "Building $f"
rm -f $f
ctags -d -w \
	../include/*.src ../include/*.h ../btree/*.[ch] ../clib/*.[ch] \
	../common/*.[ch] ../db/*.[ch] ../db185/*.[ch] ../dbm/*.[ch] \
	../hash/*.[ch] ../hsearch/*.[ch] ../lock/*.[ch] ../log/*.[ch] \
	../mp/*.[ch] ../mutex/*.[ch] ../os/*.[ch] ../txn/*.[ch] \
	../test/*.[ch]
chmod 444 $f
