<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/
include_once 'build_inc.php';

function task_deployClientToTest() {

    global $local_temp_dir,
           $local_source_dir,
           $remote_temp_dir,
           $remote_dest_dir_teambuddy;

    $lTempDir = $local_temp_dir . '/new-client';
    $lSrcDir = $local_source_dir . '/apps/teambuddy';

    debug( "Remove old directory if exists...\n" );
    windowsRemoveDir( $lTempDir );
    localMkdir( $lTempDir );
    localMkDir( $lTempDir . '/public' );
    localMkDir( $lTempDir . '/public/client' );
    localMkDir( $lTempDir . '/src' );
    localMkDir( $lTempDir . '/src/client' );

    debug( "Copy client code to temp folder..." );
    localRCopy( $lSrcDir . '/public/client',$lTempDir . '/public/client' );
    localRCopy( $lSrcDir . '/src/client',   $lTempDir . '/src/client' );

    debug( "Overwrite local config..." );
    localFileCopy( $lTempDir . '/src/client/config/locals/tbuddy-live.php', $lTempDir . '/src/client/config/boot_local.php' );

    localZipFolder( $lTempDir, $lTempDir . '/../new-client.zip' );

    debug( "Uploading compressed new client..." );
    sshConnect();
    sshFileCopy( $lTempDir . '/../new-client.zip',  $remote_temp_dir . '/new-client.zip' );

    debug ( "Cleanup old sources..." );
    sshExec( "rm -rf $remote_dest_dir_teambuddy/public/client" );
    sshExec( "md $remote_dest_dir_teambuddy/public/client" );
    sshExec( "rm -rf $remote_dest_dir_teambuddy/src/client" );
    sshExec( "md $remote_dest_dir_teambuddy/src/client" );

    debug( "Copy new client files to remote dest dir..." );
    sshExec( "cd $remote_temp_dir;unzip -o new-client.zip -d $remote_dest_dir_teambuddy" );

    debug( "Cleanup remote zip file..." );
    sshExec( "cd $remote_temp_dir;rm new-client.zip;" );
}

task_deployClientToTest();