<?php

class Emulator extends Module
{
  var $Id;
  var $Emulator;
  var $Task;
  
  function __construct($Database, $Id)
  {
    $this->Database = $Database;
    $this->Task = new Task($Database);
    $this->Id = $Id;
    $DbResult = $this->Database->query('SELECT * FROM `Emulator` WHERE `Id`='.$Id);
    $this->Emulator = $DbResult->fetch_assoc();
    $DbResult = $this->Database->query('SELECT * FROM `Client` WHERE `Id`='.$this->Emulator['Client']);
    $this->Emulator['Client'] = $DbResult->fetch_assoc();
  }
  
  function Add()
  {
    $EmulatorDir = '../emulator/'.$this->Id.'/';
    if(!file_exists($EmulatorDir)) mkdir($EmulatorDir, 0777, true);    
    
    //$this->AddTask('Vytvoření nového emulátoru', array(
    //  'ln -s wowclient/'.$this->Emulator['Client']['Version'].'/maps emulator/'.$this->Emulator['Id'].'/mangos/maps',
    //  'ln -s wowclient/'.$this->Emulator['Client']['Version'].'/dbc emulator/'.$this->Emulator['Id'].'/mangos/dbc',
    //));
    $this->Download();
    $this->Compile();
  }
  
  function Download()
  {
    global $Config;
    
    $this->Lock();
    $this->Task->Add('Stažení emulátoru', array(
      'php www/shell.php EmulatorLock '.$this->Id,
      'mkdir '.$Config['BaseDir'].'emulator/'.$this->Id.'/',
      'cd '.$Config['BaseDir'].'emulator/'.$this->Id.'/',
      'git clone git://github.com/mangos/mangos.git source',
      'cd source',
      'git checkout '.$this->Emulator['CommitHash'],
      'mkdir src/bindings/ScriptDev2',
      'svn checkout -r '.$this->Emulator['ScriptDev2Revision'].' https://scriptdev2.svn.sourceforge.net/svnroot/scriptdev2/ src/bindings/ScriptDev2',
      'git apply src/bindings/ScriptDev2/patches/'.$this->Emulator['ScriptDev2PatchFileName'],
      'cd ../../..',
      'php www/shell.php EmulatorUnLock '.$this->Id,
    ));
  }
  
  function Compile()
  {
    global $Config;
    
    $this->Lock();
    $this->Task->Add('Překlad emulátoru', array(
      'php www/shell.php EmulatorLock '.$this->Id,
      'cd '.$Config['BaseDir'].'emulator/'.$this->Id.'/source',
      'autoreconf -ifv',
      'mkdir objdir',
      'cd objdir',
      'export CFLAGS="-g -ggdb '.$Config['CompilerParameters'].'"',
      'export CXXFLAGS="-g -ggdb '.$Config['CompilerParameters'].'"',
      '../configure --prefix='.$Config['BaseDir'].'emulator/'.$this->Id.'/ --enable-cli --enable-ra',
      'make',
      'make install',
      'cd ../../../..',
      'php www/shell.php EmulatorUnLock '.$this->Id,
    ));
  }

  function ExtractMaps()
  {
    $this->Lock();
    $this->Task->Add('Vygenerování souborů map', array(
      'php www/shell.php EmulatorLock '.$this->Id,
      'cd wowclient/'.$this->Emulator['Client']['Version'].'/client',
      'wine emulator/'.$this->Id.'/source/contrib/extractor/ad.exe',
      'mv wowclient/'.$this->Emulator['Client']['Version'].'/client/dbc wowclient/'.$this->Emulator['Client']['Version'].'/',
      'mv wowclient/'.$this->Emulator['Client']['Version'].'/client/maps wowclient/'.$this->Emulator['Client']['Version'].'/',
      'cd ../../..',
      'php www/shell.php EmulatorUnLock '.$this->Id,      
    ));
    return('Požadavek na vygenerování map zařazen.');
  }
  
  function Lock()
  {
    $this->Database->update('Emulator', 'Id='.$this->Id, array('Lock' => 1));
  }
  
  function UnLock()
  {
    $this->Database->update('Emulator', 'Id='.$this->Id, array('Lock' => 0));
  }
}

?>
