#!/bin/bash -
#	$Id$
#
# Build Windows CE .vcproj files
# Build Windows .vcproj files.

. ./RELEASE

# Detect XQilla
which xqilla  > /dev/null 2>&1
if [ $? -ne 0 ]; then
 	echo "s_windows_dsp can not find xqilla command."
    	exit 1
fi

create_projects()
{
    # project name list
    PROJECTS=$1

    # xquery script template
    TEMPLATE=genproject.template

    # common properties template
    LIBTEMPLATE=library.props.template
    APPTEMPLATE=application.props.template

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

    # temporary common properties
    TEMP_LIB=library.props
    TEMP_APP=application.props

    # xml document input template
    CONFIG_INPUT=$2

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

    # location for output project files
    PROJECT_OUTPUT_PARENT_DIR=$3

    # judge Windows or Win CE for vs2010
    WinVersion=$4

    if [ $WinVersion = "WinCE" ]; then
      VLoop=VS15
    else 
      VLoop="VS10 VS12 VS15"
    fi

    # for each project, substitute 2 variables in the XQuery script, then run it
    for v in $VLoop
    do

 	if [ $v = "VS10" ]; then
	    VERSION="10.0"
 	elif [ $v = "VS12" ]; then
	    VERSION="11.0"
	elif [ $v = "VS15" ]; then
	    VERSION="14.0"
        fi

	PROJECT_OUTPUT_DIR="$PROJECT_OUTPUT_PARENT_DIR/$v"

    	# substitute some variables in the XML document template
    	sed -e "s/@DB_VERSION_MAJOR@/$DB_VERSION_MAJOR/g" \
            -e "s/@DB_VERSION_MINOR@/$DB_VERSION_MINOR/g" \
            -e "s/$/
/" \
            < $LIBTEMPLATE > $TEMP_LIB

        sed -e "s/@DB_VERSION_MAJOR@/$DB_VERSION_MAJOR/g" \
            -e "s/@DB_VERSION_MINOR@/$DB_VERSION_MINOR/g" \
            -e "s/$/
/" \
            < $APPTEMPLATE > $TEMP_APP

	mv $TEMP_LIB $PROJECT_OUTPUT_DIR/$TEMP_LIB
        mv $TEMP_APP $PROJECT_OUTPUT_DIR/$TEMP_APP
    	sed -e "s/@DB_VERSION_MAJOR@/$DB_VERSION_MAJOR/g" \
            -e "s/@DB_VERSION_MINOR@/$DB_VERSION_MINOR/g" \
            < $CONFIG_INPUT > $CONFIG_OUTPUT
	
	
	
        echo "Building for Visual Studio version $VERSION"
        for i in `cat $PROJECTS`
        do
            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.vcxproj
            TARG=$PROJECT_OUTPUT_DIR/${i}.vcxproj
            xqilla -o $TMP $TEMP_SCRIPT
            rm -f $TEMP_SCRIPT
            chmod 644 $TMP
            cmp $TMP $TARG > /dev/null 2>&1 ||
            (echo "Building $TARG" && rm -f $TARG &&
            cp $TMP $TARG)
            rm -f $TMP
        done
    done

    # Cleanup.
    rm -f $CONFIG_OUTPUT
}

create_projects_csharp()
{
    # project name list
    PROJECTS=$1

    # xquery script template
    TEMPLATE=genproject_csharp.template

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

    # xml document input template
    CONFIG_INPUT=$2

    # temporary xml document, post-sed-replacement
    CONFIG_OUTPUT=projects.xml
    
    VLoop="VS10 VS12 VS15"
    for v in $VLoop
    do
 	if [ $v = "VS10" ]; then
	    VERSION="10.0"
 	elif [ $v = "VS12" ]; then
	    VERSION="11.0"
	elif [ $v = "VS15" ]; then
	    VERSION="14.0"
        fi

        # substitute some variables in the XML document template
        sed -e "s/@DB_VERSION_MAJOR@/$DB_VERSION_MAJOR/g" \
            -e "s/@DB_VERSION_MINOR@/$DB_VERSION_MINOR/g" \
            -e "s/@VISUAL_STUDIO_VERSION@/$v/g" \
            < $CONFIG_INPUT > $CONFIG_OUTPUT

    	# for each project, substitute 2 variables in the XQuery script, then run it
    	for i in `cat $PROJECTS`
    	do
    	    # Split project path into dir + basename
 	    PROJECT_OUTPUT_DIR=`dirname $i`
 	    i=`basename $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/$v/$i.tmp.csproj
	    TARG=$PROJECT_OUTPUT_DIR/$v/${i}.csproj
            xqilla -o $TMP $TEMP_SCRIPT
            rm -f $TEMP_SCRIPT
            chmod 644 $TMP
            cmp $TMP $TARG > /dev/null 2>&1 ||
            (echo "Building $TARG" && rm -f $TARG &&
            cp $TMP $TARG)
            rm -f $TMP
        done
        # Cleanup.
        rm -f $CONFIG_OUTPUT
    done
}

# Detect XQilla
which xqilla  > /dev/null 2>&1
if [ $? -ne 0 ]; then
 	echo "Can not find xqilla command."
    	exit 1
fi

cd win_projects

# Create/Update Windows project files
echo "Building Visual Studio project files for Windows -- "
echo "   output only for modified projects (this can take a while)"
create_projects db.projects projects.template.xml ../../build_windows Win

# Create/Update C Sharp project files
echo "Building C Sharp project files -- "
echo "   output only for modified projects (this can take a while)"
create_projects_csharp db_csharp.projects projects_csharp.template.xml

cd ..

