#
#	$Id$
#
# Common functions used by both .dsp and .vcproj file generation
#

#
# Generate simple file list, matching on module name and optional filter
#
add_files()
{
    sources="$1"
    match="$2"
    fmatch="$3"
    srctemplate="$4"
    filelist="$5"
    nomatch="$6"
    
    for srcpath in `egrep "$match" $sources | egrep "$fmatch" | \
egrep -v "$nomatch" | 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;/;\\\\\\\\;g'`
	srcdir="$1"
	srcfile="$2"
        sed -e "s/@srcdir@/$srcdir/g" \
            -e "s/@srcfile@/$srcfile/g" \
            < $srctemplate >> $filelist
    done
}

#
# Generate the file list for the project into the output file specified.
# Include grouped/filtered files, if specified
#
create_file_list()
{
    projname="$1"       # name of the project/output file
    match="$2"          # the string used to egrep the $sources file
    sources="$3"        # a modified version of $SRCFILES to facilitate matches
    srctemplate="$4"    # template file for the src file fragments
    filelist="$5"         # output file for the list

    rm -f $filelist
    
    filtermatch="$match.filters="
    filters=`egrep $filtermatch $sources | sed -e "s/^$filtermatch//"`
    if [ "$filters" != "" ] ; then
	for filter in $filters
	do
	  # start a filter
	  sed -e "s/@filtname@/$filter/g" \
              < $FILT_START >> $filelist

	  # special-case "impl" subdirectories and add another level of
	  # grouping.  This mechanism is hard-coded to "impl" subdirectories,
	  # and will not work for other subdirectories.
	  implmatch="/$filter/impl"
	  doimpl=`egrep $implmatch $sources`
	  if [ "$doimpl" != "" ] ; then
	      sed -e "s/@filtname@/impl/g" \
		  < $FILT_START >> $filelist
	      add_files $sources $match $implmatch $srctemplate $filelist *
	      cat $FILT_END >> $filelist
	  else
	      implmatch="*"
	  fi
	  filtmatch="/$filter/"
	  # add files for that filter, removing any "impl" matches
	  add_files $sources $match $filtmatch $srctemplate $filelist $implmatch

	  # end the filter
	  cat $FILT_END >> $filelist
	done
    else
	  # no filter, just add files (filter match is *)
	  add_files $sources $match "*" $srctemplate $filelist *
    fi
}

#
# function to create an individual project file
#
create_project()
{
    projname="$1"       # name of the project (will be $projname.{dsp,vcproj})
    match="$2"          # the string used to egrep the $sources file
    sources="$3"        # a modified version of $SRCFILES to facilitate matches
    projtemplate="$4"   # template file for the project
    srctemplate="$5"    # template file for the src file fragments
    output=$6           # project output file name

    # destination targets for build components
    # OUTDIR must be defined in the driving script
    libDest="$OUTDIR/\$(ConfigurationName)"
    binRelDest="$OUTDIR/\$(ConfigurationName)"
    binDebugDest="$OUTDIR/\$(ConfigurationName)"
    jarDest="$OUTDIR"
    outdir="$OUTDIR"
    # incl is a placeholder for additional includes.
    # right now, it's redundant
    incl="../../include"

    filelist=$output.insert
    create_file_list $projname $match $sources \
	$srctemplate $filelist

    sed -e "/@SOURCE_FILES@/r$filelist" \
        -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_dest@!$jarDest!g" \
        -e "s!@outdir@!$outdir!g" \
        -e "s!@include@!$incl!g" \
        -e "s/@XQILLA_VERSION_MAJOR@/$XQILLA_VERSION_MAJOR/g" \
        -e "s/@XQILLA_VERSION_MINOR@/$XQILLA_VERSION_MINOR/g" \
        -f lib_paths.sed \
     < $projtemplate > $output.new

    # Set the file mode to 644 because the IDE needs a writeable file
    unix2dos $output.new
    cmp $output.new $output > /dev/null 2>&1 ||
    (echo "Building $output" && rm -f $output &&
	cp $output.new $output && chmod 664 $output)
    rm -f $filelist $output.new
}


generate_projects()
{
    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.  Eliminate ".filters" lines
    #
    MODULES="`egrep -v '.filters' $TMPA | sed -e 's/^[^ ]* //' \
    | tr ' ' '\012' | sort | uniq`"

    for module in $MODULES
    do
      case "$module" in
	  lib )
              create_project xqilla " $module " $TMPA \
		  $LIB_SRC $FILT_FILE_SRC $BUILDDIR/xqilla.$EXT
              ;;
	  dll=* )
	      dllname=`echo $module | sed -e 's/^dll=//'`
	      if [ -f $TEMPLATEDIR/$dllname.$EXT.src ] ; then
		  srcname=$TEMPLATEDIR/$dllname.$EXT.src
	      else 
		  srcname=$DLL_SRC
	      fi
              create_project $dllname " $module " $TMPA \
		  $srcname $FILE_SRC  $BUILDDIR/$dllname.$EXT
              ;;
	  app=* )
	      appname=`echo $module | sed -e 's/^app=//'`
	      if [ -f $TEMPLATEDIR/$appname.$EXT.src ] ; then
		  srcname=$TEMPLATEDIR/$appname.$EXT.src
	      else 
		  srcname=$APP_SRC
	      fi
              create_project $appname " $module " $TMPA \
		  $srcname $FILE_SRC $BUILDDIR/$appname.$EXT
              ;;
	  test=* )
	      appname=`echo $module | sed -e 's/^test=//'`
              create_project $appname " $module " $TMPA \
		  $TEST_SRC $FILE_SRC $BUILDDIR/$appname.$EXT
              ;;
	  * )
              echo "s_win32_common: module name $module in $SRCFILES is unknown type"
              ;;
      esac
    done
    rm -f $TMPA
}