#!/bin/sh -
#	$Id: s_java_stat,v 1.2 2004/03/30 01:21:34 jtownsen Exp $
#
# Build the Java files.

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

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

# 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 '/u_int8_t[	 ]*xid\[/{' \
	-e "s/^[	 ]*[^\*]*\*[	 ]*\([^;]*\).*/$2    public byte[] xid;/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
	echo "static 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 '/u_int8_t[	 ]*xid\[/{' \
	-e "s/^[	 ]*[^\*]*\*[	 ]*\([^;]*\).*/	JAVADB_STAT_XID(jnienv, cl, jobj, statp, xid);/p"\
	-e 'd' \
	-e '}' \
	-e "s/^[	 ]*[^	 ]*[	 ]*\([^;]*\).*/	JAVADB_STAT_INT(jnienv, cl, jobj, statp, \1);/p" \
	-e '}' >> $c
        echo '	return (0);' >> $c
	echo '}' >> $c
}

# Script to convert DB C structure declarations into a toString method body
jclass_toString()
{
	echo "/**"
	echo " * Provide a string representation of all the fields contained"
	echo " * within this class."
	echo " *"
	echo " * @return The string representation."
	echo " */"
	echo "$3    public String toString() {"
	echo "$3        return \"$2:\""
	cat ../dbinc/db.in |
	sed -n \
	-e "/struct $1 {/,/^}/{" \
	-e "/$1/d" \
	-e '/;/!d' \
	-e '/^}/d' \
	-e '/char[	 ]*\*/{' \
	-e "s/^[	 ]*[^\*]*\*[	 ]*\([^;]*\).*/$3            + \"\\\\n$3  \1=\" + \1/p"\
	-e 'd' \
	-e '}' \
	-e '/DB_TXN_ACTIVE[	 ]*\*/{' \
	-e "s/^[	 ]*[^\*]*\*[	 ]*\([^;]*\).*/$3            + \"\\\\n$3  \1=\" + DbUtil.objectArrayToString(\1, \"\1\")/p"\
	-e 'd' \
	-e '}' \
	-e '/u_int8_t[	 ]*xid\[/{' \
	-e "s/^[	 ]*[^\*]*\*[	 ]*\([^;]*\).*/$3            + \"\\\\n$3  xid=\" + DbUtil.byteArrayToString(xid)/p"\
	-e 'd' \
	-e '}' \
	-e "s/^[	 ]*[^	 ]*[	 ]*\([^;]*\).*/$3            + \"\\\\n$3  \1=\" + \1/p"\
	-e '}'
	echo "$3            ;"
	echo "$3    }"
}

echo "$msgjava" >> $c

stat_class()
{
	c_struct=$1
	j_class=$2
	fill=$3

	(echo "$msgjava"
	 echo
	 echo 'package com.sleepycat.db;'
	 echo
	 echo "/**"
	 echo " * Statistics associated with $j_class generated by"
	 echo " * DbEnv on request.<p>"
	 echo " * The information contained within instances of this"
	 echo " * class is a snapshot in time, it is not continually updated."
	 echo " */"
	 echo "public class $j_class"
	 echo '{'
	 jclass $c_struct
	 echo
	 jclass_toString $c_struct $j_class
	 echo '}'
	 echo "// end of $j_class.java") > $t
	jclass_jni $c_struct $fill $c
	f=../java/src/com/sleepycat/db/$j_class.java
	cmp $t $f > /dev/null 2>&1 ||
	    (echo "Building $f" && rm -f $f && cp $t $f && chmod 444 $f)
}

stat_class __db_bt_stat DbBtreeStat __dbj_fill_bt_stat
stat_class __db_h_stat DbHashStat __dbj_fill_h_stat
stat_class __db_lock_stat DbLockStat __dbj_fill_lock_stat
stat_class __db_log_stat DbLogStat __dbj_fill_log_stat
stat_class __db_mpool_fstat DbMpoolFStat __dbj_fill_mpool_fstat
stat_class __db_mpool_stat DbMpoolStat __dbj_fill_mpool_stat
stat_class __db_qam_stat DbQueueStat __dbj_fill_qam_stat
stat_class __db_rep_stat DbRepStat __dbj_fill_rep_stat

# Build DbTxnStat.java - special because of embedded Active class
(echo "$msgjava" &&
 echo &&
 echo 'package com.sleepycat.db;' &&
 echo &&
 echo "/**"
 echo " * Statistics associated with DbTxnStat generated by"
 echo " * DbEnv on request."
 echo " * The information contained within instances of this"
 echo " * class is a snapshot in time, it is not continually updated."
 echo " */"
 echo "public class DbTxnStat"
 echo '{'
 echo "    public static class Active {"
 jclass __db_txn_active "    "
 jclass_toString __db_txn_active Active "    "

 echo '    };'
 jclass __db_txn_stat
 jclass_toString __db_txn_stat DbTxnStat
 echo '}'
 echo '// end of DbTxnStat.java') > $t
jclass_jni __db_txn_stat __dbj_fill_txn_stat $c $h
jclass_jni __db_txn_active __dbj_fill_txn_active $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)
