<?php

define ('CODEPOT_VERSION',   '@VERSION@');
define ('CODEPOT_DEPOT_DIR', '@DEPOTDIR@');
define ('CODEPOT_CFG_DIR',   '@CFGDIR@');
define ('CODEPOT_WWW_DIR',   '@WWWDIR@');
define ('CODEPOT_LOG_DIR',   '@LOGDIR@/'); // this requires a trailing slash
define ('CODEPOT_CACHE_DIR', '@CACHEDIR@');

define ('CODEPOT_SIGNIN_REDIR_PATH', 'main/signin/');

function load_ini ($file)
{
	if (defined('INI_SCANNER_RAW'))
	{
		$cfg = @parse_ini_file ($file, FALSE, INI_SCANNER_RAW);
	}
	else
	{
		$cfg = @parse_ini_file ($file, FALSE);
	}

	if (!is_array($cfg)) $cfg = array ();

	$xcfgs = array (
		array ('disallowed_letters_in_filename', 'string', ':#|[]$*/'),
		array ('disallowed_letters_in_wikiname', 'string', ':#|[]$*'),

		array ('default_site_language',        'string',     'english'),
		array ('default_site_name',            'string',     '@PACKAGE@'),

		array ('language',                     'string',     'auto'),
		array ('index_page',                   'string',     'index.php'),

		array ('signin_compulsory',            'boolean',    FALSE),
		array ('code_read_access',             'string',     'anonymous'),
		array ('file_read_access',             'string',     'anonymous'),

		array ('https_compulsory',             'boolean',    FALSE),
		array ('https_url',                    'string',     'https://${SERVER_NAME}${REQUEST_URI}'),
		array ('api_base_url',                 'string',     'http://127.0.0.1'),
		//array ('svn_base_url',                 'string',     '${REQUEST_PROTOCOL}://${SERVER_NAME}:${SERVER_PORT}/svn'),
		array ('svn_base_url',                 'string',     '${REQUEST_PROTOCOL}://${HTTP_POST}/svn'),

		array ('login_model',                  'string',     'LdapLoginModel'),
		array ('sysadmin_userids',             'string',     ''),
		array ('max_upload_size',              'string',     '10000'), // kbytes
		array ('max_latest_projects',          'integer',    10),
		array ('max_top_projects',             'integer',    10),
		array ('max_top_committers',           'integer',    10),
		array ('max_projects_per_page',        'integer',    50),
		array ('max_issues_per_page',          'integer',    50),
		array ('max_logs_per_page',            'integer',    50),
		array ('max_logs_in_site_home',        'integer',    10),
		array ('max_logs_in_project_home',     'integer',    5),

		array ('database_hostname',            'string',     'localhost'),
		array ('database_port',                'string',     ''),
		array ('database_username',            'string',     ''),
		array ('database_password',            'string',     ''),
		array ('database_name',                'string',     ''),
		array ('database_driver',              'string',     ''),
		array ('database_prefix',              'string',     ''),
		array ('database_store_gmt',           'boolean',    FALSE),

		array ('auth_mysql_hostname',          'string',     'localhost'),
		array ('auth_mysql_port',              'string',     ''),
		array ('auth_mysql_username',          'string',     ''),
		array ('auth_mysql_password',          'string',     ''),
		array ('auth_mysql_name',              'string',     ''),
		array ('auth_mysql_prefix',            'string',     ''),

		array ('ldap_server_uri',              'string',     'ldap://127.0.0.1:389'),
		array ('ldap_server_protocol_version', 'integer',    3),
		array ('ldap_auth_mode',               'integer',    1),
		array ('ldap_userid_format',           'string',     '${userid}'),
		array ('ldap_password_format',         'string',     '${password}'),
		array ('ldap_admin_binddn',            'string',     ''),
		array ('ldap_admin_password',          'string',     ''),
		array ('ldap_userid_search_filter',    'string',     '(uid=${userid})'),
		array ('ldap_userid_search_base',      'string',     ''),
		array ('ldap_mail_attribute_name',     'string',     ''),
		array ('ldap_insider_attribute_names', 'string',     ''),
		array ('ldap_insider_attribute_value', 'string',     ''),

		array ('svnrepo_dir',                  'string',      CODEPOT_DEPOT_DIR.'/svnrepo'),
		array ('file_dir',                     'string',      CODEPOT_DEPOT_DIR.'/files'),
		array ('issue_file_dir',               'string',      CODEPOT_DEPOT_DIR.'/issuefiles'),
		array ('attachment_dir',               'string',      CODEPOT_DEPOT_DIR.'/attachments'),
		array ('usericon_dir',                 'string',      CODEPOT_DEPOT_DIR.'/usericons'),

		array ('log_threshold',                'integer',     0),

		array ('create_missing_wiki',          'boolean',     FALSE),
		array ('force_project_delete',         'boolean',     FALSE),
		array ('allow_set_time_limit',         'boolean',     FALSE),
		array ('signin_for_code_search',       'boolean',     TRUE),

		array ('footer',                       'string',      ''),
		array ('cloc_command_path',            'string',      CODEPOT_CFG_DIR.'/cloc.pl'),
		array ('code_folder_readme',           'string',     'README'),

		array ('email_sender',                 'string',     ''),
		array ('commit_notification',          'boolean',    TRUE),
		array ('commit_review_notification',   'boolean',    TRUE),
		array ('commit_notification_url',      'string',     ''), // used by the svn post-commit script
		array ('issue_notification',           'boolean',    TRUE),

		array ('svn_tag_property',             'string',     'codepot:tag'),

		// these items are not used by php but by subersion hooks written in perl.
		array ('svn_read_access',                          'string',      'member'),
		array ('svn_min_commit_message_length',            'integer',     0),
		array ('svn_restricted_topdirs',                   'string',      ''),
		array ('svn_restriction_allowed_subdir_depth_min', 'integer',     0),
		array ('svn_restriction_allowed_subdir_depth_max', 'integer',     0),

		
		// this item is used by the codepot-user command.
		array ('codepot_user_executor',                    'string',      'root'),
	);

	foreach ($xcfgs as $x)
	{
		$idx = $x[0];
		$const = 'CODEPOT_' . strtoupper ($idx);

		if (array_key_exists ($idx, $cfg))
		{
			$rawval = $cfg[$idx];
			if ($x[1] == 'string') 
			{
				define ($const, $rawval);
			}
			else if ($x[1] == 'boolean') 
			{
				if (is_bool($rawval))
					define ($const, $rawval);
				else if (is_numeric($rawval) === TRUE)
					define ($const, $rawval != 0);
				else
					define ($const, strcasecmp ($rawval, 'yes') == 0); 
			}
			else if ($x[1] == 'integer') 
			{
				define ($const, intval ($rawval));
			}
			else 
			{
				// set the default value
				define ($const, $x[2]);
			}
		}
		else define ($const, $x[2]);
	}

	return TRUE;
}

load_ini (CODEPOT_CFG_DIR.'/codepot.ini');
?>
