<?xml version="1.0"?>
<!--
  ~ ELASTICSEARCH CONFIDENTIAL
  ~ __________________
  ~
  ~  [2014] Elasticsearch Incorporated. All Rights Reserved.
  ~
  ~ NOTICE:  All information contained herein is, and remains
  ~ the property of Elasticsearch Incorporated and its suppliers,
  ~ if any.  The intellectual and technical concepts contained
  ~ herein are proprietary to Elasticsearch Incorporated
  ~ and its suppliers and may be covered by U.S. and Foreign Patents,
  ~ patents in process, and are protected by trade secret or copyright law.
  ~ Dissemination of this information or reproduction of this material
  ~ is strictly forbidden unless prior written permission is obtained
  ~ from Elasticsearch Incorporated.
  -->

<project name="smoke-test-watcher-and-shield"
         xmlns:ac="antlib:net.sf.antcontrib">

  <import file="${elasticsearch.integ.antfile.default}"/>

  <!-- redefined to work with auth -->
  <macrodef name="waitfor-elasticsearch">
      <attribute name="port"/>
      <attribute name="timeoutproperty"/>
    <sequential>
      <echo>Waiting for elasticsearch to become available on port @{port}...</echo>
      <waitfor maxwait="30" maxwaitunit="second"
               checkevery="500" checkeveryunit="millisecond"
               timeoutproperty="@{timeoutproperty}">
        <socket server="127.0.0.1" port="@{port}"/>
      </waitfor>
    </sequential>
  </macrodef>

  <target name="start-external-cluster-with-found-license-and-shield" depends="setup-workspace">
    <ac:for list="${xplugins.list}" param="xplugin.name">
      <sequential>
        <fail message="Expected @{xplugin.name}-${version}.zip as a dependency, but could not be found in ${integ.deps}/plugins}">
          <condition>
            <not>
              <available file="${integ.deps}/plugins/@{xplugin.name}-${elasticsearch.version}.zip" />
            </not>
          </condition>
        </fail>
      </sequential>
    </ac:for>

    <ac:for param="file">
      <path>
        <fileset dir="${integ.deps}/plugins"/>
      </path>
      <sequential>
        <local name="plugin.name"/>
        <convert-plugin-name file="@{file}" outputproperty="plugin.name"/>
        <install-plugin name="${plugin.name}" file="@{file}"/>
      </sequential>
    </ac:for>

    <local name="home"/>
    <property name="home" location="${integ.scratch}/elasticsearch-${elasticsearch.version}"/>

    <echo>Adding roles.yml with watcher roles</echo>
    <copy file="watcher-with-shield-roles.yml" tofile="${home}/config/shield/roles.yml" overwrite="true"/>

    <echo>Adding shield users...</echo>
    <run-script script="${home}/bin/shield/esusers">
      <nested>
        <arg value="useradd"/>
        <arg value="test_admin"/>
        <arg value="-p"/>
        <arg value="changeme"/>
        <arg value="-r"/>
        <arg value="admin"/>
      </nested>
    </run-script>
    <run-script script="${home}/bin/shield/esusers">
      <nested>
        <arg value="useradd"/>
        <arg value="watcher_manager"/>
        <arg value="-p"/>
        <arg value="changeme"/>
        <arg value="-r"/>
        <arg value="watcher_manager"/>
      </nested>
    </run-script>
    <run-script script="${home}/bin/shield/esusers">
      <nested>
        <arg value="useradd"/>
        <arg value="powerless_user"/>
        <arg value="-p"/>
        <arg value="changeme"/>
        <arg value="-r"/>
        <arg value="crapy_role"/>
      </nested>
    </run-script>

    <startup-elasticsearch>
      <!-- Useful for when debugging -->
      <!--<additional-args>-->
        <!--<arg value="-Des.http.cors.enabled=true"/>-->
        <!--<arg value="-Des.http.cors.allow-origin=*"/>-->
      <!--</additional-args>-->
    </startup-elasticsearch>

    <echo>Checking we can connect with basic auth on port ${integ.http.port}...</echo>
    <local name="temp.file"/>
    <tempfile property="temp.file" destdir="${java.io.tmpdir}"/>
    <get src="http://127.0.0.1:${integ.http.port}" dest="${temp.file}" 
         username="test_admin" password="changeme" verbose="true" retries="10"/>
  </target>
</project>