<?php

include_once(dirname(__FILE__).'/../../Base/Model.php');

class Shell extends Model
{
  var $Database;
  
  function __construct($System)
  {
    parent::__construct($System);  
  }
  
  function HistoryUpdate()
  {     
    $DbResult = $this->Database->query('SELECT Id FROM Server');
    while($Server = $DbResult->fetch_assoc())
    {
      $History = new History($this->System, $Server['Id'], 1);
      $Server = new Server($this->System, $Server['Id']);
      $History->AddValue($Server->GetUsedMemory());
    }   
  }

  function Show() 
  { 
    $Output = ''; 
    if(count($_SERVER['argv']) > 1) 
    { 
      $Command = $_SERVER['argv'][1]; 
      if($Command == 'ServerProcessLog') 
      { 
        if((count($_SERVER['argv']) > 2) and is_numeric($_SERVER['argv'][2])) 
        { 
          $MangosDebug = new MangosDebug($this->System); 
          $Output = $MangosDebug->ProcessLog($_SERVER['argv'][2]); 
        } else $Output = 'Jako druhý parameter je nutno zadat Id serveru.'; 
      } else 
      if($Command == 'ServerLock') 
      { 
        if((count($_SERVER['argv']) > 2) and is_numeric($_SERVER['argv'][2])) 
        { 
          $Server = new Server($this->System, $_SERVER['argv'][2]); 
          $Server->Lock(); 
        } else $Output = 'Jako druhý parameter je nutno zadat Id serveru.'; 
      } else  
      if($Command == 'ServerUnLock')
      {
        if((count($_SERVER['argv']) > 2) and is_numeric($_SERVER['argv'][2]))
        { 
          $Server = new Server($this->System, $_SERVER['argv'][2]);
          $Server->UnLock();
        } else $Output = 'Jako druhý parameter je nutno zadat Id serveru.';
      } else 
      if($Command == 'RealmLock')
      {
        if((count($_SERVER['argv']) > 2) and is_numeric($_SERVER['argv'][2]))
        {
          $Realm = new Realm($this->System, $_SERVER['argv'][2]);
          $Realm->Lock();
        } else $Output = 'Jako druhý parameter je nutno zadat Id světa.';
      } else 
      if($Command == 'RealmUnLock')
      {
        if((count($_SERVER['argv']) > 2) and is_numeric($_SERVER['argv'][2]))
        { 
          $Realm = new Realm($this->System, $_SERVER['argv'][2]);
          $Realm->UnLock();
        } else $Output = 'Jako druhý parameter je nutno zadat Id světa.';
      } else 
      if($Command == 'EmulatorLock')
      { 
        if((count($_SERVER['argv']) > 2) and is_numeric($_SERVER['argv'][2]))
        {
          $Emulator = new Emulator($this->System, $_SERVER['argv'][2]);
          $Emulator->Lock();
        } else $Output = 'Jako druhý parameter je nutno zadat Id emulátoru.';
      } else 
      if($Command == 'EmulatorUnLock')
      {
        if((count($_SERVER['argv']) > 2) and is_numeric($_SERVER['argv'][2]))
        {
          $Emulator = new Emulator($this->System, $_SERVER['argv'][2]);
          $Emulator->UnLock();
        } else $Output = 'Jako druhý parameter je nutno zadat Id emulátoru.';
      } else 
      if($Command == 'BackupLock')
      {
        if((count($_SERVER['argv']) > 2) and is_numeric($_SERVER['argv'][2]))
        {
          $Backup = new Backup($this->System, $_SERVER['argv'][2]);
          $Backup->Lock();
        } else $Output = 'Jako druhý parameter je nutno zadat Id zálohy.';
      } else 
      if($Command == 'BackupUnLock')
      {
        if((count($_SERVER['argv']) > 2) and is_numeric($_SERVER['argv'][2]))
        {
          $Backup = new Backup($this->System, $_SERVER['argv'][2]);
          $Backup->UnLock();
        } else $Output = 'Jako druhý parameter je nutno zadat Id zálohy.';
      } else 
      if($Command == 'TaskProcess')
      {
        $Task = new Task($this->System);
        $Task->ProcessAllCycle();
      } else 
      if($Command == 'ServerStartAll')
      {
        // Start servers
        $DbResult = $this->Database->select('Server', 'Id'); 
        while($DbRow = $DbResult->fetch_assoc())
        {
          $Server = new Server($this->System, $DbRow['Id']);
          $this->System->Modules['User']->User['Id'] = $Server->Server['User'];
          $Server->Start();
        }
        // Start realmd
        $DbResult = $this->Database->select('Realm', 'Id'); 
        while($DbRow = $DbResult->fetch_assoc())
        {
          $Realm = new Realm($this->System, $DbRow['Id']);
          $this->System->Modules['User']->User['Id'] = $Realm->Data['User'];
          $Realm->Start();
        }
      } else 
      if($Command == 'BackupCreateAll')
      {
        // Servers
        $Backup = new Backup($this->System, 0);
        $DbResult = $this->Database->select('Server', 'Id, User'); 
        while($DbRow = $DbResult->fetch_assoc())
        {
          $this->System->Modules['User']->User['Id'] = $DbRow['User'];
          $Backup->Create($DbRow['Id']);
        }
      } else 
      if($Command == 'ServerProcessLogPipe')
      {
        if((count($_SERVER['argv']) > 2) and is_numeric($_SERVER['argv'][2]))
        {
          $Server = new Server($this->System, $_SERVER['argv'][2]);
          $Server->ProcessLog();
        } else $Output = 'Jako druhý parameter je nutno zadat Id serveru.';
      } else 
      if($Command == 'HistoryUpdate')
      {
        $this->HistoryUpdate();
      } else 
      if($Command == 'UpdateState')
      {
        $Realm = new Realm($this->System, 0);
        $Realm->UpdateStateAll();
        $Server = new Server($this->System, 0);
        $Server->UpdateStateAll();
      } else 
      if($Command == 'ServerDatabaseChange')
      {
        if((count($_SERVER['argv']) > 3) and is_numeric($_SERVER['argv'][2]) and is_numeric($_SERVER['argv'][3]))
        {
          $Server = new Server($this->System, $_SERVER['argv'][2]);
          $Server->ChangeDatabaseId($_SERVER['argv'][3]);
        } else $Output = 'Jako druhý parameter je nutno zadat Id serveru, jako třetí Id databáze.';
      } else
      if($Command == 'RealmDatabaseChange')
      {
        if((count($_SERVER['argv']) > 3) and is_numeric($_SERVER['argv'][2]) and is_numeric($_SERVER['argv'][3]))
        {
          $Realm = new Realm($this->System, $_SERVER['argv'][2]);
          $Realm->ChangeDatabaseId($_SERVER['argv'][3]);
        } else $Output = 'Jako druhý parameter je nutno zadat Id světa, jako třetí Id databáze.';
      } else 
        $Output = 'Neznámý příkaz '.$Command;
    } else $Output = 'Jako první parameter je nutno zadat povel.';
    return($Output);
  }
}