<project name="{{appName}}" default="deploy" basedir="." xmlns:sf="antlib:com.salesforce">

  <property environment="env"/>
  <property file="build.properties"/>

  <condition property="SF_USERNAME" value="">
    <not>
      <isset property="SF_USERNAME"></isset>
    </not>
  </condition>
  <condition property="SF_PASSWORD" value="">
    <not>
      <isset property="SF_PASSWORD"></isset>
    </not>
  </condition>
  <condition property="SF_SESSIONID" value="">
    <not>
      <isset property="SF_SESSIONID"></isset>
    </not>
  </condition>
  <condition property="sf.serverurl" value="login.salesforce.com">
    <not>
      <isset property="sf.serverurl"></isset>
    </not>
  </condition>
  <condition property="sf.maxPoll" value="200">
    <not>
      <isset property="sf.maxPoll"></isset>
    </not>
  </condition>

  <condition property="sf.username" value="${env.SF_USERNAME}" else="${SF_USERNAME}">
    <isset property="env.SF_USERNAME"/>
  </condition>
  <condition property="sf.password" value="${env.SF_PASSWORD}" else="${SF_PASSWORD}">
    <isset property="env.SF_PASSWORD"/>
  </condition>
  <condition property="sf.sessionId" value="${env.SF_SESSIONID}" else="${SF_SESSIONID}">
    <isset property="env.SF_SESSIONID"/>
  </condition>

  <taskdef resource="com/salesforce/antlib.xml" uri="antlib:com.salesforce">
    <classpath>
      <pathelement location="./ant-salesforce.jar"/>
    </classpath>
  </taskdef>

  <target name="deploy"
          description="Deploys the API client library to your Salesforce organization">
    <echo message="Deploying the API client library..."/>
    <sf:deploy username="${sf.username}" password="${sf.password}"
               sessionId="${sf.sessionId}" serverurl="${sf.serverurl}"
               maxPoll="${sf.maxPoll}" deployRoot="deploy" testLevel="RunSpecifiedTests"
               rollbackOnError="true">
  {{#apiInfo}}
    {{#apis}}
      <runTest>{{classname}}Test</runTest>
    {{/apis}}
  {{/apiInfo}}
  {{#models}}
    {{#model}}
      <runTest>{{classname}}Test</runTest>
    {{/model}}
  {{/models}}
      <runTest>SwaggerTest</runTest>
    </sf:deploy>
  </target>

  <target name="undeploy"
          description="Removes the API client library from your Salesforce organization">
    <echo message="Removing the API client library..."/>
    <sf:deploy username="${sf.username}" password="${sf.password}"
               sessionId="${sf.sessionId}" serverurl="${sf.serverurl}"
               maxPoll="${sf.maxPoll}" deployRoot="undeploy"/>
  </target>

  <target name="deployCheckOnly"
          description="Deploys the API client library in check-only mode, without saving changes">
    <echo message="Run 'ant deploy' to deploy this library to your organization."/>
    <echo message="Testing deployment of this API client library without saving changes"/>
    <sf:deploy username="${sf.username}" password="${sf.password}"
               sessionId="${sf.sessionId}" serverurl="${sf.serverurl}"
               maxPoll="${sf.maxPoll}" deployRoot="deploy" testLevel="RunSpecifiedTests"
               checkOnly="true">
  {{#apiInfo}}
    {{#apis}}
      <runTest>{{classname}}Test</runTest>
    {{/apis}}
  {{/apiInfo}}
  {{#models}}
    {{#model}}
      <runTest>{{classname}}Test</runTest>
    {{/model}}
  {{/models}}
      <runTest>SwaggerTest</runTest>
    </sf:deploy>
  </target>
</project>
