<?xml version="1.0" encoding="UTF-8"?>
<project name="comdono" default="deploy" basedir="." description="comdono deploys">
 <target name="deploy" description="Deploys the current working copy to a remote host">
  <!-- Default hostname is empty, must be passed from the command line -->
  <property name="buildDir" value="../build" />	
  <property name="project.name" value="false" override="true" />
  <property name="base.url" value="false" override="true" />
  <property name="hostfile" value="../${project.name}.properties" />

  <!-- Check for specific host/env file, if not fail! -->
  <available file="${hostfile}" property="hostfilefound" value="true"/>

  <fail unless="hostfilefound" message="Missing host configuration file (${hostfile})!" />

  <!-- Host file exists so loading... -->
  <property file="${hostfile}" />
  <tstamp>
   <format property="build.timestamp" pattern="%Y%m%d%H%M%S"/>
  </tstamp>

  <property name="build.release" value="${project.name}-${build.timestamp}"/>
  <property name="package.name" value="${build.release}.tar.gz"/>

  <tar destfile = "${buildDir}/${package.name}">
    <fileset dir = ".">
      <include name = "application/**"/>
      <include name = "html/**"/>
      <include name = "builder/**"/>
      <include name = "system/**"/>
    </fileset>
  </tar>

  <ssh host="${ssh.host}"
    username="${ssh.username}"
    privkeyfile="${ssh.key}"
    pubkeyfile="${ssh.pub}"
    command="sudo rm -rf ${deploy.location}/${project.name};
      mkdir -p ${deploy.location}/${project.name}"
    failonerror="true"/>

  <echo>Copying package</echo>
  <scp host="${ssh.host}"
    username="${ssh.username}"
    privkeyfile="${ssh.key}"
    pubkeyfile="${ssh.pub}"
    todir="${deploy.location}/${project.name}"
    file="${buildDir}/${package.name}" />
 
  <echo>Extracting package</echo>
  <ssh host="${ssh.host}"
    username="${ssh.username}"
    privkeyfile="${ssh.key}"
    pubkeyfile="${ssh.pub}"
    command="cd ${deploy.location}/${project.name};
    tar zxf ${package.name} >> /tmp/deploy; rm ${package.name} >> /tmp/deploy;
    cd ${deploy.location}/${project.name}/html;
    echo 'Sitemap: http://${base.url}/sitemap.xml' >> robots.txt;
    cd ${deploy.location}/${project.name}/application/libraries;
    sudo ./composer install;
    sudo chown -R www-data:www-data ${deploy.location}/${project.name};
    curl -L ${base.url}/sistema/migration >> /tmp/deploy;
    curl -L ${base.url}/chat/start_server >> /tmp/deploy;
    rm -f /var/log/prod/send_logs_email.php"
    failonerror="false" />

 </target>
</project>
