<?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_homeInstallLiveDBOnLocal() {

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

    $yesterday = date( 'Y-m-d' ); //, (time() - 60 * 60 * 24) );
    $backupName = 'dbdump' . $yesterday . '.zip';


    print ( "Download dump..." );
    $server->download( Server::ROOT_DIR . '/_backup/tbuddy/' . $backupName, Local::TEMP_DIR . '/dbdump.zip' );

    print ( "Extract dump..." );
    $local->mkDir( Local::TEMP_DIR . '/dbdump' );
    $local->unzipToFolder( Local::TEMP_DIR . '/dbdump.zip', Local::TEMP_DIR . '/dbdump' );

    print ( "Import SQL dump..." );
    $local->exec( 'c:\wamp\bin\mysql\mysql5.6.17\bin\mysql.exe -uroot tb_core < ' .  Local::TEMP_DIR .  '/dbdump/tbcore.sql' );
    $local->exec( 'c:\wamp\bin\mysql\mysql5.6.17\bin\mysql.exe -uroot tb_teamdata < ' . Local::TEMP_DIR . '/dbdump/tbteamdata.sql' );

    print ( "Set master password..." );
    $masterPW = TB_Shared_Utils_Security::getSaltedPassword( 'test' );
    $dbCore = TB_Shared_Db_Core::get();

    $sql = 'UPDATE ' . TB_Shared_Ent_Core_Account::getTable() . ' SET pass = :password';

    $stmt = $dbCore->prepare( $sql );
    $stmt->execute( array( ':password' => $masterPW ) );

    print ( "Done." );
}

task_homeInstallLiveDBOnLocal();