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

include_once 'build_inc-new.php';

require_once __DIR__ . '/../../src/server/config/boot_global.php';
require_once __DIR__ . '/../../src/server/config/boot_local.php';

function task_homeDeployAdminToTest() {

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

    $lTempDir = Local::TEMP_DIR . '/new-admin';
    $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/admin' );
    $local->mkDir( $lTempDir . '/src' );
    $local->mkDir( $lTempDir . '/src/admin' );

    print( "Copy sources to temp dir...\n" );
    $local->rCopy( $lSrcDir . '/public/admin', $lTempDir . '/public/admin' );
    // Explicitly copy .htaccess
    $local->fileCopy( $lSrcDir . '/public/admin/api/.htaccess', $lTempDir . '/public/admin/api/.htaccess' );
    $local->rCopy( $lSrcDir . '/src/admin', $lTempDir . '/src/admin' );

    // Overwrite config
    $local->fileCopy( $lTempDir . '/src/admin/config/locals/global-live.json', $lTempDir . '/src/admin/config/global.json' );

    print( "Zip...\n" );
    $local->zipFolder( $lTempDir, $lTempDir . '/../new-admin.zip' );

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

    print( "Remove outdated sources...\n");
    $server->exec( "rm -rf $remoteRootDir/public/admin" );
    $server->exec( "md $remoteRootDir/public/admin" );
    $server->exec( "rm -rf $remoteRootDir/src/admin" );
    $server->exec( "md $remoteRootDir/src/admin" );

    print( "Extract new sources to destination...\n" );
    $server->exec( "cd " . Server::TEMP_DIR. ";unzip -o new-admin.zip -d $remoteRootDir" );

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

task_homeDeployAdminToTest();