#!/bin/sh -
#	$Id: s_windows_dsp,v 12.9 2006/07/13 06:19:45 mjc Exp $
#
# Build Windows/32 .dsp files.

. RELEASE

SRCFILES=srcfiles.in

create_dsp()
{
    projname="$1"       # name of the .dsp file
    match="$2"          # the string used to egrep the $sources file
    sources="$3"        # a modified version of $SRCFILES to facilitate matches
    dsptemplate="$4"    # overall template file for the .dsp
    extra_cppflags="$5" # extra flags to send to compiler
    release_libs="$6"   # libraries to link against in Release builds
    debug_libs="$7"     # libraries to link against in Debug builds
    lib_suffix="$8"     # the library name is libdb@lib_suffix@@VERSION@

    srctemplate="$BUILDDIR/srcfile_dsp.src"    # template file for the src file fragments
    dspoutput=$BUILDDIR/$projname.dsp


    postbuild=$dspoutput.postbuild
    if [ ! -f $postbuild ] ; then
	    postbuild=/dev/null
    fi

    rm -f $dspoutput.insert
    for srcpath in `egrep "$match" $sources | sed -e 's/[ 	].*//'`
    do
        # take the path name and break it up, converting / to \\.
        # so many backslashes needed because of shell quoting and
        # sed quoting -- we'll end up with two backslashes for every
        # forward slash, but we need that when feeding that to the
        # later sed command.
        set - `echo $srcpath | sed -e 's;\(.*\)/;../\\1 ;' \
            -e "s;$BUILDDIR;.;" \
            -e 's;/;\\\\\\\\;g'`
	srcdir="$1"
	srcfile="$2"
        sed -e "s/@srcdir@/$srcdir/g" \
            -e "s/@srcfile@/$srcfile/g" \
            < $srctemplate >> $dspoutput.insert
    done
    sed -e "/@SOURCE_FILES@/r$dspoutput.insert" \
        -e "/@SOURCE_FILES@/d" \
        -e "/@POST_BUILD@/r$postbuild" \
        -e "/@POST_BUILD@/d" \
        -e "s/@project_name@/$projname/g" \
        -e "s/@bin_rel_dest@/Release/g" \
        -e "s/@lib_rel_dest@/Release/g" \
        -e "s/@bin_debug_dest@/Debug/g" \
        -e "s/@lib_debug_dest@/Debug/g" \
        -e "s,@extra_cppflags@,$extra_cppflags,g" \
        -e "s,@release_libs@,$release_libs,g" \
        -e "s,@debug_libs@,$debug_libs,g" \
        -e "s,@lib_suffix@,$lib_suffix,g" \
        -e "s/@DB_VERSION_MAJOR@/$DB_VERSION_MAJOR/g" \
        -e "s/@DB_VERSION_MINOR@/$DB_VERSION_MINOR/g" \
      < $dsptemplate > $dspoutput.new

    # Set the file mode to 644 because the VC++ IDE needs a writeable file
    # in our development environment.
    cmp $dspoutput.new $dspoutput > /dev/null 2>&1 ||
	(echo "Building $dspoutput" && rm -f $dspoutput &&
	    cp $dspoutput.new $dspoutput && chmod 664 $dspoutput)
    rm -f $dspoutput.insert $dspoutput.new
}

TMPA=/tmp/swindsp$$a
trap "rm -f $TMPA; exit 1" 1 2 3 15

# create a copy of the srcfiles with comments and empty lines removed.
# add a space at the end of each list of modules so that each module
# can be unambiguously matched e.g. ' dynamic '
sed -e "s/#.*$//" \
    -e "/^[ 	]*$/d" \
    -e "s/[ 	][ 	]*/ /" \
    -e "s/[ 	]*$//" \
    -e "/[	 ]/!d" \
    -e "s/$/ /" < $SRCFILES > $TMPA

# get a list of all modules mentioned
#
MODULES="`sed -e 's/^[^ ]* //' < $TMPA \
    | tr ' ' '\012' | sort | uniq`"

for BUILDDIR in ../build_windows
do
    for module in $MODULES
    do
        case "$module" in
        dynamic )
            create_dsp db_dll " $module " $TMPA $BUILDDIR/dynamic_dsp.src \
                '' 'ws2_32.lib' 'ws2_32.lib'
            ;;
        small )
            create_dsp db_small " $module " $TMPA $BUILDDIR/static_dsp.src \
                '/D "HAVE_SMALLBUILD"' '' '' _small
            ;;
        static )
            create_dsp db_static " $module " $TMPA $BUILDDIR/static_dsp.src
            ;;
        java )
            create_dsp db_java " $module " $TMPA $BUILDDIR/dynamic_dsp.src '' \
                'libdb@DB_VERSION_MAJOR@@DB_VERSION_MINOR@.lib' \
                'libdb@DB_VERSION_MAJOR@@DB_VERSION_MINOR@d.lib' _java
            ;;
        tcl )
            create_dsp db_tcl " $module " $TMPA $BUILDDIR/dynamic_dsp.src \
                '/D "DB_TCL_SUPPORT"' \
                'libdb@DB_VERSION_MAJOR@@DB_VERSION_MINOR@.lib tcl84.lib' \
                'libdb@DB_VERSION_MAJOR@@DB_VERSION_MINOR@d.lib tcl84g.lib' _tcl
            ;;
        testutil )
            create_dsp db_test " $module " $TMPA $BUILDDIR/app_dsp.src \
	        '' '/out:"dbkill.exe"' '/out:"dbkill.exe"'
            ;;
        app=* )
            appname=`echo $module | sed -e 's/^app=//'`
            case "$appname" in
            ex_rep_base )
                libs='ws2_32.lib'
                ;;
            * )
                libs=''
                ;;
            esac
            create_dsp $appname " $module " $TMPA $BUILDDIR/app_dsp.src '' \
                $libs $libs
            ;;
        vx|vxsmall )
            ;;
        * )
            echo \
    "s_windows_dsp: module name $module in $SRCFILES is unknown type"
            ;;
        esac
    done
done

rm -f $TMPA
