#!/bin/sh
# $Id: $
#
# Build the distribution package.
. ./RELEASE || exit 1

# A version string can be specified on the command line (e.g., "20080219").
# Otherwise, use the standard X.X.X format.
VERSION=${1:-${DB_VERSION_MAJOR}.${DB_VERSION_MINOR}.${DB_VERSION_PATCH}}

# Use "ustar" as the archiver
TAR=ustar

# Set root directory where we do the work, can be anywhere.
START_DIR=`pwd`
D=`pwd`/../release
R="$D/db-${VERSION}"
RNC="$D/db-$VERSION.NC"
#DOCS=`pwd`/../../docs_books
DOCS=`pwd`/../../docs_books-5.0

if [ ! -d $DOCS ]; then
	echo "buildpkg requires a docs_books repository at the same level as
the db repository."
	exit 1
fi

fix_permissions()
{
    ROOT_DIR="$1"
    cd $ROOT_DIR && find . -type d | xargs chmod 775
    cd $ROOT_DIR && find . -type f | xargs chmod 444
    cd $ROOT_DIR && chmod 664 build_windows/*vcproj
    cd $ROOT_DIR && chmod 664 build_wince/*vcproj
    cd $ROOT_DIR && chmod 664 build_wince/*sln
    cd $ROOT_DIR && chmod 664 csharp/doc/libdb_dotnet*.XML
    cd $ROOT_DIR && chmod 664 csharp/*csproj
    cd $ROOT_DIR && chmod 664 examples_csharp/*/*.csproj
    cd $ROOT_DIR && chmod 664 sql/jdbc/SQLite/Constants.java
    cd $ROOT_DIR && chmod 664 sql/odbc/resource3.h
    cd $ROOT_DIR && chmod 555 sql/odbc/conf* sql/jdbc/conf*
    cd $ROOT_DIR/dist && sh s_perm
}

# Create directory, remove any previous release tree.
rm -rf $R $RNC
mkdir -p $R

echo "Removed old release build from $R"

# Copy the files in the current tip to $R
hg archive $R

echo "Created hg archive in $R"

# Check that the doc repo is up to date, and create a tag if necessary.
cd $DOCS
hg pull -u
if [ $? != 0 ]; then
	echo "Failed updating the docs_books repository. Exiting."
	rm -rf $R
	exit 1
fi
has_tag=`hg tags | grep "db-${VERSION}"`
if [ "$has_tag" = "" ]; then
	hg tag "db-${VERSION}"
	TAG_CREATED="true"
else
	hg up -r "db-${VERSION}"
fi

# Build a copy of the documentation in the release tree.
cd $R/dist
sh s_docs db-${VERSION} $DOCS

if [ $? != 0 ]; then
	echo "Failed generating documentation. Exiting"
	rm -rf $R
	exit 1
fi

# Copy the C sharp doc from the original tree - it's not generated.
rm -rf $R/docs/csharp
mkdir -p $R/docs/csharp
cp -rp $START_DIR/../docs/csharp $R/docs

# Pull a copy of the JDBC and ODBC libraries into the package.
# Tell the script where to look for packages.
sh s_sql_drivers ../..

cd $START_DIR

# Build the documentation.
cd $R/dist && sh s_javadoc

# Remove source directories we don't distribute.
cd $R && rm -rf build_brew build_s60
cd $R && rm -rf java/src/com/sleepycat/xa
cd $R && rm -rf os_brew os_s60
cd $R && rm -rf rpc_* dbinc/db_server_int.h dbinc_auto/rpc*.h
cd $R && rm -rf test/TODO test/upgrade test/scr036 test_erlang
cd $R && rm -rf test_perf test_purify test_repmgr
cd $R && rm -rf test_server test_stl test_vxworks
cd $R && find . -name '.hg*' | xargs rm -f
cd $R && find . -name 'tags' | xargs rm -f

# Create symbolic links and cscope output, fix permissions.
#cd $R/dist && sh s_perm
#cd $R/dist && sh s_cscope

# Build a regular version and smoke test.
### cd $R && rm -rf build_run && mkdir build_run
### cd $R/build_run && ../dist/configure && make >& mklog
### cd $R/build_run && make ex_access && echo "test" | ./ex_access
# Check the install
### cd $R/build_run && make prefix=`pwd`/BDB install

# Build a small-footprint version and smoke test.
### cd $R && rm -rf build_run && mkdir build_run
### cd $R/build_run && ../dist/configure --enable-smallbuild && make >& mklog
### cd $R/build_run && make ex_access && echo "test" | ./ex_access

# Remove the build directory
### cd $R && rm -rf build_run

fix_permissions $R

# Check for file names differing only in case.
cd $R && find . | sort -f | uniq -ic | sed '/1 /d'

# Create the crypto tar archive release.
T="$D/db-$VERSION.tar.gz"
cd $D && $TAR czf $T -find db-$VERSION -chown 100 -chgrp 100
chmod 444 $T

# Create the non-crypto tree.
cd $D && mv -i db-$VERSION $RNC && $TAR xzf $T
cd $RNC/dist && sh s_crypto

fix_permissions $RNC

# Create the non-crypto tar archive release.
T="$D/db-$VERSION.NC.tar.gz"
cd $RNC/.. && $TAR czf $T -find db-$VERSION.NC -chown 100 -chgrp 100
chmod 444 $T

t=__tmp
cd $R && awk '{print $0 "\r"}' < LICENSE > $t && rm -f LICENSE && cp $t LICENSE
cd $R && awk '{print $0 "\r"}' < README > $t && rm -f README && cp $t README && rm $t
cd $RNC && awk '{print $0 "\r"}' < LICENSE > $t && rm -f LICENSE && cp $t LICENSE
cd $RNC && awk '{print $0 "\r"}' < README > $t && rm -f README && cp $t README && rm $t

# Create the crypto zip archive release.
T="$D/db-$VERSION.zip"
cd $R/.. && rm -f $T && zip -q -r $T db-$VERSION
chmod 444 $T

# Create the non-crypto zip archive release.
T="$D/db-$VERSION.NC.zip"
cd $RNC/.. && rm -f $T && zip -q -r $T db-$VERSION.NC 
chmod 444 $T

rm -rf $R $RNC

if [ "$TAG_CREATED" = "true" ]; then
	echo "Created a tag in docs_books repository. Please push."
fi
