#!/usr/bin/perl -w

%toplevels = ( 'api' => 'api',
	       'dba' => 'Database%20access',
	       'external' => 'utilities',
	       'framework' => 'framework',
	       'graphics' => 'Drawing',
	       'gsl' => 'gsl',
	       'installation' => 'Installation',
	       'l10n' => 'l10n',
	       'oi' => 'oi',
	       'porting' => 'porting',
	       'sc' => 'Spreadsheet',
	       'script' => 'scripting',
	       'sw' => 'Word%20processor',
	       'tools' => 'tools',
	       'ucb' => 'ucb',
	       'udk' => 'udk',
	       'ui'  => 'ui',
	       'util' => 'utilities',
	       'whiteboard' => 'whiteboard',
	       'xml' => 'xml'
	     );

%allmodules = (
	       'accessibility' => 'gsl/accessibility',
	       'autodoc' => 'tools/autodoc',
	       'automation' => 'util/automation',
	       'basctl' => 'script/basctl',
	       'basegfx' => 'graphics/basegfx',
	       'basic' => 'script/basic',
	       'berkeleydb' => 'external/berkeleydb',
	       'binfilter' => 'framework/binfilter',
	       'bitstream_vera_fonts' => 'external/addons/bitstream_vera_fonts',
	       'boost' => 'external/boost',
	       'bridges' => 'udk/bridges',
	       'canvas' => 'gsl/canvas',
	       'chaos' => 'ucb/chaos',
	       'chart2' => 'graphics/chart2',
	       'codemaker' => 'udk/codemaker',
	       'comphelper' => 'util/comphelper',
	       'configmgr' => 'util/configmgr',
	       'config_office' => 'tools/config_office',
	       'connectivity' => 'dba/connectivity',
	       'cosv' => 'tools/cosv',
	       'cppcanvas' => 'gsl/cppcanvas',
	       'cppu' => 'udk/cppu',
	       'cppuhelper' => 'udk/cppuhelper',
	       'cpputools' => 'udk/cpputools',
	       'crashrep' => 'porting/crashrep',
	       'curl' => 'external/curl',
	       'default_images' => 'ui/default_images',
	       'dbaccess' => 'dba/dbaccess',
	       'desktop' => 'framework/desktop',
	       'dictionaries' => 'external/addons/dictionaries',
	       'dmake' => 'tools/dmake',
	       'dtrans' => 'gsl/dtrans',
	       'embedserv' => 'framework/embedserv',
	       'eventattacher' => 'util/eventattacher',
	       'expat' => 'external/expat',
	       'extensions' => 'util/extensions',
	       'external' => 'util/external',
	       'extras' => 'installation/extras',
	       'fileaccess' => 'util/fileaccess',
	       'filter' => 'framework/filter',
	       'forms' => 'gsl/forms',
	       'fpicker' => 'gsl/fpicker',
	       'framework' => 'framework/framework',
	       'freetype' => 'external/freetype',
	       'goodies' => 'graphics/goodies',
	       'helpcontent' => 'installation/helpcontent',
	       'hwpfilter' => 'sw/hwpfilter',
	       'i18n' => 'l10n/i18n',
	       'i18npool' => 'l10n/i18npool',
	       'i18n_simple' => 'l10n/i18n',
	       'i18nutil' => 'l10n/i18nutil',
	       'icu' => 'external/icu',
	       'idl' => 'framework/idl',
	       'idlc' => 'udk/idlc',
	       'instsetoo' => 'installation/instsetoo',
	       'instsetoo_native' => 'installation/instsetoo_native',
	       'io' => 'util/io',
	       'javaunohelper' => 'udk/javaunohelper',
	       'jpeg' => 'external/jpeg',
	       'jurt' => 'udk/jurt',
	       'jut' => 'util/jut',
	       'jvmfwk' => 'udk/jvmfwk',
	       'jvmaccess' => 'udk/jvmaccess',
	       'lingucomponent' => 'whiteboard/lingucomponent',
	       'linguistic' => 'sw/linguistic',
	       'MathMLDTD' => 'external/MathMLDTD',
	       'moz' => 'external/moz',
	       'nas' => 'external/nas',
	       'neon' => 'external/neon',
	       'netbeans_integration' => 'external/netbeans_integration',
	       'np_sdk' => 'external/np_sdk',
	       'odk' => 'api/odk',
	       'offapi' => 'api/offapi',
	       'officecfg' => 'util/officecfg',
	       'offmgr' => 'framework/offmgr',
	       'offuh' => 'api/offuh',
	       'ooo_custom_images' => 'ui/ooo_custom_images',
	       'package' => 'xml/package',
	       'padmin' => 'gsl/padmin',
	       'psprint' => 'gsl/psprint',
	       'psprint_config' => 'external/psprint_config',
	       'python' => 'external/python',
	       'pyuno' => 'udk/pyuno',
	       'rdbmaker' => 'udk/rdbmaker',
	       'readlicense' => 'installation/readlicense',
	       'readlicense_oo' => 'installation/readlicense',
	       'regexp' => 'external/regexp',
	       'registry' => 'udk/registry',
	       'remotebridges' => 'udk/remotebridges',
	       'res' => 'framework/res', # deprecated
	       'ridljar' => 'udk/ridljar',
	       'rsc' => 'gsl/rsc',
	       'rvpapi' => 'gsl/rvpapi',
	       'sablot' => 'external/sablot',
	       'sal' => 'porting/sal',
	       'salhelper' => 'udk/salhelper',
	       'sandbox' => 'util/sandbox',
	       'sane' => 'external/sane',
	       'sax' => 'xml/sax',
	       'sc' => 'sc/sc',
	       'scaddins' => 'sc/scaddins',
	       'sch' => 'graphics/sch',
	       'scp' => 'installation/scp', # deprecated
	       'scp2' => 'installation/scp2',
	       'scptools' => 'installation/scptools', # deprecated
	       'sd' => 'graphics/sd',
	       'sdk_oo' => 'api/sdk_oo',
	       'setup2' => 'installation/setup2', # deprecated
	       'sfx2' => 'framework/sfx2',
	       'shell' => 'gsl/shell',
	       'sj2' => 'oi/sj2',
	       'slideshow' => 'graphics/slideshow',
	       'so3' => 'oi/so3',
	       'solenv' => 'tools/solenv',
	       'soltools' => 'tools/soltools',
	       'sot' => 'util/sot',
	       'starmath' => 'sw/starmath',
	       'stlport' => 'external/stlport',
	       'stoc' => 'udk/stoc',
	       'store' => 'ucb/store',
	       'svtools' => 'util/svtools',
	       'svx' => 'graphics/svx',
	       'sw' => 'sw/sw',
	       'sysui' => 'gsl/sysui',
	       'toolkit' => 'gsl/toolkit',
	       'tools' => 'util/tools',
	       'transex3' => 'l10n/transex3',
	       'twain' => 'external/twain',
	       'ucb' => 'ucb/ucb',
	       'ucbhelper' => 'ucb/ucbhelper',
	       'udkapi' => 'api/udkapi',
	       'udm' => 'tools/udm',
	       'unixODBC' => 'external/unixODBC',
	       'UnoControls' => 'gsl/UnoControls',
	       'unoil' => 'udk/unoil',
	       'unotools' => 'util/unotools',
	       'unoxml' => 'framework/unoxml',
	       'unzip' => 'util/unzip',
	       'uui' => 'ucb/uui',
	       'vcl' => 'gsl/vcl',
	       'vos' => 'udk/vos',
	       'wizards' => 'installation/wizards',
	       'x11_extensions' => 'external/x11_extensions',
	       'xml2cmp' => 'tools/xml2cmp',
	       'xmlhelp' => 'util/xmlhelp',
	       'xmloff' => 'xml/xmloff',
	       'xmlscript' => 'script/xmlscript',
	       'xmlsecurity' => 'xml/xmlsecurity',
	       'XmlSearch' => 'util/XmlSearch',
	       'zlib' => 'external/zlib'
	      );

