<?xml version="1.0" encoding="UTF-8"?>

<!--
	1. SvnAnt must be installed to ANT_HOME/lib from http://subclipse.tigris.org/svnant.html
	2. Ant-Contrib must be installed to ANT_HOME/lib from http://ant-contrib.sourceforge.ne
	3, ORO must be installed to ANT_HOME/lib
	4. "svn" must be in PATH
	5. "cvs" must be in PATH
	6. "p4" must be in PATH
	7. Make sure that the repo exist in SVN, CVS and P4
	7. Configure hook scripts for SVN, CVS and P4
	8. Configure commit acceptance rules
	9. Create the 4 preconfigured issues (see the issue.* properties below)
	10. Run "commitAll" and check the result log
-->
<project basedir="." default="commitAll">
	<taskdef name="svn" classname="org.tigris.subversion.svnant.SvnTask"/>
	<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

	<!-- configuration -->
	<property name="tempDir.path" location="/temp/jira-commitacceptance-test" />
	<property name="logfile.path" location="${tempDir.path}/log.txt" />
	<property name="commitMessage" value="John Doe!" />

	<property name="svn.repo.url" value="file:///new/testrepos/jira-svn-repo" />
	<property name="svn.workingCopy.dir" location="${tempDir.path}/svn-workingcopy" />
	<property name="svn.testFile.path" location="${svn.workingCopy.dir}/LICENSE.txt" />

	<property name="cvs.repo.cvsRoot" value="\new\testrepos\cvs-server-jira" />
	<property name="cvs.repo.package" value="x" />
	<property name="cvs.workingCopy.dir" location="${tempDir.path}/cvs-workingcopy" />
	<property name="cvs.testFile.path" location="${cvs.workingCopy.dir}/${cvs.repo.package}/file.txt" />

	<property name="p4.client" value="commitacceptance" />
	<property name="p4.testFile.depotPath" value="LICENSE.txt" />
	<property name="p4.workingCopy.dir" location="${tempDir.path}/p4-workingcopy" />
	<property name="p4.testFile.path" location="${p4.workingCopy.dir}\${p4.testFile.depotPath}" />

	<property name="issue.nonExisting" value="TST-0"></property>
	<property name="issue.unresolvedUnassigned" value="TST-1"></property><!-- can be assigned to a different user -->
	<property name="issue.unresolvedAssigned" value="TST-2"></property>
	<property name="issue.resolvedUnassigned" value="TST-3"></property><!-- can be assigned to a different user -->
	<property name="issue.resolvedAssigned" value="TST-4"></property>
	<property name="issue.anotherProject" value="ARP-1"></property><!-- can be anything, but must be valid -->

	<target name="init">
		<!-- checkout SVN working copy -->
		<if>
			<available file="${svn.testFile.path}" />
			<then>
				<echo>SVN working copy exists.</echo>
			</then>
			<else>
				<mkdir dir="${svn.workingCopy.dir}" />
				<svn>
					<checkout url="${svn.repo.url}" destPath="${svn.workingCopy.dir}" />
				</svn>
			</else>
		</if>
		<!-- checkout CVS working copy -->
		<if>
			<available file="${cvs.testFile.path}" />
			<then>
				<echo>CVS working copy exists.</echo>
			</then>
			<else>
				<mkdir dir="${cvs.workingCopy.dir}" />
				<cvs command="checkout" cvsroot="${cvs.repo.cvsRoot}" package="${cvs.repo.package}" dest="${cvs.workingCopy.dir}" />
			</else>
		</if>
		<!-- checkout P4 working copy -->
		<if>
			<available file="${p4.testFile.path}" />
			<then>
				<echo>P4 working copy exists.</echo>
			</then>
			<else>
				<mkdir dir="${p4.workingCopy.dir}" />
				<p4sync />
			</else>
		</if>
	</target>

	<target name="commit" depends="init" description="TODO">
		<!-- commit to SVN -->
		<buildnumber file="${svn.testFile.path}"/>
		<svn>
			<status path="${svn.testFile.path}" revisionProperty="svn.oldRevision" />
		</svn>
		<exec executable="svn"><!-- commandline SVN executable is used to commit as SvnAnt can treat with commit hooks?! -->
			<arg value="commit" />
			<arg value="-m" />
			<arg value="${commitMessage}" />
			<arg value="${svn.testFile.path}" />
		</exec>
		<svn>
			<status path="${svn.testFile.path}" revisionProperty="svn.newRevision" />
		</svn>
		<!-- commit to CVS -->
		<buildnumber file="${cvs.testFile.path}"/>
		<exec dir="${cvs.workingCopy.dir}/${cvs.repo.package}" executable="cvs" resultproperty="cvsResultCode"><!-- commandline CVS executable is used to commit as the <cvs> task had problems with the current dir -->
			<arg value="-d"/>
			<arg value="${cvs.repo.cvsRoot}"/>
			<arg value="commit"/>
			<arg value="-m"/>
			<arg value="${commitMessage}"/>
		</exec>
		<!-- commit to P4 -->
		<p4change description="${commitMessage}" />
		<p4edit change="${p4.change}" view="${p4.testFile.path}" />
		<p4reopen tochange="${p4.change}" view="${p4.testFile.path}" />
		<buildnumber file="${p4.testFile.path}" />
		<exec executable="p4" resultproperty="p4ResultCode"><!-- commandline P4 executable is used to commit as p4submit task does not return any result code -->
			<arg value="-c" />
			<arg value="${p4.client}" />
			<arg value="submit" />
			<arg value="-c" />
			<arg value="${p4.change}" />
		</exec>
		<!-- log -->
		<if>
			<equals arg1="${svn.newRevision}" arg2="${svn.oldRevision}" />
			<then>
				<echo file="${logfile.path}" append="true">REJECTED  </echo>
			</then>
			<else>
				<echo file="${logfile.path}" append="true">Accepted  </echo>
			</else>
		</if>
		<if>
			<not>
				<equals arg1="${cvsResultCode}" arg2="0" />
			</not>
			<then>
				<echo file="${logfile.path}" append="true">REJECTED  </echo>
			</then>
			<else>
				<echo file="${logfile.path}" append="true">Accepted  </echo>
			</else>
		</if>
		<if>
			<not>
				<equals arg1="${p4ResultCode}" arg2="0" />
			</not>
			<then>
				<echo file="${logfile.path}" append="true">REJECTED  </echo>
			</then>
			<else>
				<echo file="${logfile.path}" append="true">Accepted  </echo>
			</else>
		</if>

		<echo file="${logfile.path}" append="true">'${commitMessage}'${line.separator}</echo>
	</target>

	<target name="commitAll" depends="init" description="TODO">
		<delete file="${logfile.path}"></delete>

		<antcall target="commit"><param name="commitMessage" value="No issues" /></antcall>
		<antcall target="commit"><param name="commitMessage" value="1 nonExisting:                                 ${issue.nonExisting}" /></antcall>
		<antcall target="commit"><param name="commitMessage" value="1 nonExisting + 1 unresolvedUnassigned:        ${issue.nonExisting} ${issue.unresolvedUnassigned}" /></antcall>
		<antcall target="commit"><param name="commitMessage" value="1 unresolvedUnassigned:                        ${issue.unresolvedUnassigned}" /></antcall>
		<antcall target="commit"><param name="commitMessage" value="2 unassigned:                                  ${issue.unresolvedUnassigned} ${issue.resolvedUnassigned}" /></antcall>

		<antcall target="commit"><param name="commitMessage" value="1 unresolvedUnassigned:                        ${issue.unresolvedUnassigned}" /></antcall>
		<antcall target="commit"><param name="commitMessage" value="1 unresolvedUnassigned + 1 resolvedUnassigned: ${issue.unresolvedUnassigned} ${issue.resolvedUnassigned}" /></antcall>
		<antcall target="commit"><param name="commitMessage" value="1 resolvedUnassigned:                          ${issue.resolvedUnassigned}" /></antcall>
		<antcall target="commit"><param name="commitMessage" value="2 resolved:                                    ${issue.resolvedUnassigned} ${issue.resolvedAssigned}" /></antcall>

		<antcall target="commit"><param name="commitMessage" value="1 unresolvedUnassigned:                        ${issue.unresolvedUnassigned}" /></antcall>
		<antcall target="commit"><param name="commitMessage" value="1 unresolvedUnassigned + 1 unresolvedAssigned: ${issue.unresolvedUnassigned} ${issue.unresolvedAssigned}" /></antcall>
		<antcall target="commit"><param name="commitMessage" value="1 unresolvedAssigned:                          ${issue.unresolvedAssigned}" /></antcall>
		<antcall target="commit"><param name="commitMessage" value="2 assigned:                                    ${issue.unresolvedAssigned} ${issue.resolvedAssigned}" /></antcall>

		<antcall target="commit"><param name="commitMessage" value="1 anotherProject:                              ${issue.anotherProject}" /></antcall>
		<antcall target="commit"><param name="commitMessage" value="1 unresolvedUnassigned + 1 anotherProject:     ${issue.unresolvedUnassigned} ${issue.anotherProject}" /></antcall>

		<loadfile property="log" srcfile="${logfile.path}" />
		<echo>--------------------------------------------------------------------------------</echo>
		<echo>SVN       CVS       P4        Message</echo>
		<echo>--------------------------------------------------------------------------------</echo>
		<echo>${log}</echo>
	</target>
</project>
