#!/bin/sh -
#	$Id: s_java,v 1.13 2002/09/09 20:47:30 bostic Exp $
#
# Build the Java files.

msgjava="/* DO NOT EDIT: automatically built by dist/s_java. */"

. RELEASE

t=/tmp/__java
c=/tmp/__javajnic
h=/tmp/__javajnih
trap 'rm -f $t $c $h; exit 0' 0 1 2 3 13 15

# Build DbConstants.java.
(echo "$msgjava" &&
 echo &&
 echo 'package com.sleepycat.db;' &&
 echo &&
 echo 'class DbConstants' &&
 echo '{' &&
 for i in `egrep '^DB_.*J$' pubdef.in | awk '{print $1}'`; do \
	egrep -w "^#define[	 ]$i|^[	 ][	 ]*$i" ../dbinc/db.in; \
 done |
 sed -e "s/@DB_VERSION_MAJOR@/$DB_VERSION_MAJOR/" \
     -e "s/@DB_VERSION_MINOR@/$DB_VERSION_MINOR/" \
     -e "s/@DB_VERSION_PATCH@/$DB_VERSION_PATCH/" \
     -e 's/^#define[	 ][	 ]*//' \
     -e 's/[()=,]/ /g' \
     -e 's/\/\*/ /' | \
 awk '{ print "    static final int " $1 " = " $2 ";" }' &&
 echo '}' &&
 echo &&
 echo '// end of DbConstants.java') > $t

f=../java/src/com/sleepycat/db/DbConstants.java
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

# Build Db.java.
f=../java/src/com/sleepycat/db/Db.java.in
sed '/BEGIN-JAVA-SPECIAL-CONSTANTS/q' < $f > $t
(echo "    $msgjava" &&
 for i in `egrep '^DB_.*C$' pubdef.in | awk '{print $1}'`; do \
	egrep -w "^#define[	 ]$i|^[	 ][	 ]*$i" ../dbinc/db.in; \
 done |
 sed -e 's/^#define[	 ][	 ]*//' \
     -e 's/[()=,]/ /g' |
 awk '{ print "    public static final int " $1 " = " $2 ";" }') >> $t
(for i in `egrep '^DB_.*J$' pubdef.in | awk '{print $1}'`; do \
	egrep -w "^#define[	 ]$i|^[	 ][	 ]*$i" ../dbinc/db.in; \
 done |
 sed -e 's/^#define[	 ][	 ]*//' \
     -e 's/[()=,]/ /g' |
 awk '{ print "    public static final int " $1 ";" }') >> $t
sed -n \
    '/END-JAVA-SPECIAL-CONSTANTS/,/BEGIN-JAVA-CONSTANT-INITIALIZATION/p' \
    < $f >> $t
(echo "        $msgjava" &&
 for i in `egrep '^DB_.*J$' pubdef.in | awk '{print $1}'`; do \
	egrep -w "^#define[	 ]$i|^[	 ][	 ]*$i" ../dbinc/db.in; \
 done |
 sed -e 's/^#define[	 ][	 ]*//' \
     -e 's/[()=,]/ /g' \
     -e 's/\/\*/ /' | \
 awk '{ print "        " $1 " = DbConstants." $1 ";" }') >> $t
sed -n '/END-JAVA-CONSTANT-INITIALIZATION/,$p' < $f >> $t

f=../java/src/com/sleepycat/db/Db.java
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

# Script to convert DB C structure declarations into Java declarations.
jclass()
{
	cat ../dbinc/db.in |
	sed -n \
	-e "/struct $1 {/,/^}/{" \
	-e "/$1/d" \
	-e '/;/!d' \
	-e '/^}/d' \
	-e '/char[	 ]*\*/{' \
	-e "s/^[	 ]*[^\*]*\*[	 ]*\([^;]*\).*/$2    public String \1;/p"\
	-e 'd' \
	-e '}' \
	-e '/time_t/{' \
	-e "s/^[	 ]*[^	 ]*[	 ]*\([^;]*\).*/$2    public long \1;/p" \
	-e 'd' \
	-e '}' \
	-e '/DB_LSN[	 ]*/{' \
	-e "s/^[	 ]*[^	 ]*[	 ]*\([^;]*\).*/$2    public DbLsn \1;/p"\
	-e 'd' \
	-e '}' \
	-e '/DB_TXN_ACTIVE[	 ]*\*/{' \
	-e "s/^[	 ]*[^\*]*\*[	 ]*\([^;]*\).*/$2    public Active \1[];/p"\
	-e 'd' \
	-e '}' \
	-e "s/^[	 ]*[^	 ]*[	 ]*\([^;]*\).*/$2    public int \1;/p" \
	-e '}'
}