%exceptions = (
	       'sd' => 'Drawing',   # graphics
	       'sch' => 'Chart',    # graphics
	       );

%languages = (
	      'Brazilian(Pt)' => 'br-pt',
	      'Catalan' => 'ca',
	      'Czech' => 'cs',
	      'Danish' => 'da',
	      'German' => 'de',
	      'Greek' => 'el',
	      'Spanish' => 'es',
	      'Basque' => 'eu',
	      'Finnish' => 'fi',
	      'French' => 'fr',
	      'Hindi' => 'hi',
	      'Hungarian' => 'hu',
	      'Japanese' => 'ja',
	      'Korean' => 'ko',
	      'Laotian' => 'lo',
	      'Malaysian' => 'ms',
	      'Netherlands' => 'nl',
	      'Norwegian' => 'no',
	      'Portugese' => 'pt',
	      'Romanian' => 'ro',
	      'Russian' => 'ru',
	      'Slovak' => 'sk',
	      'Slovenian' => 'sl',
	      'Serbian' => 'sr',
	      'Tamil' => 'ta',
	      'Thai' => 'th',
	      'Turkish' => 'tr',
	      'Vietnamese' => 'vi',
	      'Chinese' => 'zh',
	      );

sub build_substitutions($)
{
    my $value;
    my $subs = shift;
    
    $value = '';
    for my $item (sort keys %toplevels) {
	$value .= '<tr><td><a href="http://www.openoffice.org/issues/enter_bug.cgi?component=';
	$value .= $toplevels{$item};
	$value .= '">' . $item . "</a></td></tr>\n";
    }
    $subs->{'%% Toplevels %%'} = $value;

    $value = '';
    for my $item (sort keys %allmodules) {
	my $module;

	if (defined $exceptions{$item}) {
	    $module = $exceptions{$item};
	} else {
	    my $name = $allmodules{$item};
	    $name =~s/\/.*//;
	    $module = $toplevels{$name};
	    defined $module || die "No such module '$name'";
	}

	$value .= '<tr><td><a href="http://www.openoffice.org/issues/enter_bug.cgi?component=';
	$value .= $module;
	$module =~ s/%20/ /g;
	$value .= '">' . $item . "</a></td><td>$module</td></tr>\n";
    }
    $subs->{'%% All modules %%'} = $value;

    $value = '';
    for my $item (sort keys %languages) {
	$value .= '<tr><td><a href="http://www.openoffice.org/issues/enter_bug.cgi?component=';
	$value .= $languages{$item};
	$value .= '">' . $item . "</a></td></tr>\n";
    }
    $subs->{'%% All langs %%'} = $value;
}

my $template;
my %substitutions;
my $InFile;

$template = shift @ARGV || die "build-issues <template-file>\n";

build_substitutions (\%substitutions);

open $InFile, "$template" || die "Can't open $template";
while (<$InFile>) {
    for my $reg (keys %substitutions) {
	s/$reg/$substitutions{$reg}/g;
    }
    print $_;
}
close $InFile;
