<?php
require_once dirname(__FILE__)."/util.php";

$self_dir = ensure_suffix("/", dirname($_SERVER["SCRIPT_NAME"]));

function by_prefix($url, $main_url=null)
{
	if (!isset($main_url)) $main_url = $url;
	
	return function($path) use($url, $main_url)
	{
		return $path ? $url.$path : $main_url;
	};
}

$h_level = 5;
$root = $_SERVER["SERVER_NAME"].rm_suffix("/", $self_dir)."?";
# instead of PATH_INFO, to support the Apache ErrorDocument directive
$rel_fs_path = rm_prefix($self_dir, $_SERVER["REQUEST_URI"]);
$path = parse_dot_path($rel_fs_path);
$ns = strtolower($path->head);

function add_ns($name, $path_pat, $url_func)
{
	global $root, $h_level, $path, $ns;
	if (is_string($url_func)) $url_func = by_prefix($url_func);
	
	print("<h".$h_level.' id="'.$name.'">'.$root.'<big><a href="http://'.$root
		.$name.'">'.$name."</a></big><i>".$path_pat."</i></h".$h_level.">\n");
	if ($ns === strtolower($name)) # found match, so redirect
	{
		header("Location: ".strip_url($url_func($path->tail)));
		ob_end_flush();
		exit;
	}
}

function custom_separator($url, $sep, $main_url=null)
{
	if (!isset($main_url)) $main_url = $url;
	
	return function($path) use($url, $sep, $main_url)
	{
		if ($path === "") return $main_url;
		$path = parse_dot_path($path);
		return $url.join_non_empty($sep, array($path->head, $path->tail));
	};
}

function fragment_override($url, $fragment=null)
{
	return function($path) use($url, $fragment)
	{
		if (!isset($fragment))
		{
			$path = parse_dot_path($path);
			$fragment = $path->head;
			$path = $path->tail;
		}
		if ($path !== "") $url .= "?".$path;
		return $url."#".$fragment;
	};
}

function phpPgAdmin($url, $table=null)
{
	return function($path) use($url, $table)
	{
		$path = join_non_empty(".", array($table, $path));
		$path = parse_dot_path($path);
		$subject = "schema";
		if ($path->head !== "")
		{
			$url .= "&table=".$path->head;
			if ($path->tail !== "")
			{
				$url .= "&column=".$path->tail;
				$subject = "column";
			}
			else $subject = "table";
		}
		$url .= "&subject=".$subject;
		return $url;
	};
}

function phpMyAdmin($url, $table=null)
{
	return function($path) use($url, $table)
	{
		$path = join_non_empty(".", array($table, $path));
		$path = parse_dot_path($path);
		$target = "db_structure";
		if ($path->head !== "")
		{
			$url .= "&table=".$path->head;
			if ($path->tail !== "") $url .= "&column=".$path->tail;
			$target = "tbl_structure";
		}
		$url .= "&target=".$target.".php";
		return $url;
	};
}

ob_start(); // delay output in case there is a redirect
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>VegPath</title>
		<link rel="stylesheet" type="text/css" href="/main.css" />
		<style type="text/css">
h1 {color: green;}
		</style>
		<script type="text/javascript" src="/util.js"></script>
	</head>
	<body>
		<h1>VegPath&nbsp;&nbsp; <small><a href="http://en.wikipedia.org/wiki/PURL">persistent URLs</a> for vegetation resources</small></h1>
		<div>Supported URL patterns:&nbsp;&nbsp; <small>(elements in <i>italics</i> are optional)</small></div>
<?php
# add and list URLs

$Redmine = "https://projects.nceas.ucsb.edu/nceas/projects/bien";
$Redmine_svn = $Redmine."/repository/raw";

$nimoy = "http://nimoy.nceas.ucsb.edu/phpmyadmin/index.php";
function nimoy_db($db) { global $nimoy; return phpMyAdmin($nimoy."?db=".$db); }

$IH_db = phpMyAdmin($nimoy."?db=bien3_adb", "ih");

$SALVIAS = "http://salvias.net/Documents/salvias_data_dictionary.html";
?>
		<table border="0" cellspacing="0" cellpadding="0">
			<tr valign="top">
				<td nowrap="nowrap"><h2>Terms</h2></td>
				<td nowrap="nowrap"><h2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</h2></td>
				<td nowrap="nowrap"><h2>Data</h2></td>
			</tr>
			<tr valign="top">
				<td nowrap="nowrap">
