<?php
function searchinc($basename)
{
	static $cache = array();
	$proc = 'searchinc: ';

	// look in the cashe first
	if (isset($cache[$basename]))
	{
		$loc = $cache[$basename].$basename;
	}
	else
	{
		// find how many levels we are below the base path
		$here = realpath('.');
		assert('substr($here, 0, strlen($GLOBALS[\'GLOBAL_ROOTDIR\'])) == $GLOBALS[\'GLOBAL_ROOTDIR\']');
		$subdir = substr($here, strlen($GLOBALS['GLOBAL_ROOTDIR']));
		$levels = substr_count($subdir, '/')+1;
		$prefix = '';
		$loc = false;
	
		// for each level
		while ($levels--)
		{
			// look in the current directory
			if (file_exists($prefix.$basename) && is_file($prefix.$basename))
			{
				$loc = $prefix.$basename;
				$cache[$basename] = $prefix;
				break;
			}
			// look in 'include'
			else
			{
				if (file_exists($prefix.'include/'.$basename) && is_file($prefix.'include/'.$basename))
				{
					$loc = $prefix.'include/'.$basename;
					$cache[$basename] = $prefix.'include/';
					break;
				}
				// look in 'inc'
				else
				{
					if (file_exists($prefix.'inc/'.$basename) && is_file($prefix.'inc/'.$basename))
					{
						$loc = $prefix.'inc/'.$basename;
						$cache[$basename] = $prefix.'inc/';
						break;
					}
				}
			}
			// if not found go the next level up
			if (!$loc)
			{
				$prefix .= '../';
			}
		}
		// end for each level
	}
	
	return $loc;
}
 ?>