#!/usr/bin/php
<?php
/*=====================================================================*\
|| ###################################################################
|| # xgettext For ISSO PHP and Template Files
|| # Copyright (c)2005-2009 Iris Studios, Inc.
|| #
|| # This program is free software; you can redistribute it and/or modify
|| # it under the terms of the GNU General Public License as published by
|| # the Free Software Foundation; version 2 of the License.
|| #
|| # This program 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 General Public License for
|| # more details.
|| #
|| # You should have received a copy of the GNU General Public License along
|| # with this program; if not, write to the Free Software Foundation, Inc.,
|| # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|| ###################################################################
\*=====================================================================*/

require_once('/usr/local/apache2/htdocs/ISSO/Functions.php');

// ###################################################################
// generate the list of files to process

$filelist = BSFunctions::scan_directory('.');

foreach ($filelist as $file)
{
	$ext = BSFunctions::fetch_extension($file);
	if ($ext == 'php')
	{
		$files[dirname($file)] = BSFunctions::fetch_source_path(dirname($file)) . '*.php';
	}
	else if ($ext == 'xml' || $ext == 'tpl' || $ext == 'html' || $ext == 'htm')
	{
		$files[dirname($file)] = BSFunctions::fetch_source_path(dirname($file)) . '*.php';
		$templates[] = array(
			'orig' => BSFunctions::fetch_source_path('./' . $dirpath) . $file,
			'temp' => BSFunctions::fetch_source_path('./' . $dirpath) . $file . '.xgt.php'
		);
	}
}

// ###################################################################
// compile the templates into gettext-friendly PHP code

function process_template_text($text)
{
	return '<?php gettext("' . $text . '"); ?>';
}

foreach ($templates as $paths)
{
	$template = file_get_contents($paths['orig']);

	$template = preg_replace('#\{@\\"(.*?)\\"\}#ise', 'process_template_text(\'$1\')', $template);

	file_put_contents($paths['temp'], $template);

	$removelater[] = $paths['temp'];
}

// ###################################################################
// run xgettext on all of the various files that we have now created

// get any arguments and just pass them to xgettext
$arguments = $argv;
unset($arguments[0]);

exec('xgettext ' . implode(' ', $arguments) . ' ' . implode(' ', $files));

// ###################################################################
// remove all the files we no longer need

foreach ($removelater as $path)
{
	unlink($path);
}

?>