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

function task_deployServerToTest() {

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

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

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

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

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

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

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

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

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

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

task_deployServerToTest();