#!/bin/bash -
#	$Id: s_winmsi,v 1.5 2005/04/16 15:59:55 philipr Exp $
#
# Note: The s_winmsi script in Berkeley DB core closely parallels the
# s_winmsi script in Berkeley DB/XML.  If you change one,
# consider whether your changes apply to the other.
# As of this writing, the two s_winmsi scripts 'diff' very closely, and
# identical portions have been factored into functions in s_winmsi.fcn.
#
#   Usage: s_winmsi [ options ]
#
# See the Usage() function in s_winmsi.fcn for a full list of options.
# By default, this script expects a dbxml-X.Y.Z.NC.zip file
# to be in this directory, and uses it to build all binaries
# needed for an Windows install, and finally builds the an
# output dbxml-X.Y.Z.NC.msi file that can be installed on
# Windows XP and 2000.
#
# The major other inputs to this script are these files:
#
#   features.in        list of choosable features (like Java,PHP,...)
#   files.in           what files are in each feature and where they belong
#   links.in           a list of URLs that end up as part of the Start Menu
#   environment.in     a list of environment vars that must be set
#
# This script does a number of operations, using the directory
# './winmsi/stage' as a staging area:
#
#   extracts the contents of the input ZIP file and uses those
#   files (minus docs/...) to build a Sources directory for
#   the Sources features.
#
#   builds Berkeley DB using Visual Studio tools using a .BAT
#   script derived from winbuild.in .
#
#   builds Perl and other APIs .
#
#   uses {features,files,links,environment}.in to build some include
#   files in WiX XML format.  These files are named
#   *.wixinc (e.g. directory.wixinc)
#
#   run m4 on dbxmlwix.in to create dbxml.wxs .  dbxmlwix.in
#   uses m4 macros to allow reasonable refactoring of repeated
#   UI code.  Also, m4 is used to include the files created in
#   the previous step.
#
#   Use the WiX compiler/linker on the .wxs files to create the .msi file.
#
################################################################

# Set DB_HOME.  This is needed to find installer files shared with DB core.
DB_HOME=`echo '@DB_HOME@' | sed -f lib_paths.sed`

# Make it an absolute path
DB_HOME=`(cd $DB_HOME 2>/dev/null && pwd)`

# Define all needed shell functions
. $DB_HOME/dist/winmsi/s_winmsi.fcn

ERRORLOG="$0".log
SetupErrorLog

# Do this before parsing options, we need the version number
. ./RELEASE
dbver=dbxml-$DBXML_VERSION

# Set variables used by functions to customize this installer
PRODUCT_NAME="Berkeley DB XML"
PRODUCT_VERSION="$DBXML_VERSION"
PRODUCT_STAGE=`pwd`/winmsi/stage
PRODUCT_LICENSEDIR="${PRODUCT_STAGE}/$dbver/dbxml"
PRODUCT_SUB_BLDDIR="${PRODUCT_STAGE}/$dbver/dbxml"
PRODUCT_BLDDIR="${PRODUCT_STAGE}/$dbver"
PRODUCT_SRCDIR="${PRODUCT_STAGE}/$dbver"
PRODUCT_DBBUILDDIR=".../ERROR_DBBUILDDIR_USED/..."
PRODUCT_SHARED_WINMSIDIR=$DB_HOME/dist/winmsi
PRODUCT_IMAGEDIR=$PRODUCT_SHARED_WINMSIDIR/images
PRODUCT_ZIP_FILEFMT="dbxml-X.Y.Z.NC.ZIP"
PRODUCT_MSI_FILEFMT="dbxml-X.Y.Z.NC.MSI"

# Gather command line options, and use reasonable defaults
SetupOptions \
        -input "$dbver.zip" \
        -output "$dbver.msi" \
        "$@"

if [ "$OPT_USEBUILD" != '' ]; then
    PRODUCT_BLDDIR="${OPT_USEBUILD}"
    PRODUCT_SUB_BLDDIR="${OPT_USEBUILD}/dbxml"
fi

Progress "s_winmsi starting, errors to $ERRORLOG"

# Fail fast for certain missing files

RequireCygwin
RequireJava
RequireTcl
RequireWix
RequirePython

CreateStage
cd ${PRODUCT_STAGE}


CreateSources ${PRODUCT_STAGE}/Sources

# The sources contain some things we don't want to install. The easiest thing
# to do is remove them here. (George supplied the list to exclude)
Progress "Removing unnecessary sources in ${PRODUCT_STAGE}/Sources"
rm -fr ${PRODUCT_STAGE}/Sources/dbxml/build_win32/*.ncb
rm -fr ${PRODUCT_STAGE}/Sources/dbxml/build_win32/*.suo
rm -fr ${PRODUCT_STAGE}/Sources/dbxml/build_win32/*.template
rm -fr ${PRODUCT_STAGE}/Sources/dbxml/build_win32/bdbxml_msi*


# The docs are put into a separate feature set
mv ${PRODUCT_STAGE}/Sources/dbxml/docs ${PRODUCT_STAGE}/

# Build everything unless we were told to use a preexisting build
if [ "$OPT_USEBUILD" = '' ]; then
    CreateWindowsBuild
    CreateWindowsSystem

    # We don't need to create an include directory, one
    # is already built in ${PRODUCT_BLDDIR}/include
fi

if ! "$OPT_SKIPGEN" ; then
  CreateLicenseRtf ../../../LICENSE license.rtf
  CreateWixIncludeFiles
fi

CreateMsi ../dbxmlwix.in dbxml.wxs "$OPT_OUTFILE"

Progress "s_winmsi finished, $OPT_OUTFILE created."
exit 0

