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

function task_homeDeployServerToTest() {

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

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

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

    print( "Copy sources to temp dir...\n" );
    $local->rCopy( $lSrcDir . '/public/server', $lTempDir . '/public/server' );
    $local->rCopy( $lSrcDir . '/src/server', $lTempDir . '/src/server' );
    $local->rCopy( $lSrcDir . '/src/shared', $lTempDir . '/src/shared' );

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

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

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

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

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

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

}

task_homeDeployServerToTest();