<?xml version="1.0"?>
<!--
 This is a general Build-Script providing base functions to be used by
 all Java-resources Build-Scripts via jRuby.

-->
<project name="build-common-ruby_jar">

	<property environment="env"/>
	<property name="ant.setting.file"  value="${project.home}/../.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ant.core.prefs" />
	<property name="ruby_setting_file"  value="${project.home}/../.metadata/.plugins/org.eclipse.core.runtime/.settings/ruby.prefs" />

	<property file="${ruby_setting_file}" />

	<tstamp><format property="current.date" pattern="yyyy-MM-dd hh:mm:ss" unit="hour" /></tstamp>

	<path id="ant.lib.files">
		<fileset dir="${env.ANT_HOME}/lib"><include name="*.jar" /></fileset>
	</path>



	<target name="backup.eclipse.ant.core.setting">
		<echo message="making copy of ${ant.setting.file}"/>
		<copy file="${ant.setting.file}" tofile="${ant.setting.file}~" overwrite="false" failonerror="true"/>
    </target>

	<target name="replace.eclipse.ant.core.setting.patch" depends="backup.eclipse.ant.core.setting" >
		<replace file="${ant.setting.file}" summary="true" token="\\" value="\\\\"/>
		<replace file="${ant.setting.file}" summary="true" token="\:" value="\\:"/>
	</target>

	<target name="replace.eclipse.ant.core.setting.create" >
		<replace file="${ant.setting.file}" summary="true" token="\" value="\\"/>
		<replace file="${ant.setting.file}" summary="true" token=":" value="\:"/>
		<replace file="${ant.setting.file}" summary="true" token=";" value=","/>
	</target>


	<target name="create.eclipse.ant.core.setting" unless="ruby.patch" >
		<echo message="creating ${ant.setting.file}"/>
		<echo message="ANT_HOME: ${env.ANT_HOME}"/>

	<!-- don't touch, is an odd way to create line-breaks -->
<concat	append="false" destfile="${ant.setting.file}"># ${current.date}
</concat>
<concat	fixlastline="yes" append="true" destfile="${ant.setting.file}"># created by Stephan.mueller@id-media.com (eclipse-ant-ruby-patch)
</concat>
<concat	fixlastline="yes" append="true" destfile="${ant.setting.file}">ant_home_entries=${toString:ant.lib.files}
</concat>
<concat	fixlastline="yes" append="false" destfile="${ruby_setting_file}">ruby.patch=yes</concat>
<!-- don't touch, is an odd way to create line-breaks -->
	<antcall target="replace.eclipse.ant.core.setting.create"/>
	</target>



	<target name="patch.eclipse.ant.core.setting" depends="replace.eclipse.ant.core.setting.patch" unless="ruby.patch" >
		<property file="${ant.setting.file}" />
		<echo message="patching ${ant.setting.file}"/>
<!-- don't touch, is an odd way to create line-breaks -->
<concat	append="false" destfile="${ant.setting.file}"># ${current.date}
</concat>
<concat	fixlastline="yes" append="true" destfile="${ant.setting.file}"># created by Stephan.mueller@id-media.com (eclipse-ant-ruby-patch)
</concat>
<concat	fixlastline="yes" append="true" destfile="${ant.setting.file}">ant_home_entries=${ant_home_entries}${workspace_loc\:/xxx-libs/ant-import/lib/jruby.jar},${workspace_loc\:/xx-libs/ant-import/lib/bsf.jar},${workspace_loc\:/xxx-libs/xxx/runtime/commons-logging-1.0.4.jar}
</concat>
<concat	fixlastline="yes" append="false" destfile="${ruby_setting_file}">ruby.patch=yes</concat>
<!-- don't touch, is an odd way to create line-breaks -->
	</target>



	<target name="copy.ruby.libs" unless="ruby.patch">
		<echo message="copying missing ruby ant-libs to ${env.ANT_HOME}/lib"/>
		<copy todir="${env.ANT_HOME}/lib" overwrite="false">
			<fileset dir="${project.home}/../xxx-libs/ant-import/lib">
				<include name="*.jar" />
			</fileset>
			<fileset dir="${project.home}/../xxx/yyy/runtime">
				<include name="commons-logging-1.0.4.jar" />
			</fileset>
		</copy>
    </target>


	<target name="ruby.test.run" if="ruby.patch">
		<script language="ruby">
			print 'ruby is working on your workspace'
			File.open($ruby_setting_file, "a") {|f|
				f.write("\nruby.installed=true")
			}
		</script>
	 </target>

	<target name="install.ruby.config.file.exsits" if="ant_home_entries">
		<echo message="installation with existing config file"/>
		<antcall target="copy.ruby.libs"/>
		<antcall target="patch.eclipse.ant.core.setting"/>
		<antcall target="ruby.was.installed"/>
	</target>

	<target name="install.ruby.config.file.not.exsits" unless="ant_home_entries">
		<echo message="installation without existing config file"/>
		<antcall target="copy.ruby.libs"/>
		<antcall target="create.eclipse.ant.core.setting"/>
		<antcall target="ruby.was.installed"/>
	</target>

	<target name="install.ruby" unless="ruby.patch">
		<antcall target="install.ruby.config.file.exsits"/>
		<antcall target="install.ruby.config.file.not.exsits"/>
	</target>

	<target name="ruby.was.installed">
		<echo message="!!!----------------------------------------------------------!!!" />
		<echo message="!!! JRuby has been successfully installed on your workbench. !!!" />
		<echo message="!!!             You need to restart eclipse                  !!!" />
		<echo message="!!!----------------------------------------------------------!!!" />
		<fail message="Please restart eclipse"/>
	</target>

	<target name="ruby.is.installed" if="ruby.patch">
		<echo message="JRuby is installed on your workbench, be happy :)"/>
	</target>

	<target name="check.ruby.installation" unless="ruby.installed">
		<antcall target="ruby.is.installed"/>
		<antcall target="install.ruby"/>
		<antcall target="ruby.test.run"/>
	</target>

</project>
