#
# This script drives construction of Visual Studio (version 8 and up) projects 
# files, using an xquery script (genproject.template),an input XML
# document, and a file containing a list of project names.
#
# project name list
PROJECTS=dbxml.projects

# xquery script template
FILTER_TEMPLATE=vs2010_filter.xq

# temporary script, post-sed-replacement
TEMP_SCRIPT=genproject.script
FILTER=vs2010.filter

# xml document input template
CONFIG_INPUT=dbxml.template.xml

# temporary xml document, post-sed-replacement
CONFIG_OUTPUT=dbxml.xml

# substitute some variables in the XML document template
sed -f lib_paths.sed < $CONFIG_INPUT > $CONFIG_OUTPUT

echo "Building Visual Studio project files -- "
echo "   output only for modified projects (this can take a while)"

# for each project, substitute 3 variables in the XQuery script, then run it
for v in VC9 VC10 VC11
do
    if [ $v = "VC9" ]; then
	VERSION="9.00"
	TEMPLATE=genproject.template
	PROJECT_OUTPUT_DIR=../build_windows/VC9
    elif [ $v = "VC10" ]; then
        VERSION="10.0"
	TEMPLATE=vs2010.full.xq
	PROJECT_OUTPUT_DIR=../build_windows/VC10
    else
        VERSION="11.0"
	TEMPLATE=vs2010.full.xq
	PROJECT_OUTPUT_DIR=../build_windows/VC11
    fi
    
    echo "Building for Visual Studio version $VERSION"

    for i in `cat $PROJECTS`
    do
        echo "building ${i}"
	sed -e "s!@PROJECT_NAME@!$i!g" -e "s!@PROJECT_INPUT@!$CONFIG_OUTPUT!g" -e"s!@VISUAL_STUDIO_VERSION@!$VERSION!g" < $TEMPLATE > $TEMP_SCRIPT
	TMP=$PROJECT_OUTPUT_DIR/$i.tmp.vcproj
	if [ $v = "VC9" ]; then
	    TARG=$PROJECT_OUTPUT_DIR/${i}.vcproj
	else 
	    TARG=$PROJECT_OUTPUT_DIR/${i}.vcxproj
	fi
	xqilla -o $TMP $TEMP_SCRIPT
	rm -f $TEMP_SCRIPT
	cmp $TMP $TARG > /dev/null 2>&1 ||
	(echo "Building $TARG" && rm -f $TARG &&
	    cp $TMP $TARG && chmod 664 $TARG)
	rm -f $TMP
    done

    #for VC10, VC11, create extra filter files
    if [ $v != "VC9" ]; then
	for i in dbxml # add your project name here, if has filters
	do
	    sed -e "s!@PROJECT_NAME@!$i!g" -e "s!@PROJECT_INPUT@!$CONFIG_OUTPUT!g" -e"s!@VISUAL_STUDIO_VERSION@!$VERSION!g" < $FILTER_TEMPLATE > $FILTER
	    FTMP=$PROJECT_OUTPUT_DIR/$i.tmp.vcxproj.filters
	    FTARG=$PROJECT_OUTPUT_DIR/${i}.vcxproj.filters
	    xqilla -o $FTMP $FILTER
	    rm -f $FILTER
	    cmp $FTMP $FTARG > /dev/null 2>&1 ||
	    (echo "Building $FTARG" && rm -f $FTARG &&
	        cp $FTMP $FTARG && chmod 664 $FTARG)
	    rm -f $FTMP
	done
    fi
done
