#!/bin/sh
# script relies on GNU versions of find, xargs, and fgrep (using their options
# for NUL-delimited file names).

usage () {
  echo >&2 "usage: $0   # run in the top directory (OPENVMDIR) of your checkout"
  exit 1
}

if ! test -f bin/installLaunchConfigs.sh -a -d src
then
  echo >&2 "This doesn't look like the top of an Ovm checkout: `pwd`"
  usage
fi

launchesDir="${WORKSPACE:-..}/.metadata/.plugins/org.eclipse.debug.core/.launches"

if ! test -d "$launchesDir"
then
  echo >&2 "This doesn't look like a checkout in an Eclipse workspace:"
  echo >&2 "WORKSPACE=${WORKSPACE:-..} directory $launchesDir not found"
  usage
fi

myProj=`basename "\`pwd\`"`

deletionKey="<!-- OvmOvm Project $myProj Launcher OvmOvm -->"

find "$launchesDir" -maxdepth 1 -name '*.launch' -type f -print0  | {
  xargs -0r fgrep -lZ "$deletionKey"
  find "$launchesDir" -maxdepth 1 \
       -name "Build $myProj Image (*endian).launch" -print0
  }  |
  xargs -0tr rm

# redhat /bin/sh is braindamaged and hangs on foo=`cat` <<here
# but works if I just exec stdin to the here document and cat it; and that
# also works in working shells, so I'll do it that way.

exec <<\BurmaShave
<?xml version="1.0" encoding="UTF-8"?>
DELETIONKEY
<launchConfiguration
 type="org.eclipse.jdt.launching.localJavaApplication">
 <booleanAttribute
  key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH"
  value="false"/>
 <stringAttribute
  key="org.eclipse.jdt.launching.MAIN_TYPE"
  value="s3.services.bootimage.Driver"/>
 <stringAttribute
  key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS"
  value="CONFIG"/>
 <listAttribute key="org.eclipse.jdt.launching.CLASSPATH">
  <listEntry
   value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  	  &lt;runtimeClasspathEntry
  	      containerPath=&quot;org.eclipse.jdt.launching.JRE_CONTAINER&quot;
  	      path=&quot;2&quot;
  	      type=&quot;4&quot;/&gt; "/>
  <listEntry
   value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  	  &lt;runtimeClasspathEntry
  	      internalArchive=&quot;/PROJECTNAME/config&quot;
  	      path=&quot;3&quot;
  	      type=&quot;2&quot;/&gt; "/>
  <listEntry
   value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  	  &lt;runtimeClasspathEntry
  	      internalArchive=&quot;/PROJECTNAME/src&quot;
  	      path=&quot;3&quot;
  	      type=&quot;2&quot;/&gt; "/>
  <listEntry
   value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  	  &lt;runtimeClasspathEntry
  	      internalArchive=&quot;/PROJECTNAME/lib/aspectjrt.jar&quot;
  	      path=&quot;3&quot;
  	      type=&quot;2&quot;/&gt; "/>
  <listEntry
   value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  	  &lt;runtimeClasspathEntry
  	      internalArchive=&quot;/PROJECTNAME/src/syslib/user/build/ovm_rt_user.jar&quot;
  	      path=&quot;3&quot;
  	      type=&quot;2&quot;/&gt; "/>
 </listAttribute>
 <stringAttribute 
  key="org.eclipse.jdt.launching.PROJECT_ATTR"
  value="PROJECTNAME"/>
 <stringAttribute
  key="org.eclipse.jdt.launching.VM_ARGUMENTS"
  value="-Xmx512M"/>
 <stringAttribute
  key="org.eclipse.debug.ui.target_debug_perspective"
  value="perspective_default"/>
 <stringAttribute
  key="org.eclipse.jdt.launching.WORKING_DIRECTORY"
  value="PROJECTNAME"/>
 <stringAttribute
  key="org.eclipse.debug.ui.target_run_perspective"
  value="perspective_default"/>
 <stringAttribute
  key="org.eclipse.debug.core.source_locator_id"
  value="org.eclipse.jdt.debug.ui.javaSourceLocator"/>
</launchConfiguration>
BurmaShave

xmlText=`cat`

#interpreter jit jit-debug j2c "j2c -j2c-prof" "j2c -opt=run"

engines="interpreter simplejit j2c"
models="JMTk-B_M_J_H
	MostlyCopying-B_M_F_H 
	MostlyCopyingRegions-B_M_F_H
	SimpleSemiSpace-B_M_F_H
	minimalMM-B_M"
threadservices="JVMConfigurator	RealtimeJVMConfigurator"
ioservices="SelectSockets_PollingOther_IOServiceConfigurator"

for engine in $engines
do
  config_e="-engine=$engine"
  cfg_name_e="$myProj $engine"
    for model in $models
    do
      config_em="$config_e -model=$model"
      cfg_name_em="$cfg_name_e|$model"
      for svc_cfg in $threadservices
      do
	config_ems="$config_em -threadserviceconfigurator=$svc_cfg"
	cfg_name_ems="$cfg_name_em|$svc_cfg"
	for ios in $ioservices
	do
	  config_emsi="$config_ems -ioserviceconfigurator=$ios"
	  cfg_name_emsi="$cfg_name_ems|$ios"
          echo "$xmlText"  |
            sed >"$launchesDir/$cfg_name_emsi Image.launch" \
	      's/^DELETIONKEY$/'"$deletionKey"'/
	      s/PROJECTNAME/'"$myProj"'/
              s/CONFIG/'"$config_emsi"/
      done
    done
  done
done

