<project name="Jmol releases" default="deploy" basedir="..">

  <!--
  
  6/2019 I can no longer get this script to run. It hangs on input, and I have tried
         everything I can think of. build.xml does accept input, so I have moved all this
         code to it.  BH
  
    To use this script, the scp task must be available.
    The scp task requires the jsch jar in the class path.
  -->

  <!-- Contributions -->
  <taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
      <pathelement location="tools/ant-contrib.jar" />
    </classpath>
  </taskdef>

	
    <input
      message="Please enter your username."
      addproperty="ssh.user"
    />

	
  <property name="sendZip"         value="true" />
  <property name="dist.dir"        value="build/dist" />
  <property name="src.dir"         value="src" />
  <property name="temp.dir"        value="build/release" />
  <property name="web.dir"         value="/home/frs/project/j/jm/jmol" />
  <property name="web.host"        value="frs.sourceforge.net" /> 
  <property name="prefix.release"  value="Jmol " />

  <echo message="${dist.dir}" />
	
<!--

12/17/2016	
Release Note: switching to semantic versioning (http://semver.org/)
Release Note: When releasing stable version, remove "-beta"
	
-->
	
	
  <property file="${dist.dir}/Jmol.properties" prefix="Jmol.properties" />
  <property name="version" value="${Jmol.properties.Jmol.___JmolVersion}" />
  <propertyregex property="version"
	  override = "true"
	  input="${version}"
	  regexp='(")([^"]+)(")'
  	  select="\2"
      global="true"
  />
  <echo message="version=${version}" />

	<loadresource property="versionMajorMinor">
	  <string value="${version}"/>
	  <filterchain>
	    <tokenfilter>
		      <replaceregex pattern="([0-9]+[.][0-9]+)(.*)" replace="\1"/>
	    </tokenfilter>
	  </filterchain>
	</loadresource>
	<echo message="versionMajorMinor: ${versionMajorMinor}"/>
	
	<loadresource property="versionMajorMinorPatch">
	  <string value="${version}"/>
	  <filterchain>
	    <tokenfilter>
		      <replaceregex pattern="([^-]*)(-beta)(.*)" replace="\1"/>
	    </tokenfilter>
	  </filterchain>
	</loadresource>
	<echo message="versionMajorMinorPatch: ${versionMajorMinorPatch}"/>

		<loadresource property="betaFlag">
	  <string value="${version}"/>
	  <filterchain>
	    <tokenfilter>
	      <replaceregex pattern="([^-]*)(-beta)(.*)" replace="\2"/>
	    </tokenfilter>
	  </filterchain>
	</loadresource>
	<property name="pathdir-full" value="Jmol/Version" />
  	<if>
     <equals arg1="${betaFlag}" arg2="${version}" />
  	 <then>
  		<property name="pathdir"    value="${pathdir-full}" />
    </then>
  	<else>
  		<property name="pathdir"    value="Jmol-beta/Jmol" />
  	</else>
  	</if>
	<echo message="betaFlag: ${betaFlag}"/>

	<property name="path.release"    value="${pathdir} ${versionMajorMinor}" />
	<echo message="path.release: ${path.release}"/>


	<!-- Copying files to website -->
  <target name="deploy" >

  	
    <!-- Ask user for confirmation -->

    <input
      message="Destination Path for ${version} (clear to exit)"
      defaultvalue="${path.release}/${prefix.release}${versionMajorMinorPatch}"
      addproperty="uploadPath"
    />

  	<!--
  	<input
      message="Filepath  ${uploadPath0} ?"
      validargs="y,n"
      defaultvalue="y"
      addproperty="do.scp"
    />
    <condition property="do.abort">
      <equals arg1="n" arg2="${do.scp}"/>
    </condition>
    <fail if="do.abort">Build aborted by user.</fail>
-->
    <!-- Ask user for username and password -->
    <input
      message="Please enter your password."
      addproperty="ssh.password"
    />

    <!-- Prepare temporary image -->
    <delete dir="${temp.dir}" />
    <mkdir dir="${temp.dir}" />
    <mkdir dir="${temp.dir}/${uploadPath}" />
  	
  	<if>
     <equals arg1="${sendZip}" arg2="true" />
  	 <then>
    <copy todir="${temp.dir}/${uploadPath}"
          file="${dist.dir}/Jmol-${version}-binary.zip" />
    <copy tofile="${temp.dir}/${uploadPath}/README-${version}.properties"
          file="${dist.dir}/Jmol.properties" />
    </then>
  	</if>
  	<if>
     <equals arg1="${pathdir}" arg2="${pathdir-full}" />
  	 <then>
	    <copy todir="${temp.dir}/${uploadPath}"
	          file="${dist.dir}/Jmol-${version}-binary.tar.gz" />
	    <copy todir="${temp.dir}/${uploadPath}"
	          file="${dist.dir}/Jmol-${version}-full.tar.gz" />
  	 </then>
  	</if>

    <!-- Do the copy using scp -->
    <echo message="Copying binary.zip to ${web.dir}" level="info"/>
    <scp todir="${ssh.user},jmol:${ssh.password}@${web.host}:${web.dir}"
         trust="yes" failonerror="true" verbose="true">
      <fileset dir="${temp.dir}">
        <include name="**/*.zip"/>
        <include name="**/*.properties"/>
      </fileset>
    </scp>
    <echo message="Copying binary.tar.gz to ${web.dir}" level="info"/>
    <scp todir="${ssh.user},jmol:${ssh.password}@${web.host}:${web.dir}"
         trust="yes" failonerror="true" verbose="true">
      <fileset dir="${temp.dir}">
        <include name="**/*-binary.tar.gz"/>
      </fileset>
    </scp>
    <echo message="Copying full.tar.gz to ${web.dir}" level="info"/>
    <scp todir="${ssh.user},jmol:${ssh.password}@${web.host}:${web.dir}"
         trust="yes" failonerror="true" verbose="true">
      <fileset dir="${temp.dir}">
        <include name="**/*-full.tar.gz"/>
      </fileset>
    </scp>

  	<!-- Cleanup -->
    <delete dir="${temp.dir}"/>
  </target>

</project>
