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

<project name="athena-project-build" default="deploy">

  <!-- Load Tasks -->
  <taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
      <pathelement location="${wpilib.ant.dir}/ant-contrib.jar"/>
    </classpath>
  </taskdef>
  <taskdef resource="net/jtools/classloadertask/antlib.xml" classpath="${classloadertask.jar}"/>
  <classloader loader="system" classpath="${jsch.jar}"/>

  <target name="clean" description="Clean up all build and distribution artifacts.">
    <delete dir="${build.dir}"/>
    <delete dir="${dist.dir}"/>
  </target>

  <!-- Targets -->

  <target name="get-target-ip">
    <property name="target" value="roboRIO-${team-number}.local" />
	<echo>Trying Target: ${target}</echo>
	 <if>
		<isreachable host="${target}"/>
	  <then>
		<echo>roboRIO found via mDNS</echo>
	  </then>
	  <else>
		<math result="ip.upper" operand1="${team-number}" operation="/" operand2="100" datatype="int"/>
		<math result="ip.lower" operand1="${team-number}" operation="%" operand2="100" datatype="int"/>
		<property name="target" value="10.${ip-upper}.${ip.lower}.2"/>
		<echo>roboRIO not found via mDNS, falling back to static address of ${target}</echo>
		<assert name="roboRIOFound" message="roboRIO not found, please check that the roboRIO is connected, imaged and that the team number is set properly in Eclipse">
			<isreachable host="${target}"/>
		</assert>
	  </else>
	 </if>
  </target>

  <target name="deploy" depends="get-target-ip, dependencies" description="Deploy the progam and start it running.">
    <sshexec host="${target}"
         username="${username}"
         password="${password}"
         trust="true"
         failonerror="no"
         command="rm ${deploy.dir}/FRCUserProgram" />

    <echo>[athena-deploy] Copying code over.</echo>
    <scp file="${out.exe}" sftp="true" todir="${username}@${target}:${deploy.dir}" password="${password}" trust="true"/>
    <scp file="${wpilib.ant.dir}/robotCommand" todir="${username}@${target}:/home/lvuser/" password="${password}" trust="true"/>

    <echo>[athena-deploy] Starting program.</echo>
    <sshexec host="${target}"
       username="${username}"
       password="${password}"
       trust="true"
       command=". /etc/profile.d/natinst-path.sh; chmod a+x ${deploy.dir}/${out}; ${deploy.kill.command};"/>

  </target>
  
  <target name="kill-program" depends="get-target-ip" description="Kill the currently running FRC program">
	  <sshexec host="${target}"
		   username="${username}"
		   password="${password}"
		   trust="true"
		   command="/usr/local/frc/bin/frcKillRobot.sh"/>
  </target>

  <!-- Simulation support -->
  <target name="simulate">
    <parallel>
      <sequential>
	    <echo>[simulate] Running Gazebo.</echo>
	    <exec executable="frcsim">
	      <arg value="${simulation.world.file}"/>
	    </exec>
      </sequential>
      <sequential>
        <sleep seconds="5"/>
	    <echo>[simulate] Running DriverStation.</echo>
	    <java jar="${sim.tools}/SimDS.jar" fork="true">
          <jvmarg value="-Djava.library.path=${sim.lib}" />
        </java>
      </sequential>
      <sequential>
        <sleep seconds="5"/>
	    <echo>[simulate] Running Code.</echo>
	    <exec executable="${sim.exe}"></exec>
      </sequential>
    </parallel>
  </target>
  
  <target name="dependencies" depends="get-target-ip">
    <property name="ant.enable.asserts" value="true"/>
	<post to="http://${target}/nisysapi/server" logfile="sysProps.xml" verbose="false" encoding="UTF-16LE" append="false">
		<prop name="Function" value="GetPropertiesOfItem"/>
		<prop name="Plugins" value="nisyscfg"/>
		<prop name="Items" value="system"/>
	</post>
	<loadfile srcFile="sysProps.xml" encoding="UTF-16LE" property="roboRIOSysValues"/>
	<propertyregex property="roboRIOImage" input="${roboRIOSysValues}" regexp="FRC_roboRIO_2015_v([0-9]+)" select="\1" defaultValue="ImageRegExFail"/>
	<assert message="roboRIO Image does not match plugin, allowed image version: ${roboRIOAllowedImages}">
		<bool>
			<contains string="${roboRIOAllowedImages}" substring="${roboRIOImage}"/>
		</bool>
	</assert>
  </target>
</project>
