<?php
/**
 * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
 *
 * BOKEH is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
 * BOKEH is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */

class CosmoPaths {
  protected static $_dir_name = 'cosmogramme';
  protected $_filesystem;


  public static function setDirName($name) {
    static::$_dir_name = $name;
  }


  public function getBasePath() {
    $parts = array_filter(explode('/', $this->getFilePath()));
    while ((count($parts)>0) && (end($parts) !== static::$_dir_name))
      array_pop($parts);
    array_pop($parts);
    return ($this->isWindowsPath() ? '' : '/')  . implode('/', $parts) . '/';
  }


  public function isWindowsPath() {
    return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
  }


  public function setFileSystem($filesystem) {
    $this->_filesystem = $filesystem;
  }


  public function getFilesystem() {
    require_once(realpath(dirname(__FILE__)).'/../../../library/Class/Testing/FileSystem.php');
    return null == $this->_filesystem
      ? new Class_Testing_FileSystem()
      : $this->_filesystem;
  }


  public function getConfigPath() {
    return $this->getBasePath() . static::$_dir_name . '/config.php';
  }


  public function getBokehConfigPath() {
    return $this->getBasePath() . 'config.ini';
  }


  public function getBaseUrl() {
    if (!isset($_SERVER['SCRIPT_NAME']))
      return '/' . $this->getSite();

    $parts = array_filter(explode('/', $_SERVER['SCRIPT_NAME']));
    while ((count($parts)>0) && (end($parts) !== static::$_dir_name))
      array_pop($parts);
    array_pop($parts);
    return $parts
      ? '/' . implode('/', $parts)
      : '';
  }


  public function getCosmoBaseUrl() {
    return $this->getBaseUrl() . '/' . static::$_dir_name . '/';
  }


  public function getUserfilesPath() {
    return $this->getBasePath() . 'userfiles';
  }


  public function getSite() {
    $parts = array_filter(explode('/', $this->getBasePath()));
    return array_pop($parts);
  }


  protected function getFilePath() {
    if (isset($_SERVER['SCRIPT_FILENAME'])
        && false !== strpos($_SERVER['SCRIPT_FILENAME'], static::$_dir_name))
      return $_SERVER['SCRIPT_FILENAME'];

    $current_path = $this->getFileSystem()->getcwd();
    if (false !== strpos($current_path, static::$_dir_name))
      return $current_path;

    return realpath(dirname(__FILE__));
  }
}

?>