<?php

class TransformerController 
{
	public static function Run ($argv)
	{
		$action = $argv[1];
		$pyi = $argv[2];

		switch ($action) {
			case 'cubename':
				echo "\"" . TransformerController::GetCubeName($pyi) . "\"";
				break;
			case 'duration':
				$d = TransformerController::GetDuration($pyi);
				if ($d) {
					echo "Geschaetzte Dauer: {$d[0]} Minuten\r\n";
					echo "Geschaetztes Ende: {$d[1]}\r\n";
				}
				break;
			case 'create':
			default:
				TransformerController::CreateCube($pyi);
				break;
		}
	}


	private $app;
	private $model;
	private $cubeFile;
	private $error;
	
	public function __construct () 
	{
		$this->app = new COM("CognosTransformer.Application");
	}

	public static function GetCubeName ($file)
	{
		$transCtrl = new TransformerController();
		if ($transCtrl->OpenModel($file)) {
			$cubeFile = $transCtrl->GetCubeFilename();
			$transCtrl->Close();
			$transCtrl->Quit();

			if (preg_match("/cube_out.(.*)\.mdc$/", $cubeFile, $match)) {
				return $match[1];
			}
		}
		return "";
	}
	
	public static function GetDuration ($logfile)
	{
		if (file_exists($logfile)) {
			foreach (file($logfile) as $line) {			
				if (preg_match("/ZEIT INSGESAMT \(CUBE ERSTELLEN\),(\d\d):(\d\d):(\d\d)/", $line, $match)) {
					$duration = intval($match[1]) * 60 + intval($match[2]) + 1;
					$estimate = strtotime("now") + $duration * 60;
					return array($duration, date("H:i", $estimate));
				}
			}
		}
		return false;
	}
	
	public static function CreateCube ($file) 
	{
		$transCtrl = new TransformerController();
		$transCtrl->OpenModel($file);
		$error = $transCtrl->CreateMdc();
		$transCtrl->Close();
		$transCtrl->Quit();
		
		if ($error) {
			print("\r\n" . TransformerController::formatMessage($error) . "\r\n\r\n");
			die(110);
		}
	}
	
	public function OpenModel ($pyiFile) 
	{
		try {
			$this->model = $this->app->OpenModel($pyiFile);		
			return true;
		} catch (Exception $e) {
			return false;
		}
	}
	
	public function GetCubeFilename () 
	{
		$cube = $this->model->Cubes->Item(1);
		$this->cubeFile = $cube->MDCFile;
		return $this->cubeFile;
	}
	
	public function Close () 
	{
		$this->model->Close();
	}
	
	public function CreateMdc () 
	{
		try {
			$time = strtotime("now");
			$cube = $this->model->Cubes->Item(1);
			$cube->MDCFile = str_replace("\\cube_out\\", "\\cube_out\\tasks\\", strtolower($cube->MDCFile));
			$cube->Update();
			$this->model->CreateMDCFiles();
			
			if (!file_exists($cube->MDCFile)) {
				return "(TR0001) Cube-Datei wurde nicht geschrieben.";
			} else if (intval(filemtime($cube->MDCFile)) < $time) {
				return "(TR0002) Cube-Datei ist nicht aktuell. Stand: " . date("d.m.Y H:i:s", filemtime($cube->MDCFile));
			} else if (filesize($cube->MDCFile) < 50000) {
				return "(TR0003) Cube-Datei ist zu klein. Dateigroesse: " . (filesize($cube->MDCFile)/ 1000) . "KB";
			}
		} catch (Exception $e) {
			if (file_exists($cube->MDCFile) && filesize($cube->MDCFile) < 50000) {
				unlink($cube->MDCFile);
			}
			if (preg_match("/trModel\(CreateMDCFiles\)\s:\s(.*)$/", $e->getMessage(), $match)) {
				return $match[1];
			}
			if (preg_match("/Call to a member function Item\(\) on a non-object/", $e->getMessage(), $match)) {
				return "Modell ist blockiert";
			}
			return $e->getMessage();
		}
		return false;
	}
	
	public function Quit () 
	{
		$this->app->Quit();
	}

	private static function formatMessage($message)
	{
		$message = str_replace("'", "`", $message);
		$message = str_replace("\"", "`", $message);
		$message = str_replace("ä", "ae", $message);
		$message = str_replace("\xf6", "oe", $message);
		$message = str_replace("\xfc", "ue", $message);
		$message = str_replace("ß", "ss", $message);
		$message = str_replace("Ä", "Ae", $message);
		$message = str_replace("Ö", "Oe", $message);
		$message = str_replace("Ü", "Ue", $message);
		$message = str_replace("[->OK]", "", $message);
		$message = str_replace("\r", "", $message);
		$message = str_replace("\n", " ", $message);
		$message = str_replace("<", "", $message);
		$message = str_replace(">", "", $message);
		return $message;
	}
} 