<?xml version="1.0" encoding="UTF-8"?>
<project name="xindex updator" default="all" basedir=".">
    <property name="index" location="index"/>
    <property name="work" location="work"/>
    <property name="indexbackup"  location="indexbackup"/> 
    <property name="mail-server"  value="192.168.11.232"/>  
    <property name="mail-port"  value="25"/>
    <property name="mail-user"  value="abc@pconline.com.cn"/>
    <property name="mail-passwd"  value="abc"/>
    <property name="mail-list"  value="chenchongqi@pconline.com.cn"/>
    <tstamp>
        <format property="TODAY" pattern="yyyy-MMMM-dd-HH-mm" locale="en"/>
    </tstamp>
    <fileset id="updateFiles" dir="${work}" >
      <include name="*.txt"/>      
    </fileset>
    <echo>Target time: ${TODAY}</echo>

    <target name="check-update">
        <condition property="update.files">
            <resourcecount refid="updateFiles" when="greater" count="0" />                   
        </condition>    
    </target>
    
    <target name="backup" depends="check-update" if="update.files">
        <echo>Backup index files ....</echo>
        <zip destfile="${indexbackup}/bak${TODAY}.zip"
            basedir="${index}"
            includes="*.txt"
        />
    </target>
           
    <target name="flush-cmd" depends="backup" if="update.files">
	<exec executable="bash" outputproperty="blah" >
		<arg value="-c"/>
                <arg value="echo flush_all | nc localhost 11311"/>
        </exec>
	<condition property="flush.fail">
		<and>
			<not>
				<equals arg1="OK" arg2="${blah}" forcestring="true"/>
			</not>
			<istrue value="${update.files}"/>
		</and>		
	</condition>
        <echo>Flush command return:${blah}</echo>
    </target>    

    <target name="send-mail">
    	<echo>send alert mail</echo>
	<mail mailhost="${mail-server}" mailport="${mail-port}" subject="${title}"
		user="${mail-user}" password="${mail-passwd}" failonerror="false">
		<from address="${mail-user}"/>
  		<to address="${mail-list}"/>
  		<message>${message}</message>
	</mail>
    </target>

    <target name="flush" depends="flush-cmd" if="flush.fail">
	<antcall target="send-mail">
		<param name="title" value="Flush command failed"/>
		<param name="message" value="Flush command return: ${blah}"/>
  	</antcall>
    </target>
    
   <target name="get-cmd" >
        <exec executable="bash" outputproperty="query-result" >
                <arg value="-c"/>
		<arg value="echo get index=20937 | nc localhost 11311"/>
        </exec>
        <condition property="get.fail">
                <and>
                        <not>
                                <contains string="${query-result}" substring="recordIds"/>
                        </not>
                </and>
        </condition>
        <echo>Get command return:${query-result}</echo>
    </target>
    <target name="check-index" depends="get-cmd" if="get.fail">
        <antcall target="send-mail">
                <param name="title" value="Get command failed"/>
                <param name="message" value="Get command return: ${query-result}"/>
        </antcall>
    </target>
</project>
