<?php

$_lib_dir = '/home/joshua/devel/svn/symfony/1.0/lib';
$_data_dir = '/home/joshua/devel/svn/symfony/1.0/data';

require_once($_lib_dir.'/vendor/lime/lime.php');
require_once($_lib_dir.'/util/sfFinder.class.php');
require_once($_lib_dir.'/util/sfToolkit.class.php');

define('DS', DIRECTORY_SEPARATOR);

class symfony_cmd
{
  public $php_cli = null;
  public $data_dir = null;
  public $tmp_dir = null;
  public $t = null;
  public $current_dir = null;

  public function initialize($t, $data_dir)
  {
    $this->t = $t;
    $this->data_dir = $data_dir;

    $this->tmp_dir = sfToolkit::getTmpDir().DIRECTORY_SEPARATOR.'symfony_cmd';

    if (is_dir($this->tmp_dir))
    {
      $this->clearTmpDir();
      rmdir($this->tmp_dir);
    }

    mkdir($this->tmp_dir, 0777);

    $this->current_dir = getcwd();
    chdir($this->tmp_dir);

    $this->php_cli = sfToolkit::getPhpCli();
  }

  public function shutdown()
  {
    $this->clearTmpDir();
    rmdir($this->tmp_dir);
    chdir($this->current_dir);
  }

  protected function clearTmpDir()
  {
    sfToolkit::clearDirectory($this->tmp_dir);
  }

  public function execute_command($cmd, $test = true)
  {
    ob_start();
    passthru(sprintf('%s -d html_errors=off -d open_basedir= -q "%s" %s 2>&1', $this->php_cli, $this->data_dir . '/bin/symfony', $cmd), $return);
    $content = ob_get_clean();
    if ($test)
    {
      $this->t->cmp_ok($return, '<=', 0, sprintf('"symfony %s" returns ok', $cmd));
    }

    return $content;
  }
}

function copyr($source_dir, $dest_dir)
{
  $files = sfFinder::type('file')->name('*')->ignore_version_control()->in($source_dir);

  // copy source files
  foreach ($files as $file)
  {
    $dest = str_replace($source_dir, $dest_dir, $file);

    if (!file_exists(dirname($dest)))
    {
      mkdir(dirname($dest), 0777, true);
    }
    
    copy($file, $dest);
  }
}

function rewrite_config($config_path)
{
  $config = file_get_contents($config_path);

  $config = str_replace(
    array(
      'addon.propel.builder.SfPeerBuilder',
      'addon.propel.builder.SfObjectBuilder',
    ),
    array(
      'plugins.sfPropel13Plugin.lib.propel.builder.SfPeerBuilder',
      'plugins.sfPropel13Plugin.lib.propel.builder.SfObjectBuilder',
    ),
    $config
  );

  file_put_contents($config_path, $config);
}

$t = new lime_test(10, new lime_output_color());
$c = new symfony_cmd();
$c->initialize($t, $_data_dir);

// setup project
$content = $c->execute_command('init-project myproject', false);
$content = $c->execute_command('init-app frontend', false);

// sfPakePropel
copy(dirname(__FILE__).'/fixtures/schema.yml', $c->tmp_dir.DS.'config'.DS.'schema.yml');
copyr(realpath(dirname(__FILE__) . '/../../'), $c->tmp_dir.DS.'plugins'.DS.'sfPropel13Plugin');
rewrite_config($c->tmp_dir.DS.'config'.DS.'propel.ini');

$content = $c->execute_command('propel-13-build-sql');
$t->ok(file_exists($c->tmp_dir.DS.'data'.DS.'sql'.DS.'lib.model.schema.sql'), '"propel-build-sql" creates a "schema.sql" file under "data/sql" directory');

$content = $c->execute_command('propel-13-build-model');
$t->ok(file_exists($c->tmp_dir.DS.'lib'.DS.'model'.DS.'Article.php'), '"propel-build-model" creates model classes under "lib/model" directory');

// sfPakePropelCrudGenerator
$content = $c->execute_command('propel-13-init-crud frontend articleInitCrud Article');
$t->ok(file_exists($c->tmp_dir.DS.'apps'.DS.'frontend'.DS.'modules'.DS.'articleInitCrud'.DS.'config'.DS.'generator.yml'), '"propel-init-crud" initializes a CRUD module');

$content = $c->execute_command('propel-13-generate-crud frontend articleGenCrud Article');
$t->ok(is_dir($c->tmp_dir.DS.'apps'.DS.'frontend'.DS.'modules'.DS.'articleGenCrud'), '"propel-generate-crud" generates a CRUD module');

// sfPakePropelAdminGenerator
$content = $c->execute_command('propel-13-init-admin frontend articleInitAdmin Article');
$t->ok(file_exists($c->tmp_dir.DS.'apps'.DS.'frontend'.DS.'modules'.DS.'articleInitAdmin'.DS.'config'.DS.'generator.yml'), '"propel-init-admin" initializes an admin generator module');

$c->shutdown();
