#!/bin/sh
#
# This script pulls the ch-werner ODBC and JDBC packages into a Berkeley DB
# source tree.

# javasqlite-<date>.tar.gz and sqliteodbc-X.XX.tar.gz packages must be
# in the parent of this repository/source tree.

# The packages can be downloaded from (update to current version):
# http://www.ch-werner.de/sqliteodbc/sqliteodbc-0.86.tar.gz
# http://www.ch-werner.de/javasqlite/javasqlite-20100727.tar.gz
die()
{
	echo >&2 "$@"
	exit 1
}

START_DIR=`pwd`
if [ "x$1" != "x" ]; then
	PKG_ROOT=$1
else
	PKG_ROOT=../..
fi

#DB_ADDONS_REPO=$PKG_ROOT/db_addons
#if [ ! -d $DB_ADDONS_REPO ]; then
#	die "db_addons repository doesn't exist."
#fi
#cd $DB_ADDONS_REPO
#hg pull -u
#if [ $? != 0 ]; then
#	die "Failed updating the db_addons repository."
#fi
cd $START_DIR
ODBC_PACKAGE=`ls $PKG_ROOT/sqliteodbc-0.86.tar.gz | tail -1`
JDBC_PACKAGE=`ls $PKG_ROOT/javasqlite-20100727.tar.gz | tail -1`

if [ ! -e $ODBC_PACKAGE ]; then
	echo "No ODBC package found, can't continue."
	echo "Download it from: http://www.ch-werner.de/sqliteodbc"
	exit 1
fi
if [ ! -e $JDBC_PACKAGE ]; then
	echo "No JDBC package found, can't continue."
	echo "Download it from: http://www.ch-werner.de/javasqlite"
	exit 1
fi

SQL_DIR=../lang/sql/
ODBC_DIR=$SQL_DIR/odbc
JDBC_DIR=$SQL_DIR/jdbc
rm -rf "$ODBC_DIR" && mkdir -p $ODBC_DIR
rm -rf "$JDBC_DIR" && mkdir -p $JDBC_DIR

HOMEDIR=`pwd`

# Don't assume GNU tar
dir=`basename $ODBC_PACKAGE .tar.gz`
gzip -c -d $ODBC_PACKAGE | tar xf -
mv $dir/* $ODBC_DIR || exit $?

dir=`basename $JDBC_PACKAGE .tar.gz`
gzip -c -d $JDBC_PACKAGE | tar xf -
mv $dir/* $JDBC_DIR || exit $?

# Remove unnecessary files from packages.

# There is a symbolic link in the ODBC package: that can't go into a zip file.
rm -f $ODBC_DIR/source

# TCC has a conflicting license, so we can't ship it.
rm -rf $ODBC_DIR/tcc* $ODBC_DIR/sqlite+tcc.c $ODBC_DIR/README.sqlite+tcc

# Remove useless makefiles, they create confusion.
for f in `ls $JDBC_DIR/*mak`; do
	# TODO: It would be nice to pull this release number from the SQL
	#       code in the repository.
	if [ `basename $f` = "sqlite-3.6.22.mak" ]; then
		continue;
	fi
	rm -f $f
done

# Remove some SQLite format databases from the package.
rm -f $JDBC_DIR/db $JDBC_DIR/db2 $JDBC_DIR/db3

# Remove other bits and pieces that aren't relevant.
rm -rf $JDBC_DIR/debian $ODBC_DIR/*ming* $ODBC_DIR/README.* $ODBC_DIR/*mak

# Patch the JDBC and ODBC build files for autoconf, so the Berkeley DB library
# can be added to the link command.
mv $JDBC_DIR/Makefile.in $JDBC_DIR/Makefile.in.tmp
# 1. Define BDB_LIB for Makefile
# 2. Add LD_LIBRARY_PATH for runing native/mkconst in JDBC configuration
cat $JDBC_DIR/Makefile.in.tmp | \
        sed -e '/native\/mkconst > SQLite\/Constants.java/ i \\t\LD_LIBRARY_PATH=\"\$\$LD_LIBRARY_PATH:\$(PWD)\/..\/.libs\" \\' \
            -e '/^LIBS=/s/$/ @BDB_LIB@/' > $JDBC_DIR/Makefile.in
rm -f $JDBC_DIR/Makefile.in.tmp

# Generate the resource3.h file needed by ODBC on Windows.
VERS=`cat $ODBC_DIR/VERSION`
VERS_C=`echo $VERS | sed -e 's/\([0-9]\+\)[.]\([0-9]\+\).*/\1,\2/g'`
cat $ODBC_DIR/resource.h.in | sed -e "s/--VERS_C--/$VERS_C/g" -e "s/--VERS--/$VERS/g" > $ODBC_DIR/resource3.h

# Let sqlite3odb.rc include "$SQL_DIR/generated/sqlite3.h" instead of sqlite3.h 
mv $ODBC_DIR/sqlite3odbc.rc $ODBC_DIR/sqlite3odbc.rc.in
cat $ODBC_DIR/sqlite3odbc.rc.in | sed -e 's/sqlite3.h/..\/lang\/sql\/generated\/sqlite3.h/g' > $ODBC_DIR/sqlite3odbc.rc

# Generate Constants.java needed by JDBC on Windows
gcc -DHAVE_SQLITE3 -I$SQL_DIR/generated -o mkconst $JDBC_DIR/native/mkconst.c
./mkconst > $JDBC_DIR/SQLite/Constants.java

# Add the wildcard below since gcc on Windows appends a .exe
rm -f mkconst*

