<?xml version="1.0" encoding="UTF-8"?>
<project name="fabrik_updateserver" default="build_updateserver_xml">
	<!-- NOW THERES A PHP SCRIPT TO DO THIS.....subs.fabrikar.com/fabrik_downloads_update.php -->
<!-- 
	builds the individual plugin xml files which contain the update information.
	ftps up xml and plugin zips to server
 -->
	<target name="updater.testfiles">
		<dirset id="dist.contents" dir="${basedir}/plugins/fabrik_${folder}/" includes="*"/>
		<property name="prop.dist.contents" refid="dist.contents"/>
		<foreach list="${prop.dist.contents}" delimiter=";" param="plugin" target="updater.makefile" inheritall="true" inheritrefs="true"/>
		
		<antcall target="updater.makefile">
			 <param name="folder" value="system"/>
			<param name="plugin" value="fabrik"/>
		</antcall>
		
	</target>
	
	<target name="updater.makefile">
		<echo message=" updater.makefile ${update_source}plg_${folder}_${plugin}.xml" />
		<if>
			<available file="${update_source}plg_${folder}_${plugin}.xml"/>
				<then>
					<!--  found -->
					<xmltask source="${update_source}plg_${folder}_${plugin}.xml" dest="${update_source}plg_${folder}_${plugin}.xml"> 
						<remove path="/updates/update[version='${version}']" />
					</xmltask>
					<xmltask source="${update_source}plg_${folder}_${plugin}.xml" dest="${update_source}plg_${folder}_${plugin}.xml"> 
						<insert path="/updates"> 
							<![CDATA[	<update>
		<name>${update.product_name} ${folder}: ${plugin}</name>
		<description>${update.product_name} ${folder}: ${plugin}</description>
		<element>plg_${folder}_${plugin}</element>
		<type>plugin</type>
		<folder>${update.product_code}_${folder}</folder>
		<version>${version}</version>
		<downloads>
			<downloadurl type="full" format="zip">${update.download_folder}/plg_${update.product_code}_${folder}_${plugin}_${version}.zip</downloadurl>
		</downloads>
		<maintainer>${update.maintainer}</maintainer>
		<maintainerurl>${update.url}</maintainerurl>
		<targetplatform name="${update.target_platform}" version="${update.target_platform_version.dotted}" />
	</update>
]]>
						</insert> 
					</xmltask>
			</then>
			<else>
				<!--  not found create a new extension update xml file -->
					<xmltask dest="${update_source}plg_${folder}_${plugin}.xml"> 
						<insert path="/"  > 
							<![CDATA[<updates>
	<update>
		<name>${update.product_name} ${folder}: ${plugin}</name>
		<description>Fabrik ${folder}: ${plugin}</description>
		<element>plg_${folder}_${plugin}</element>
		<type>plugin</type>
		<folder>${update.product_code}_${folder}</folder>
		<version>${version}</version>
		<downloads>
			<downloadurl type="full" format="zip">${update.download_folder}/plg_${update.product_code}_${folder}_${plugin}_${version}.zip</downloadurl>
		</downloads>
		<maintainer>${update.maintainer}</maintainer>
		<maintainerurl>${update.url}</maintainerurl>
		<targetplatform name="${update.target_platform}" version="${update.target_platform_version.dotted}" />
	</update>
</updates>
]]>
						</insert> 
					</xmltask>
				</else>
    </if>
	</target>
	
	
	<target name="updater.ftp">
	
		<input message="Export XML to fabrik.com update server. This will allow users to see updates in the Joomla admin?" addproperty="updater.do" validargs="y,n" defaultvalue="n" />
		
		<if>
 			<equals arg1="${updater.do}" arg2="y" />
			<then>
				<!-- upload the modified extension updater files -->
				<ftp passive ="yes" server="${ftp.server}" userid="${ftp.user}" password="${ftp.password}" verbose="yes" remotedir="${ftp.updatedir}" retriesAllowed="3" depends="false" skipFailedTransfers="true">
					<fileset dir="${update_dir}">
						<include name="**/*.xml" />
					</fileset>
				</ftp> 
				<echo>uploaded update server xml files to ${ftp.updatedir}</echo>
			</then>
		</if>
		
		<input message="FTP extension zips to download folder ${ftp.downloaddir}?" addproperty="updater.ftp.zips" validargs="y,n" defaultvalue="y" />
		<if>
			<equals arg1="${updater.ftp.zips}" arg2="y" />
			<then>
				<antcall target="updater.downloaddb.add" />
				<!--  upload the plugin zips -->
				<ftp passive ="yes" server="${ftp.server}" userid="${ftp.user}" password="${ftp.password}" verbose="yes" remotedir="${ftp.downloaddir}" retriesAllowed="3" depends="false" skipFailedTransfers="true">
					<fileset dir="${cfg.destdir}/pkg_fabrik_sink/packages/">
						<include name="**/*.zip" />
					</fileset>
				</ftp>
				<echo>uploaded zip files to ${ftp.downloaddir}</echo>
				
			</then>
		</if>
		
		<input message="FTP Update server XML files and SQL updates to ${sql.downloads_table}?" addproperty="updater.ftp" validargs="y,n" defaultvalue="y" />
		<if>
			<equals arg1="${updater.ftp}" arg2="y" />
			<then>
				<antcall target="updater.downloaddb.add" />
				<!--  upload the plugin zips -->
				<ftp passive ="yes" server="${ftp.server}" userid="${ftp.user}" password="${ftp.password}" verbose="yes" remotedir="${ftp.downloaddir}" retriesAllowed="3" depends="false" skipFailedTransfers="true">
					<fileset dir="${cfg.destdir}/pkg_fabrik_sink/packages/">
						<include name="**/*.zip" />
					</fileset>
				</ftp>
				<echo>uploaded zip files to ${ftp.downloaddir}</echo>
				
			</then>
		</if>
		
	</target>
	
	<target name="updater.downloaddb.add">
		<for list="${plugin.folders}" param="plugin.type">
		
			 <sequential>
				 
            	<dirname property="plugins.@{plugin.type}" file="@{plugin.type}"/>
				<echo>@{plugin.type}</echo>
				<echo>${plugins.@{plugin.type}}</echo>
				
				<for list="${plugins.@{plugin.type}}" param="plugin.name">
					<sequential>
						<var name="filename" value="plg_fabrik_@{plugin.type}_@{plugin.name}_${version}.zip" />
						
						<!-- GET THE PLUGIN DESCRIPTION -->
						
						<echo>
							SELECT IFNULL(
        							(SELECT CONCAT('current.plugin.description=',`description`) from ${sql.downloads_table} WHERE 
					    		 	`download` LIKE "%plg_fabrik_@{plugin.type}_@{plugin.name}%" 
					    		 	AND description != ''
					    		 	AND `joomla_version` = "${update.target_platform_version}"
					    		 	ORDER BY create_date DESC LIMIT 1),
					    		 	'current.plugin.description=-')
						</echo>
							 
						<sql
							driver="${sql.driver}"
							url="${sql.url}"
							userid="${sql.user}"
							password="${sql.password}"
							expandProperties="true"
							output="temp.properties"
							showheaders="false" showtrailers="false"
							print="yes" >
							
								<transaction>
        						<![CDATA[
        						SELECT IFNULL(
        							(SELECT CONCAT('current.plugin.description=',`description`) from ${sql.downloads_table} WHERE 
					    		 	`download` LIKE "%plg_fabrik_@{plugin.type}_@{plugin.name}%" 
					    		 	AND description != '' AND description != ''
					    		 	AND `joomla_version` = "${update.target_platform_version}"
					    		 	ORDER BY create_date DESC LIMIT 1),
					    		 	'current.plugin.description=-')
        						]]>
        						</transaction>
        				</sql>
        				<var file="temp.properties" />
						<delete file="temp.properties" />
						
						<echo message="description: ${current.plugin.description}" />

						<sql
							driver="${sql.driver}"
							url="${sql.url}"
							userid="${sql.user}"
							password="${sql.password}"
							expandProperties="true"
							print="yes" >
			
		    	 
							<transaction><![CDATA[
								INSERT INTO ${sql.downloads_table} (`title`, `type`, `download`, `version`, `published`, `create_date`, `digg`, `acl`, `joomla_version`, `like`, `description`, `author`, `hits`, `slug`) 
					    	 VALUES ("@{plugin.type} - @{plugin.name} plug-in", "@{plugin.type}", "/media/downloads/${filename}", "${version}", "1", NOW(), "0", "19", "${update.target_platform_version}", "0", "${current.plugin.description}", "62", "0", "") 
					    	 ON DUPLICATE KEY UPDATE `description` = "${current.plugin.description}", download = "/media/downloads/${filename}", version = "${version}"
							]]>
							</transaction>
							
							<transaction>
							<![CDATA[
								UPDATE ${sql.downloads_table} SET `published` = "1" WHERE 
					    	 download = "/media/downloads/${filename}" AND version != "${version}" AND `joomla_version` = "${update.target_platform_version}"
							]]>
							</transaction>
						</sql>
					</sequential>
				</for>
			</sequential>
		</for>
		
		<!-- TODO: add component/package and modules/J plugins
		Update the entries so that only the main packages are sticky -->
		
		<sql
			driver="${sql.driver}"
			url="${sql.url}"
			userid="${sql.user}"
			password="${sql.password}"
			expandProperties="true"
			print="yes" >

		</sql>
			
	</target>
	
	<!-- NOW THERES A PHP SCRIPT TO DO THIS.....subs.fabrikar.com/fabrik_downloads_update.php -->
</project>