#!/bin/sh -
#	$Id: s_win32_vcproj,v 1.9 2005/03/24 18:23:57 gmf Exp $
#
# Build Windows/32 .vcproj files.

. RELEASE

BUILDDIR=../build_win32
SRCFILES=srcfiles.in

create_vcproj()
{
    projname="$1"       # name of the .vcproj file
    match="$2"          # the string used to egrep the $sources file
    sources="$3"        # a modified version of $SRCFILES to facilitate matches
    vcprojtemplate="$4"    # overall template file for the .vcproj
    srctemplate="$5"    # template file for the src file fragments

    # destination targets for build components
    libDest="../../lib"
    binRelDest="../../bin"
    binDebugDest="../../bin/debug"
    jarDest="../../jar"
    jarDebugDest="../../jar/debug"
    incl="../../include"

    vcprojoutput=$BUILDDIR/$projname.vcproj

    rm -f $vcprojoutput.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;../build_win32;.;' \
            -e 's;/;\\\\\\\\;g'`
	srcdir="$1"
	srcfile="$2"
        sed -e "s/@srcdir@/$srcdir/g" \
            -e "s/@srcfile@/$srcfile/g" \
            < $srctemplate >> $vcprojoutput.insert
    done
    sed -e "/@SOURCE_FILES@/r$vcprojoutput.insert" \
        -e "/@SOURCE_FILES@/d" \
        -e "s/@project_name@/$projname/g" \
        -e "s!@lib_dest@!$libDest!g" \
        -e "s!@bin_rel_dest@!$binRelDest!g" \
        -e "s!@bin_debug_dest@!$binDebugDest!g" \
        -e "s!@jar_rel_dest@!$jarDest!g" \
        -e "s!@jar_debug_dest@!$jarDebugDest!g" \
        -e "s!@include@!$incl!g" \
        -e "s/@DBXML_VERSION_MAJOR@/$DBXML_VERSION_MAJOR/g" \
        -e "s/@DBXML_VERSION_MINOR@/$DBXML_VERSION_MINOR/g" \
        -f lib_paths.sed \
     < $vcprojtemplate > $vcprojoutput.new

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

TMPA=/tmp/swin32vcproj$$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 module in $MODULES
do
    case "$module" in
    lib )
        create_vcproj dbxml " $module " $TMPA \
		$BUILDDIR/dbxml_vcproj.src $BUILDDIR/srcfile_vcproj.src
        ;;
    dll=* )
	dllname=`echo $module | sed -e 's/^dll=//'`
	if [ -f $BUILDDIR/$dllname.vcproj.src ] ; then
		srcname=$BUILDDIR/$dllname.vcproj.src
	else 
		srcname=$BUILDDIR/dll_vcproj.src
	fi
        create_vcproj $dllname " $module " $TMPA \
		$srcname $BUILDDIR/srcfile_vcproj.src
        ;;
    app=* )
	appname=`echo $module | sed -e 's/^app=//'`
	if [ -f $BUILDDIR/$appname.vcproj.src ] ; then
		srcname=$BUILDDIR/$appname.vcproj.src
	else 
		srcname=$BUILDDIR/app_vcproj.src
	fi
        create_vcproj $appname " $module " $TMPA \
		$srcname $BUILDDIR/srcfile_vcproj.src
        ;;
    test=* )
	appname=`echo $module | sed -e 's/^test=//'`
        create_vcproj $appname " $module " $TMPA \
		$BUILDDIR/test_vcproj.src $BUILDDIR/srcfile_vcproj.src
        ;;
    * )
        echo "s_win32_vcproj: module name $module in $SRCFILES is unknown type"
        ;;
    esac
done

# generate BDBXML_all.sln
sed -f lib_paths.sed < $BUILDDIR/BDBXML_all.sln.template > $BUILDDIR/BDBXML_all.sln.new
cmp $BUILDDIR/BDBXML_all.sln.new $BUILDDIR/BDBXML_all.sln > /dev/null 2>&1 ||
	(echo "Building BDBXML_all.sln" && rm -f $BUILDDIR/BDBXML_all.sln &&
	    cp $BUILDDIR/BDBXML_all.sln.new $BUILDDIR/BDBXML_all.sln && chmod 664 $BUILDDIR/BDBXML_all.sln)
#rm -f $BUILDDIR/BDBXML_all.sln.new

# generate bdbxml_msi.sln
sed -f lib_paths.sed < $BUILDDIR/bdbxml_msi.sln.template > $BUILDDIR/bdbxml_msi.sln.new
cmp $BUILDDIR/bdbxml_msi.sln.new $BUILDDIR/bdbxml_msi.sln > /dev/null 2>&1 ||
	(echo "Building bdbxml_msi.sln" && rm -f $BUILDDIR/bdbxml_msi.sln &&
	    cp $BUILDDIR/bdbxml_msi.sln.new $BUILDDIR/bdbxml_msi.sln && chmod 664 $BUILDDIR/bdbxml_msi.sln)
#rm -f $BUILDDIR/bdbxml_msi.sln.new

rm -f $TMPA
