<!-- deploy FRAPI to staging server -->
<target name="staging" 
  description="Deploys FRAPI from the dev branch">
  <phingcall target="deploy-frapi-core"/>
  <phingcall target="deploy-frapi-custom-dev"/>
  <phingcall target="create-frapi-archive" />
  <phingcall target="upload-frapi-staging-build"/>
</target>

<target name="deploy-frapi-core" 
  description="Deploys the FRAPI app core sans admin">
  <delete dir="${build.path.deploy}"/>
  <mkdir dir="${build.path.deploy}"/>
  <gitclone 
    gitPath="${git.path}" 
    repository="${git.frapi-core.url}" 
    targetpath="${build.path.deploy}" />
  <gitcheckout 
    gitPath="${git.path}" 
    repository="${build.path.deploy}" 
    branchname="${git.frapi-core.commit}"/>
</target>

<target name="deploy-frapi-custom-dev" 
description="Deploy FRAPI custom from dev branch">
<mkdir dir="${build.path.temp}"/>
<gitclone
  gitPath="${git.path}"
  repository="${git.frapi-custom.url}"
  targetpath="${build.path.temp}" />
<gitcheckout
  gitPath="${git.path}"
  repository="${build.path.temp}"
  branchname="dev" 
  quiet="true" />
<move 
  overwrite="true" 
  todir="${frapi.path.deploy}"
  file="${build.path.temp}/custom" />
<delete 
  includeemptydirs="true" 
  dir="${build.path.temp}/"/>
</target>

<!-- ARCHIVE / UPLOAD -->
<target name="create-frapi-archive">
  <mkdir dir="${archive.path}"/>
  <delete file="${archive.path}/${archive.filename}"/>
  <tar 
    destFile="${archive.path}/${archive.filename}" 
    compression="gzip"
    prefix="${frapi.path}">
    <!-- include only the minimum files to run FRAPI --> 
    <fileset dir="${frapi.path.deploy}">
      <include name="LICENSE" />
      <include name="public/**" />
      <include name="custom/**" />
      <include name="library/PEAR/**"/>
      <include name="library/Frapi/**" />
      <include name="library/Lupin/Config/**"/>
      <include name="library/Zend/Auth/**"/>
    </fileset>
  </tar>
</target>
<target name="upload-frapi-staging-build">
  <propertyprompt propertyName="ssh.staging.username" 
    useExistingValue="false" 
    promptText="Enter SSH username:" 
    promptCharacter=":"/>
  <propertyprompt propertyName="ssh.staging.password" 
    useExistingValue="false" 
    promptText="Enter SSH password" 
    promptCharacter=":"/>
  <echo message="Uploading build to ${ssh.staging.host}"/>
  <scp 
    username="${ssh.staging.username}" 
    password="${ssh.staging.password}"
    host="${ssh.staging.host}" 
    todir="${ssh.staging.path}" 
    file="${archive.path}/${archive.filename}" 
    autocreate="true"/>
  <echo message="Extracting archive"/>
  <ssh username="${ssh.staging.username}" 
    password="${ssh.staging.password}"
    host="${ssh.staging.host}"
    command="cd ${ssh.staging.path}; 
    tar -xf ${archive.filename};
    rm -f ${archive.filename};
    chown -R nginx:nginx ${ssh.staging.path}"/>
</target>

<!-- setup target is moved into a separate project -->
<target name="setup" 
  description="Initial FRAPI workspace setup">
  <phing 
    phingfile="build-frapi-setup.xml" 
    target="setup" 
    inheritAll="true" />
</target>
