#!/bin/sh -
#
# Generate files used to build the Android platform:
# Android.mk -- used as a makefile for building a drop-in version 
#   of libsqlite for Android
# db_config.h, sql/config.h -- hard-coded configuration used by the build.
# 
# All of the above are based on hard-coding in this configuration:
# --enable-smallbuild --enable-statistics
# If that is to change then the files involved in this script must change to 
# reflect the desired configuration

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

. ./RELEASE

s=/tmp/__db_a
t=/tmp/__db_b
u=/tmp/__db_c
androidfilelist=android.files
makefile=Android.mk
outdir=../build_android
template=android/Android.mk.template

# function to create Android.mk from a template
createAndroidMk()
{
    echo "# DO NOT EDIT: automatically built by dist/s_android."
    rm -f $u
    for file in `cat $androidfilelist` 
    do # NOTE: keep the tab as the first char of the echo below
	echo "	\$(BDB_TOP)/$file \\" >> $u
    done
    sed -e "/@SOURCE_FILES@/r$u" -e "/@SOURCE_FILES@/d" < $mk_template
    rm -f $u
}

# Build the list of files used by the Android build
sed -e '/^$/d' -e '/^[	 #]/d' srcfiles.in |
    egrep -w android |
    sed 's/[	 ].*//' > $androidfilelist

for mode in "drop-in" "jdbc"
do
    # Generate "drop-in" make file on build_android/
    # Generate "JDBC" make file on build_android/jdbc/jni. The name "jni" 
    # is required by Android ndk toolchain.
    if [ "$mode" = "drop-in" ]; then
        mk_outdir=../build_android
        mk_template=android/Android.mk.template
    elif [ "$mode" = "jdbc" ]; then
        mk_outdir=../build_android/jdbc/jni
        mk_template=android/jdbc/Android.mk.template
    else
        echo "Unknown mode $mode"
        exit 1
    fi

    # create Android.mk substituting relevant version strings
    createAndroidMk | sed -e "s/@DB_VERSION@/$DB_VERSION/g" \
        -e "s/@DB_VERSION_FULL_STRING@/$DB_VERSION_FULL_STRING/g" > $t

    f=$mk_outdir/Android.mk
    cmp $t $f > /dev/null 2>&1 ||
        (echo "Building $f" && rm -f $f && cp $t $f)
done

# Build other required header files usually created by configure
#    db.h db_config.h, sql/config.h, db_int.h and clib_port.h
# These are all sed-based editing of template files that would othwerwise
# be done by a configure script

# Build db.h from dbinc/db.in (Android is ARM Linux)

cat <<ENDOFSEDTEXT > $s
s/@inttypes_h_decl@/#include <inttypes.h>/
s/@stdint_h_decl@/#include <stdint.h>/
s/@stddef_h_decl@/#include <stddef.h>/
s/@unistd_h_decl@/#include <unistd.h>/
s/@thread_h_decl@/#include <pthread.h>/
s/@u_int8_decl@//
/@int16_decl@/d
s/@u_int16_decl@//
/@int32_decl@/d
s/@u_int32_decl@//
s/@int64_decl@//
s/@u_int64_decl@//
/@u_char_decl@/d
/@u_int_decl@/d
/@u_long_decl@/d
/@u_short_decl@/d
s/@uintmax_t_decl@//
s/@uintptr_t_decl@//
/@FILE_t_decl@/d
/@off_t_decl@/d
s/@db_off_t_decl@/typedef int64_t db_off_t;/
/@pid_t_decl@/d
/@size_t_decl@/d
/@ssize_t_decl@/d
/@time_t_decl@/d
s/@db_seq_decl@/typedef int64_t db_seq_t;/
s/@db_threadid_t_decl@/typedef pthread_t db_threadid_t;/
s/@DB_VERSION_FAMILY@/$DB_VERSION_FAMILY/
s/@DB_VERSION_RELEASE@/$DB_VERSION_RELEASE/
s/@DB_VERSION_MAJOR@/$DB_VERSION_MAJOR/
s/@DB_VERSION_MINOR@/$DB_VERSION_MINOR/
s/@DB_VERSION_PATCH@/$DB_VERSION_PATCH/
s/@DB_VERSION_STRING@/"$DB_VERSION_STRING"/
s/@DB_VERSION_FULL_STRING@/"$DB_VERSION_FULL_STRING"/
s/@DB_VERSION_UNIQUE_NAME@//
s/@DB_CONST@//
s/@DB_PROTO1@/#undef __P/
s/@DB_PROTO2@/#define	__P(protos)	protos/
/@platform_header@/d
/@platform_footer@/d
ENDOFSEDTEXT
(echo "$msgc" &&
    sed -f $s ../src/dbinc/db.in &&
    cat ../src/dbinc_auto/api_flags.in &&
    cat ../src/dbinc_auto/ext_prot.in) > $t
test `egrep '@.*@' $t` && {
	egrep '@.*@' $t
	echo 'Unexpanded autoconf variables found in Android db.h.'
	exit 1
}
f=$outdir/db.h
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f)

# Build db_int.h from dbinc/db_int.in
cat <<ENDOFSEDTEXT > $s
s/@DB_STRUCT_ALIGN8@//
s/@PATH_SEPARATOR@/\//
s/@db_int_def@//
ENDOFSEDTEXT
(echo "$msgc" && sed -f $s ../src/dbinc/db_int.in) > $t
test `egrep '@.*@' $t` && {
	egrep '@.*@' $t
	echo 'Unexpanded autoconf variables found in Android db_int.h.'
	exit 1
}
f=$outdir/db_int.h
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f)

# Build clib_port.h from dist/clib_port.in
cat <<ENDOFSEDTEXT > $s
s/@INT64_FMT@/#define	INT64_FMT	"%lld"/
s/@UINT64_FMT@/#define	UINT64_FMT	"%llu"/
ENDOFSEDTEXT
sed -f $s clib_port.in > $t
test `egrep '@.*@' $t` && {
	egrep '@.*@' $t
	echo 'Unexpanded autoconf variables found in Android clib_port.h.'
	exit 1
}
f=$outdir/clib_port.h
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f)

# Build db_config.h from dist/android/android_config.in
# Don't fail, but complain if the android_config.in file isn't up-to-date.
check_config()
{
	egrep '^#undef' config.hin | awk '{print $2}' | sort > $s
	(egrep '#undef' $1 | awk '{print $3}'
	 egrep '^#define' $1 | awk '{print $2}') | sort > $t
	cmp $s $t > /dev/null || {
		echo "config.hin and $1 differ"
		echo "<<< config.hin >>> $1"
		diff $s $t
	}
}
check_config android/android_config.in
f=$outdir/db_config.h
(echo "$msgc" && sed "s/__EDIT_DB_VERSION__/$DB_VERSION/" android/android_config.in) > $t
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f)


# Build sql/config.h from android/sql_config.in
f=$outdir/sql/config.h
sqlitever=`cat ../lang/sql/sqlite/VERSION`
mkdir $outdir/sql >/dev/null 2>&1
# NOTE: the content of sql_config.in is hard-coded based on configuration
# on the Android platform

(echo "$msgc" && sed -e "s/@SQLITE_VERSION@/$sqlitever/g" android/sql_config.in) > $t
cmp $t $f > /dev/null 2>&1 ||
    (echo "Building $f" && rm -f $f && cp $t $f)


#cleanup
rm $androidfilelist
rm -f $s $t $u

