<project name="ekit" basedir="." default="install">
  <property environment="env"/>
  <property name="gsdl3srchome" value="${env.GSDL3SRCHOME}"/>

  <property name="html.path" value="/Configuration/Associations/Entry[@extension='html']"/>
  <property name="htm.path" value="/Configuration/Associations/Entry[@extension='htm']"/>
  <property name="html.text.path" value="${html.path}/text()"/>
  <property name="htm.text.path" value="${htm.path}/text()"/>
  <property name="ekit.assoc.entry" value="java -jar ${basedir}/ekit.jar -x -s -f%1"/>
  <property name="orig.html.file" value="${basedir}/htmlConfSettings.orig"/>
  <property name="orig.htm.file" value="${basedir}/htmConfSettings.orig"/>
  
  <path id="classpath">
    <fileset dir="${gsdl3srchome}/lib/java">
      <include name="**/*.jar"/>
    </fileset>
  </path>

  <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"  classpathref="classpath"/>
  <taskdef name="if" classname="ise.antelope.tasks.IfTask" classpathref="classpath"/>

  <target name="main">
    <if>
      <bool><equals arg1="${os.name}" arg2="Linux"/></bool>
      <property name="os" value="linux"/>
      
      <else>
	
	<if>
	  <bool><equals arg1="${os.name}" arg2="Mac OS X"/></bool>
	  <property name="os" value="mac"/>
	  
	  <else>
	  
	    <if>
	      <bool><or>
		  <equals arg1="${os.name}" arg2="Windows 95"/>
		  <equals arg1="${os.name}" arg2="Windows 98"/>
		  <equals arg1="${os.name}" arg2="Windows 2000"/>
		  <equals arg1="${os.name}" arg2="Windows XP"/>
		  <equals arg1="${os.name}" arg2="Windows NT"/>
		  <equals arg1="${os.name}" arg2="Windows ME"/>
		  <equals arg1="${os.name}" arg2="Windows Vista"/>
		  <equals arg1="${os.name}" arg2="Windows 7"/>
		</or></bool>
	      <property name="os" value="windows"/>
	    </if>

	  </else>
	</if>
      </else>
    </if>

    <if>
      <bool>
	<equals arg1="${os}" arg2="Windows"/>
      </bool>

      <property name="config.file" value="${user.home}/Application Data/Greenstone/GLI/config.xml"/>

      <else>
	<property name="config.file" value="${user.home}/.gli/config.xml"/>
      </else>
    </if>
  </target>

  <target name="install" depends="main">
    <if>
      <bool>
	<not><available file="${config.file}"/></not>
      </bool>
      <fail>Error associating EKIT with GLI, if you have not yet run GLI for the first time then do so and then try this again.</fail>
    </if>

    <xmltask source="${config.file}">
      <copy path="${html.text.path}" property="html.entry"/> 
      <copy path="${htm.text.path}" property="htm.entry"/> 
    </xmltask>

    <if>
      <bool>
	<not><equals arg1="${html.entry}" arg2="${ekit.assoc.entry}"/></not>
      </bool>

      <if>
	<bool>
	  <isset property="html.entry"/>
	</bool>

	<touch file="${orig.html.file}"/>
	<echo file="${orig.html.file}">${html.entry}</echo>
	
	<else>
	  <delete file="${orig.html.file}"/>
	</else>
      </if>
      
      <xmltask source="${config.file}" dest="${config.file}">
	<remove path="${html.text.path}"/>
	<insert path="${html.path}">${ekit.assoc.entry}</insert>
      </xmltask>
     
      <echo>EKIT has been associated with html files in GLI</echo>

      <else>
	<echo>EKIT is already associated with the html files in GLI</echo>
      </else>
    </if>
    
    <if>
      <bool>
	<not><equals arg1="${htm.entry}" arg2="${ekit.assoc.entry}"/></not>
      </bool>
      
      <if>
	<bool>
	  <isset property="htm.entry"/>
	</bool>

	<touch file="${orig.htm.file}"/>
	<echo file="${orig.htm.file}">${htm.entry}</echo>

	<else>
	  <delete file="${orig.htm.file}"/>
	</else>
      </if>

      <xmltask source="${config.file}" dest="${config.file}">
	<remove path="${htm.text.path}"/>
	<insert path="${htm.path}">${ekit.assoc.entry}</insert>
      </xmltask>

      <echo>EKIT has been associated with htm files in GLI</echo>

      <else>
	<echo>EKIT is already associated with the htm files in GLI</echo>
      </else>
    </if>
  </target>

  <target name="clean" depends="main">
    <xmltask source="${config.file}">
      <copy path="${html.text.path}" property="html.entry"/> 
      <copy path="${htm.text.path}" property="htm.entry"/>
    </xmltask>

    <if>
      <bool>
	<equals arg1="${html.entry}" arg2="${ekit.assoc.entry}"/>
      </bool>

      <if>
	<bool>
	  <available file="${orig.html.file}"/>
	</bool>

	<xmltask source="${config.file}" dest="${config.file}">
	  <remove path="${html.text.path}"/>
	  <insert path="${html.path}" file="${orig.html.file}"/>
	</xmltask>

	<delete file="${orig.html.file}"/>
	
	<else>
	  <xmltask source="${config.file}" dest="${config.file}">
	    <remove path="${html.text.path}"/>
	  </xmltask>
	</else>
      </if>
    </if>

    <if>
      <bool>
	<equals arg1="${htm.entry}" arg2="${ekit.assoc.entry}"/>
      </bool>

      <if>
	<bool>
	  <available file="${orig.htm.file}"/>
	</bool>

	<xmltask source="${config.file}" dest="${config.file}">
	  <remove path="${htm.text.path}"/>
	  <insert path="${htm.path}" file="${orig.htm.file}"/>
	</xmltask>

	<delete file="${orig.htm.file}"/>
	
	<else>
	  <xmltask source="${config.file}" dest="${config.file}">
	    <remove path="${htm.text.path}"/>
	  </xmltask>
	</else>
      </if>
    </if>    

  </target>
</project>
