<?xml version="1.0" encoding="ISO-8859-1"?>
<!--

    gvSIG. Desktop Geographic Information System.

    Copyright (C) 2007-2013 gvSIG Association.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 3
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA  02110-1301, USA.

    For any additional information, do not hesitate to contact us
    at info AT gvsig.com, or visit our website www.gvsig.com.

-->
<project name="gvSIG-make-maven-project" default="mkproject" basedir=".">

	<!-- Get current file location folder -->
	<dirname property="gvSIG-make-maven-project.basedir" file="${ant.file.gvSIG-make-maven-project}" />

	<!-- Libraries folder in the gvSIG extension -->
	<property name="lib.folder" location="${gvSIG-make-maven-project.basedir}/../lib" />
	<!-- Templates folder in the gvSIG extension -->
	<property name="templates.folder" location="${gvSIG-make-maven-project.basedir}/../templates" />

	<property name="gvsiglogo" location="${basedir}/../gvSIG.png" />

	<!-- Load some ant external utility tasks -->
	<property name="antform.lib" location="${lib.folder}/antform-2.0.jar" />
	<property name="antcontrib.lib" location="${lib.folder}/ant-contrib-1.0b3.jar" />
	<property name="antelope.lib" location="${lib.folder}/antelopetasks-3.2.10.jar" />

	<property name="form.width" value="700" />
	<property name="form.height" value="550" />
	<property name="form.columns" value="40" />


	<taskdef resource="net/sf/antcontrib/antlib.xml">
		<classpath>
			<pathelement location="${antcontrib.lib}" />
		</classpath>
	</taskdef>

	<taskdef name="antform" classname="com.sardak.antform.AntForm" classpath="${antform.lib}" />

	<taskdef name="antmenu" classname="com.sardak.antform.AntMenu" classpath="${antform.lib}" />

	<taskdef name="stringutil" classname="ise.antelope.tasks.StringUtilTask" classpath="${antelope.lib}" />


	<!-- 
	================ begin specific code of templates ================= 
	-->
	<property name="template.fortunecookies-basic.option" value="2 - General project based on Fortune cookies basic example project" />
	<property name="template.fortunecookies-basic.description1" value="Create a general pourpose project with the multimodule" />
	<property name="template.fortunecookies-basic.description2" value="structure. It has no GEO logic and is based on a basic" />
	<property name="template.fortunecookies-basic.description3" value="API/implementation separation" />

	<!--
    <property name="template.fortunecookies-provider.option" value="3 - General project based on Fortune cookies example project" />
    <property name="template.fortunecookies-provider.description1" value="Create a general pourpose project with the multimodule" />
    <property name="template.fortunecookies-provider.description2" value="structure. It has no GEO logic and is based on" />
    <property name="template.fortunecookies-provider.description3" value="API/SPI/implementation separation and providers"  />
	-->
	<property name="template.landregistryviewer.option" value="1 - Basic plugin with spatial support" />
	<property name="template.landregistryviewer.description1" value="Create a project based in the LandRegistryViewer example." />
	<property name="template.landregistryviewer.description2" value="Uses access to View, MapControl and datasources." />
	<property name="template.landregistryviewer.description3" value="" />

	<!--
	<property name="templates" value="fortunecookies-basic,fortunecookies-provider,landregistryviewer" />
	-->
	<property name="templates" value="fortunecookies-basic,landregistryviewer" />
	<!-- 
	================ End specific code of templates ================= 
	-->

	<target name="mkproject" depends="mkproject-prompt-basic-data">
		<description>Creates a new gvSIG project</description>
	</target>

	<target name="mkproject-prompt-basic-data">

		<property name="project.group.id" value="org.gvsig" />

		<antform title="Create project" width="${form.width}" image="${gvsiglogo}">
			<textProperty label="" editable="false" columns="${form.columns}" property="form.title" />
			<textProperty label="Name : " property="project.name" required="true" />
			<label>                               For the project name, use the java class naming rules. 
                               Ex: FortuneCookie or LandRegistryViewer</label>
			<textProperty label="Group Id : " property="project.group.id" required="true" />

			<fileSelectionProperty label="Create project in : " property="project.folder" directoryChooser="true" editable="false" required="true" />
			<separator />
			<label>Select the template to use:</label>

			<!-- 
			================ begin specific code of templates ================= 
			-->
			<checkSelectionProperty property="template.landregistryviewer.check" values="${template.landregistryviewer.option}" label="" />
			<label>                                         ${template.landregistryviewer.description1}
                                         ${template.landregistryviewer.description2}
                                         ${template.landregistryviewer.description3}</label>

			<checkSelectionProperty editable="true" property="template.fortunecookies-basic.check" values="${template.fortunecookies-basic.option}" label="" />
			<label>                                         ${template.fortunecookies-basic.description1}
                                         ${template.fortunecookies-basic.description2}
                                         ${template.fortunecookies-basic.description3}</label>
			<!--
            <checkSelectionProperty editable="false" property="template.fortunecookies-provider.check" values="${template.fortunecookies-provider.option}" label="" />
            <label>                                         ${template.fortunecookies-provider.description1}
                                         ${template.fortunecookies-provider.description2}
                                         ${template.fortunecookies-provider.description3}</label>
            -->
			<!-- 
			================ End specific code of templates ================= 
			-->
			<separator />
			<controlbar>
				<button type="cancel" label=" Cancel " target="mkproject-build-cancelled" />
				<button type="ok" label=" Next " target="mkproject-fixnames" />
			</controlbar>
		</antform>
	</target>

	<target name="mkproject-fixnames">

		<!-- Trim project name and group id -->
		<stringutil string="${project.name}" property="project.name">
			<trim />
		</stringutil>
		<stringutil string="${project.group.id}" property="project.group.id">
			<trim />
		</stringutil>

		<!-- Lower case project name -->
		<stringutil string="${project.name}" property="project.name.lowercase">
			<lowercase />
		</stringutil>

		<!-- Capitalize project name, just in case -->
		<stringutil string="${project.name}" property="project.name.capitalized.end">
			<substring beginindex="1" />
		</stringutil>
		<stringutil string="${project.name}" property="project.name.capitalized.beginning">
			<substring endindex="1" />
			<uppercase />
		</stringutil>
		<property name="project.name.capitalized" value="${project.name.capitalized.beginning}${project.name.capitalized.end}" />

		<!-- Create artifactID with project.group.id + . + project.name.lowercase -->
		<property name="project.artifact.id" value="${project.group.id}.${project.name.lowercase}" />

		<for list="${templates}" param="template">
			<sequential>
				<var name="template.name" value="@{template}" />
				<var name="check.name" value="template.${template.name}.check" />
				<propertycopy override="true" name="arg1" from="${check.name}" />
				<propertycopy override="true" name="arg2" from="template.${template.name}.option" />
				<if>
					<and>
						<isset property="selected.template" />
						<equals arg1="${arg1}" arg2="${arg2}" />
					</and>
					<then>
						<antcall target="fail">
							<param name="message" value="Only one template can be selected" />
						</antcall>
					</then>
				</if>
				<if>
					<equals arg1="${arg1}" arg2="${arg2}" />
					<then>
						<echo>selected.template = "${template.name}"</echo>
						<property name="selected.template" value="${template.name}" />
					</then>
				</if>
			</sequential>
		</for>
		<antcall target="mkproject-confirm" inheritall="true" />
	</target>

	<target name="mkproject-confirm">
		<antform title="Confirm the creation" width="${form.width}" image="${gvsiglogo}">
			<textProperty label="" editable="false" columns="${form.columns}" property="form.title" />
			<label>The data used for the creation of project are:</label>
			<textProperty label="Project" editable="false" property="project.name" />
			<textProperty label="Project name capitalized" editable="false" property="project.name.capitalized" />
			<textProperty label="Project name lowercase" editable="false" property="project.name.lowercase" />
			<textProperty label="Group Id" editable="false" property="project.group.id" />
			<textProperty label="Artifact Id" editable="false" property="project.artifact.id" />
			<textProperty label="Folder" editable="false" property="project.folder" />
			<textProperty label="Template" editable="false" property="selected.template" />
			<label>Continue ?</label>
			<separator />
			<controlbar>
				<button type="cancel" label=" Cancel " target="mkproject-build-cancelled" />
				<button type="ok" label=" Previous " target="mkproject-prompt-basic-data" />
				<button type="ok" label=" Next " target="mkproject-build" />
			</controlbar>
		</antform>
	</target>

	<target name="mkproject-build">
		<echo>
  Project name: "${project.name}"
  Project name capitalized: "${project.name.capitalized}"
  Project name lowercase: ${project.name.lowercase}"
  Group id: "${project.group.id}"
  ArtifactId: "${project.artifact.id}"
  Project folder: "${project.folder}"
  Selected template: "${selected.template}"
        </echo>
		<!--  call specific code of the selected template -->
		<ant antfile="${selected.template}.xml" inheritall="false">
			<property name="project.name" value="${project.name}" />
			<property name="project.name.capitalized" value="${project.name.capitalized}" />
			<property name="project.name.lowercase" value="${project.name.lowercase}" />
			<property name="project.group.id" value="${project.group.id}" />
			<property name="project.artifact.id" value="${project.artifact.id}" />
			<property name="project.folder" value="${project.folder}" />
			<property name="form.width" value="${form.width}" />
			<property name="form.height" value="${form.height}" />
			<property name="form.columns" value="${form.columns}" />
		</ant>
		<if>
			<resourceexists>
				<file file="${project.folder}/${project.artifact.id}" />
			</resourceexists>
			<then>
				<antcall target="mkproject-prepare-workspace" inheritall="true" />
				<antcall target="mkproject-build-succesfully" inheritall="true" />
			</then>
			<else>
				<antcall target="mkproject-build-cancelled" inheritall="true" />
			</else>
		</if>
	</target>

	<target name="mkproject-prepare-workspace">
	  <!--
		<ant dir="${project.folder}/${project.artifact.id}" antfile="prepare-workspace.xml" target="prepare-workspace" />
		<if>
			<resourceexists>
				<file file="${project.folder}/${project.artifact.id}.app" />
			</resourceexists>
			<then>
				<ant dir="${project.folder}/${project.artifact.id}.app" antfile="../org.gvsig.maven.base.build/maven-goals.xml" target="mvn-install-and-eclipse-eclipse" />
			</then>
		</if>
		-->
	</target>

	<target name="mkproject-build-succesfully">
		<antform title="Project creation" width="${form.width}" image="${gvsiglogo}">
			<textProperty label="" editable="false" columns="${form.columns}" property="form.title" />
			<label>Project ${project.name} was created succesfully</label>
			<controlbar>
				<button type="ok" label=" Close " />
			</controlbar>
		</antform>
	</target>

	<target name="mkproject-build-cancelled">
		<antform title="Project creation cancelled" width="${form.width}" image="${gvsiglogo}">
			<textProperty label="" editable="false" columns="40" property="form.title" />
			<label>Creation cancelled.</label>
			<separator />
			<controlbar>
				<button type="cancel" label=" Close " />
			</controlbar>
		</antform>
	</target>

	<target name="fail">
		<echo>${message}</echo>
		<fail message="${message}" />
	</target>

</project>