<?php
print("<blockquote>\n");
print("<h3>Exchange schemas</h3>");
{
	print("<blockquote>\n");
	add_ns("VegCore", ".term", "https://projects.nceas.ucsb.edu/nceas/projects/bien/wiki/VegCore#");
	add_ns("DwC", ".term", "http://rs.tdwg.org/dwc/terms/#");
	{
		print("<blockquote>\n"); $h_level++;
		add_ns("DwC.history", ".term", "http://rs.tdwg.org/dwc/terms/history/#");
		print("</blockquote>\n"); $h_level--;
	}
	add_ns("TCS", "#/XPath", "http://www.tdwg.org/standards/117/download/#/v101.xsd");
	add_ns("VegX", "#/XPath", "http://wiki.tdwg.org/twiki/pub/Vegetation/WebHome/VegX_Schema_1.5.3_proposed.zip#/veg.xsd");
	print("</blockquote>\n");
}
print("<h3>Aggregators</h3>");
{
	print("<blockquote>\n");
	add_ns("VegBank", ".table.column", custom_separator("http://vegbank.org/vegbank/views/dba_tabledescription_detail.jsp?view=detail&entity=dba_tabledescription&where=where_tablename&wparam=",
		"#", "http://vegbank.org/get/index/dba_tabledescription"));
	add_ns("SALVIAS", ".table.column", by_prefix($SALVIAS."#Plot_", $SALVIAS));
	{
		print("<blockquote>\n"); $h_level++;
		add_ns("SALVIAS.db", ".table.column", nimoy_db("salvias_plots"));
		add_ns("SALVIAS.users", ".table.column", nimoy_db("salvias_users"));
		print("</blockquote>\n"); $h_level--;
	}
	add_ns("BIEN2", "", $nimoy."?target=server_databases.php");
	{
		print("<blockquote>\n"); $h_level++;
		add_ns("BIEN2.web", ".table.column", nimoy_db("bien_web"));
		add_ns("BIEN2.core", ".table.column", nimoy_db("bien2"));
		add_ns("BIEN2.geoscrub", ".table.column", nimoy_db("geoscrub"));
		add_ns("BIEN2.staging", ".table.column", nimoy_db("bien2_staging"));
		print("</blockquote>\n"); $h_level--;
	}
	add_ns("VegBIEN", ".table.column", phpPgAdmin("http://vegbiendev.nceas.ucsb.edu/phppgadmin/redirect.php?server=localhost%3A5432%3Aallow&database=vegbien&schema=public"));
	print("</blockquote>\n");
}
print("<h3>Primary databases</h3>");
{
	print("<blockquote>\n");
	add_ns("TNRS", ".term", fragment_override("http://tnrs.iplantcollaborative.org/instructions.html", "download_results"));
	add_ns("CTFS", "", $Redmine."/wiki/CTFS");
	{
		print("<blockquote>\n"); $h_level++;
		add_ns("CTFS.schema", ".table.column", $Redmine_svn."/inputs/CTFS/_archive/DBv5.txt#");
		add_ns("CTFS.tables", ".table", fragment_override($Redmine."/wiki/CTFS", "Tables"));
		add_ns("CTFS.terms", ".term", $Redmine_svn."/inputs/CTFS/_src/ctfs-comments_worksheet.xls#");
		print("</blockquote>\n"); $h_level--;
	}
	add_ns("IH.db", ".term", $IH_db);
	print("</blockquote>\n");
}
print("<h3>People</h3>");
{
	print("<blockquote>\n");
	add_ns("Brad", "", "mailto:bboyleATemail.arizona.edu");
	{
		print("<blockquote>\n"); $h_level++;
		$Brad_Boyle_VegCore = $Redmine_svn."/schemas/VegCore/Brad_Boyle";
		add_ns("Brad.data-provenance", ".term", $Brad_Boyle_VegCore."/BIEN%20database%20entities%20related%20to%20data%20provenance%20and%20ownership.docx#");
		add_ns("Brad.DwC-IDs.2013-2-7", ".term", $Brad_Boyle_VegCore."/vegbien_identifiers.xlsx#");
		add_ns("Brad.DwC-IDs.2013-1-31", ".term", $Brad_Boyle_VegCore."/vegbien_identifier_examples.xlsx#");
		print("</blockquote>\n"); $h_level--;
	}
	print("</blockquote>\n");
}
print("</blockquote>\n");
?>
				</td>
				<td nowrap="nowrap"></td>
				<td nowrap="nowrap">
<?php
print("<blockquote>\n");
print("<h3>Institutions</h3>");
{
	print("<blockquote>\n");
	add_ns("IH", ".herbarium_code", by_prefix("http://sweetgum.nybg.org/ih/herbarium_list.php?QueryName=DetailedQuery&Restriction=NamPartyType+%3D+%27IH+Herbarium%27&col_NamOrganisationAcronym=",
		"http://sweetgum.nybg.org/ih/"));
	print("</blockquote>\n");
}
print("</blockquote>\n");
?>
				</td>
			</tr>
		</table>
<?php
if (ends_with($root, '#')) # URL shortener requires fragment redirect
{
?>
		<script type="text/javascript">
var loc = document.location
if (loc.hash) document.location = '?'+rm_prefix('#', loc.hash)
		</script>
<?php
}
?>
		<p>&nbsp;</p>
<?php
ob_end_flush();

# full directory index
# to fix bug, only display if invoked as "__.org/", not "__.org/index.php"
if (substr($_SERVER["SCRIPT_URI"], -1) === '/')
	fpassthru(fopen($_SERVER["SCRIPT_URI"]."all?".$_SERVER["QUERY_STRING"], "r"));
?>
	</body>
</html>