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

function task_homeDeployClientToTest() {

    $server = new Server();
    $local = new Local();

    $lTempDir = Local::TEMP_DIR . '/new-client';
    $lSrcDir = Local::PROJECT_ROOT . '/apps/teambuddy';
    $remoteRootDir = Server::ROOT_DIR . '/projects/teambuddy/apps/teambuddy';

    print( "Run task: task_buildJavascriptACL\n" );
    include_once 'task_buildJavascriptACL.php';

    print( "Reset local temp dirs...\n" );
    $local->rmDir( $lTempDir );
    $local->mkDir( $lTempDir );
    $local->mkDir( $lTempDir . '/public' );
    $local->mkDir( $lTempDir . '/public/client' );
    $local->mkDir( $lTempDir . '/src' );
    $local->mkDir( $lTempDir . '/src/client' );

    print( "Copy client code to temp folder...\n" );
    $local->rCopy( $lSrcDir . '/public/client',$lTempDir . '/public/client' );
    $local->fileCopy( $lSrcDir . '/public/client/ical/.htaccess', $lTempDir . '/public/client/ical/.htaccess' );
    $local->rCopy( $lSrcDir . '/src/client',   $lTempDir . '/src/client' );

    print( "Overwrite local config...\n" );
    $local->fileCopy( $lTempDir . '/src/client/config/locals/tbuddy-live.php', $lTempDir . '/src/client/config/boot_local.php' );

    $local->zipFolder( $lTempDir, $lTempDir . '/../new-client.zip' );

    print( "Uploading compressed new client...\n" );
    $server->upload($lTempDir . '/../new-client.zip',  Server::TEMP_DIR . '/new-client.zip' );

    print( "Cleanup old sources...\n" );
    $server->exec( "rm -rf $remoteRootDir/public/client" );
    $server->exec( "md $remoteRootDir/public/client" );
    $server->exec( "rm -rf $remoteRootDir/src/client" );
    $server->exec( "md $remoteRootDir/src/client" );

    print( "Copy new client files to remote dest dir...\n" );
    $server->exec( "cd " . Server::TEMP_DIR . ";unzip -o new-client.zip -d $remoteRootDir" );

    print( "Cleanup remote zip file...\n" );
    $server->exec( "cd " . Server::TEMP_DIR . ";rm new-client.zip;" );
}

task_homeDeployClientToTest();