# Script to convert DB C structure declarations into Java declarations.
jclass_jni()
{
	c=$3
	h=$4
	echo "extern int $2(JNIEnv *jnienv, jclass cl, jobject jobj, struct $1 *statp);" >> $h
	echo "int $2(JNIEnv *jnienv, jclass cl," >> $c
	echo "    jobject jobj, struct $1 *statp) {" >> $c
	cat ../dbinc/db.in |
	sed -n \
	-e "/struct $1 {/,/^}/{" \
	-e "/$1/d" \
	-e '/;/!d' \
	-e '/^}/d' \
	-e '/char[	 ]*\*/{' \
            -e "s/^[	 ]*[^\*]*\*[	 ]*\([^;]*\).*/	JAVADB_STAT_STRING(jnienv, cl, jobj, statp, \1);/p"\
	-e 'd' \
	-e '}' \
	-e '/time_t/{' \
	-e "s/^[	 ]*[^	 ]*[	 ]*\([^;]*\).*/	JAVADB_STAT_LONG(jnienv, cl, jobj, statp, \1);/p" \
	-e 'd' \
	-e '}' \
	-e '/DB_LSN[	 ]*/{' \
	-e "s/^[	 ]*[^	 ]*[	 ]*\([^;]*\).*/	JAVADB_STAT_LSN(jnienv, cl, jobj, statp, \1);/p"\
	-e 'd' \
	-e '}' \
	-e '/DB_TXN_ACTIVE[	 ]*\*/{' \
	-e "s/^[	 ]*[^\*]*\*[	 ]*\([^;]*\).*/	JAVADB_STAT_ACTIVE(jnienv, cl, jobj, statp, \1);/p"\
	-e 'd' \
	-e '}' \
	-e "s/^[	 ]*[^	 ]*[	 ]*\([^;]*\).*/	JAVADB_STAT_INT(jnienv, cl, jobj, statp, \1);/p" \
	-e '}' >> $c
        echo '	return (0);' >> $c
	echo '}' >> $c
}

echo "$msgjava" >> $c
echo "$msgjava" >> $h
echo '#include "java_util.h"' >> $c

# Build DbBtreeStat.java
(echo "$msgjava" &&
 echo &&
 echo 'package com.sleepycat.db;' &&
 echo &&
 echo "public class DbBtreeStat"
 echo '{'
 jclass __db_bt_stat &&
 echo '}' &&
 echo '// end of DbBtreeStat.java') > $t
jclass_jni __db_bt_stat __jv_fill_bt_stat $c $h
f=../java/src/com/sleepycat/db/DbBtreeStat.java
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

# Build DbHashStat.java
(echo "$msgjava" &&
 echo &&
 echo 'package com.sleepycat.db;' &&
 echo &&
 echo "public class DbHashStat"
 echo '{'
 jclass __db_h_stat &&
 echo '}' &&
 echo '// end of DbHashStat.java') > $t
jclass_jni __db_h_stat __jv_fill_h_stat $c $h
f=../java/src/com/sleepycat/db/DbHashStat.java
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

# Build DbLockStat.java
(echo "$msgjava" &&
 echo &&
 echo 'package com.sleepycat.db;' &&
 echo &&
 echo "public class DbLockStat"
 echo '{'
 jclass __db_lock_stat &&
 echo '}' &&
 echo '// end of DbLockStat.java') > $t
jclass_jni __db_lock_stat __jv_fill_lock_stat $c $h
f=../java/src/com/sleepycat/db/DbLockStat.java
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

# Build DbLogStat.java
(echo "$msgjava" &&
 echo &&
 echo 'package com.sleepycat.db;' &&
 echo &&
 echo "public class DbLogStat"
 echo '{'
 jclass __db_log_stat &&
 echo '}' &&
 echo '// end of DbLogStat.java') > $t
jclass_jni __db_log_stat __jv_fill_log_stat $c $h
f=../java/src/com/sleepycat/db/DbLogStat.java
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

# Build DbMpoolFStat.java
(echo "$msgjava" &&
 echo &&
 echo 'package com.sleepycat.db;' &&
 echo &&
 echo "public class DbMpoolFStat"
 echo '{'
 jclass __db_mpool_fstat &&
 echo '}' &&
 echo '// end of DbMpoolFStat.java') > $t
jclass_jni __db_mpool_stat __jv_fill_mpool_stat $c $h
f=../java/src/com/sleepycat/db/DbMpoolFStat.java
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

# Build DbQueueStat.java
(echo "$msgjava" &&
 echo &&
 echo 'package com.sleepycat.db;' &&
 echo &&
 echo "public class DbQueueStat"
 echo '{'
 jclass __db_qam_stat &&
 echo '}' &&
 echo '// end of DbQueueStat.java') > $t
jclass_jni __db_qam_stat __jv_fill_qam_stat $c $h
f=../java/src/com/sleepycat/db/DbQueueStat.java
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

# Build DbRepStat.java
(echo "$msgjava" &&
 echo &&
 echo 'package com.sleepycat.db;' &&
 echo &&
 echo "public class DbRepStat"
 echo '{'
 jclass __db_rep_stat &&
 echo '}' &&
 echo '// end of DbRepStat.java') > $t
jclass_jni __db_rep_stat __jv_fill_rep_stat $c $h
f=../java/src/com/sleepycat/db/DbRepStat.java
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

# Build DbTxnStat.java
(echo "$msgjava" &&
 echo &&
 echo 'package com.sleepycat.db;' &&
 echo &&
 echo "public class DbTxnStat"
 echo '{'
 echo "    public static class Active {"
 jclass __db_txn_active "    " &&
 echo '    };' &&
 jclass __db_txn_stat &&
 echo '}' &&
 echo '// end of DbTxnStat.java') > $t
jclass_jni __db_txn_stat __jv_fill_txn_stat $c $h
f=../java/src/com/sleepycat/db/DbTxnStat.java
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

mv $c $t
f=../libdb_java/java_stat_auto.c
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)

mv $h $t
f=../libdb_java/java_stat_auto.h
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)
