File ooo-l10n-gen-spec of Package OpenOffice_org-l10n

#!/usr/bin/perl -w

# This script changes the definite article of ProductName

use strict;

sub init_lang_data($$$$)
{
    my ($datap, $curLang, $long_name, $level) = @_;
    
    $datap->{$curLang}{'long_name'} = $long_name;
    $datap->{$curLang}{'level'} = $level;
    $datap->{$curLang}{'locale'} = "$curLang";
    $datap->{$curLang}{'locale'} =~ s/-/_/g;
    $datap->{$curLang}{'package-l10n'} = ();
    $datap->{$curLang}{'package-help'} = ();
    
    return $curLang;
}    

sub read_data($$)
{
    my ($data_file, $datap) = @_;
    my $curLang;
    
    open DATA , "< $data_file" or die "Can't open '$data_file'\n";

    while( my $line = <DATA>){
	chomp $line;
	if ( $line =~ m/^\s*\%lang\s+([\w-]+)\s+(\w+)\s+(\d+)\s*$/ ) {
	    $curLang = init_lang_data($datap, "$1", "$2", "$3");
	} elsif ( $line =~ m/^\s*\%lang\s+([\w-]+)\s+\"([\w\s]+)\"\s+(\d+)\s*$/ ) {
	    $curLang = init_lang_data($datap, "$1", "$2", "$3");
	} elsif ( $line =~ /^\s*\%poor-help\s*$/ ) {
	    $datap->{$curLang}{'poor-help'} = 1;
	} elsif ( $line =~ /^\s*\%package-l10n\s+(.*)$/ ) {
	    push @ {$datap->{$curLang}{'package-l10n'}}, "$1";
	} elsif ( $line =~ /^\s*\%package-help\s+(.*)$/ ) {
	    push @ {$datap->{$curLang}{'package-help'}}, "$1";
	} elsif ( $line =~ /^\s*\%files-l10n\s+(.*)$/ ) {
	    push @ {$datap->{$curLang}{'files-l10n'}}, "$1";
	} elsif ( $line =~ /^\s*\%files-help\s+(.*)$/ ) {
	    push @ {$datap->{$curLang}{'files-help'}}, "$1";
	} elsif ( $line =~ /^\s*$/ ) {
	    # ignore empty line
	} else {
	    die "Synrax error in $data_file, line $.\n";
	}
    }
    close(DATA);
}

sub write_generated_section_start()
{
    print "####################################################\n";
    print "# Start of a section generated by ooo-l10n-gen-spec\n";
    print "# Do not edit!\n";
    print "####################################################\n";
    print "\n";
}

sub write_generated_section_end()
{
    print "####################################################\n";
    print "# End of a section generated by ooo-l10n-gen-spec\n";
    print "####################################################\n";
    print "\n";
}

sub write_section_comment($)
{
    my ($section_name) = @_;

    print "#\n";
    print "# $section_name\n";
    print "#\n";
    print "\n";

}

sub write_level_begin($$)
{
    my ($curLevel, $newLevel) = @_;

    if ($curLevel != $newLevel) {
	print "\n";
	print "%endif\n" 				if ($curLevel > 0);
	print "%if %test_build_langs >= $newLevel\n" 	if ($newLevel > 0);
    }

    return $newLevel;
}

sub write_level_end($)
{
    my ($curLevel) = @_;

    if ($curLevel > 0) {
	print "%endif\n";
    }

    return 0;
}

sub write_l10n_package_section($$)
{
    my ($datap, $curLang) = @_;

    return if ("$curLang" eq "en-US"); # we do not have the l10n-en-US package

    print "\n";
    print "%package -n OpenOffice_org-l10n-$curLang\n";
    print "License:        LGPL v3 only\n";
    print "Summary:        $datap->{$curLang}{'long_name'} Localization Files for OpenOffice.org\n";
    print "Group:          Productivity/Office/Suite\n";
    print "Provides:       locale(OpenOffice_org:$datap->{$curLang}{'locale'})\n";
    print "Provides:       OpenOffice_org-$curLang:%_prefix/ooo-2.0/program/resource/sw680$curLang.res\n";
    print "Provides:       OpenOffice_org-$curLang = %version\n";
    print "Obsoletes:      OpenOffice_org-$curLang < %version\n";
    print "PreReq:         OpenOffice_org-ure = %version\n";

    foreach  my $line (@{$datap->{$curLang}{'package-l10n'}}) {
        print "$line\n";
    }

    print "\n";

    print "%description -n OpenOffice_org-l10n-$curLang\n";
    print "$datap->{$curLang}{'long_name'} localization files for OpenOffice.org.\n";
    print "The localized help content is in OpenOffice.org-help-$curLang.\n" unless (defined $datap->{$curLang}{'poor-help'});
    print "\n";
    print "\n";
}


sub write_help_package_section($$)
{
    my ($datap, $curLang) = @_;
    my $oldPackage;

    return if (defined $datap->{$curLang}{'poor-help'});

    if ("$curLang" eq "en-US") {
	$oldPackage = "OpenOffice_org";
    } else {
	$oldPackage = "OpenOffice_org-$curLang";
    }

    print "\n";
    print "%package -n OpenOffice_org-help-$curLang\n";
    print "License:        LGPL v3 only\n";
    print "Summary:        $datap->{$curLang}{'long_name'} Help Localization for OpenOffice.org\n";
    print "Group:          Productivity/Office/Suite\n";
    print "Provides:       locale(OpenOffice_org:$datap->{$curLang}{'locale'})\n";
    print "Provides:       $oldPackage:%_prefix/ooo-2.0/help/cs/default.css\n";
    print "PreReq:         OpenOffice_org-ure = %version\n";
    print "Supplements:    OpenOffice_org-l10n-$curLang\n";

    foreach  my $line (@{$datap->{$curLang}{'package-help'}}) {
        print "$line\n";
    }

    print "\n";

    print "%description -n OpenOffice_org-help-$curLang\n";
    print "$datap->{$curLang}{'long_name'} help localization for OpenOffice.org. The other localized stuff\n";
    print "is in OpenOffice.org-l10n-$curLang.\n";
    print "\n";
    print "\n";
}


sub write_l10n_scripts_section($$)
{
    my ($datap, $curLang) = @_;

    return if ("$curLang" eq "en-US"); # we do not have the l10n-en-US package

    print "# $curLang\n";
    print "%posttrans -n OpenOffice_org-l10n-$curLang\n";
    print "%_datadir/%ooo_home/link-to-ooo-home %_datadir/%ooo_home/files-l10n-$datap->{$curLang}{'locale'}.txt || true\n";
    print "\n";
    print "%preun -n OpenOffice_org-l10n-$curLang\n";
    print "test \"\$1\" = \"0\" && cp %_datadir/%ooo_home/files-l10n-$datap->{$curLang}{'locale'}.txt %_datadir/%ooo_home/files-l10n-$datap->{$curLang}{'locale'}.txt.postun || true\n";
    print "\n";
    print "%postun -n OpenOffice_org-l10n-$curLang\n";
    print "test \"\$1\" = \"0\" && %_datadir/%ooo_home/link-to-ooo-home --unlink %_datadir/%ooo_home/files-l10n-$datap->{$curLang}{'locale'}.txt.postun || true\n";
    print "rm -f %_datadir/%ooo_home/files-l10n-$datap->{$curLang}{'locale'}.txt.postun 2>/dev/null\n";
}

sub write_help_scripts_section($$)
{
    my ($datap, $curLang) = @_;

    return if (defined $datap->{$curLang}{'poor-help'});

    print "# $curLang\n";
    print "%posttrans -n OpenOffice_org-help-$curLang\n";
    print "%_datadir/%ooo_home/link-to-ooo-home %_datadir/%ooo_home/files-help-$datap->{$curLang}{'locale'}.txt || true\n";
    print "\n";
    print "%preun -n OpenOffice_org-help-$curLang\n";
    print "test \"\$1\" = \"0\" && cp %_datadir/%ooo_home/files-help-$datap->{$curLang}{'locale'}.txt %_datadir/%ooo_home/files-help-$datap->{$curLang}{'locale'}.txt.postun || true\n";
    print "\n";
    print "%postun -n OpenOffice_org-help-$curLang\n";
    print "test \"\$1\" = \"0\" && %_datadir/%ooo_home/link-to-ooo-home --unlink %_datadir/%ooo_home/files-help-$datap->{$curLang}{'locale'}.txt.postun || true\n";
    print "rm -f %_datadir/%ooo_home/files-help-$datap->{$curLang}{'locale'}.txt.postun 2>/dev/null\n";
}

sub write_l10n_files_section($$)
{
    my ($datap, $curLang) = @_;

    return if ("$curLang" eq "en-US"); # we do not have the l10n-en-US package

    print "\n";
    print "%files -f files-l10n-$datap->{$curLang}{'locale'}.txt -n OpenOffice_org-l10n-$curLang\n";
    print "%defattr(-,root,root)\n";

    foreach  my $line (@{$datap->{$curLang}{'files-l10n'}}) {
        print "$line\n";
    }
}

sub write_help_files_section($$$)
{
    my ($datap, $curLang) = @_;

    return if (defined $datap->{$curLang}{'poor-help'});

    print "\n";
    print "%files -f files-help-$datap->{$curLang}{'locale'}.txt -n OpenOffice_org-help-$curLang\n";
    print "%defattr(-,root,root)\n";

    foreach  my $line (@{$datap->{$curLang}{'files-help'}}) {
        print "$line\n";
    }
}

sub write_lang_section($$$)
{
    my ($datap, $write_single_lang_section, $section_name) = @_;
    my $curLevel = 0;

    write_section_comment($section_name);
    
    foreach my $curLang (sort keys %{$datap}) {
	$curLevel = write_level_begin($curLevel, $datap->{$curLang}{'level'});
	& {$write_single_lang_section} ($datap, $curLang);
    }

    $curLevel = write_level_end($curLevel);
}

sub usage()
{
    print "This tool generates pieces of the l10n and help spec files\n\n" .

          "Usage:\n".
	  "\tooo-l10n-gen-spec [--help] [--l10n|--help] data_file...\n\n" .
	
	  "Options:\n" .
	  "\t--help: prints this help\n" .
	  "\t--gen-l10n: print onyl the l10n pieces\n" .
	  "\t--gen-help: print only the help pieces\n" .
	  "\tdata_file: input file\n";
}


# info about localizations
# it is a hash, the key is the lang id, e.g. "en-US", the value is:
#	  a hash, keys introduce perl-like structure items:
#	      'long_name' 	 	... string, long name of the language, e.g. "American"
#	      'level' 	 		... integer, level in whih it should get build, e.g. '1'
#					    it is the level in the rpm spec to do a reduced build
#	      'locale' 	 		... string, it is actually the lang id with undersore instead of dash,
#					    e.g. "en_US"
#	      'poor-help' 	 	... integer, is defined when the help is poor and the help package
#					    is not created
#	      'package-l10n' 		... array of strings that should be added to the %package section for
#                                           the given language
#	      'package-help' 		... array of strings that should be added to the %package section for
#                                           the given help package
#	      'files-l10n' 		... array of strings that should be added to the %files section for
#                                           the given language
#	      'package-help' 		... array of strings that should be added to the %files section for
#                                           the given help package
my %data;
my $l10n = 1;
my $help = 1;


for my $arg (@ARGV) {
    if ($arg eq '--help' || $arg eq '-h') {
	usage;
	exit 0;
    } elsif ($arg =~ m/--gen-l10n/) {
	$l10n = 1;
	$help = 0;
    } elsif ($arg =~ m/--gen-help/) {
	$l10n = 0;
	$help = 1;
    } else {
	if (-f $arg) {
	    read_data($arg, \%data);
	} else {
	    die "File does not exist: $arg\n";
	}
    }
}

write_generated_section_start();
write_lang_section(\%data, \&write_l10n_package_section, "l10n packages") if ($l10n);
write_lang_section(\%data, \&write_help_package_section, "help packages") if ($help);
write_generated_section_end();

write_generated_section_start();
print "# FIXME: call script that links noarch stuff to the ooo-home\n";
print "# it will be better to fix OOo to find the noarch stuff\n";
print "# on the right place\n";
print "\n";
write_lang_section(\%data, \&write_l10n_scripts_section, "l10n scripts") if ($l10n);
write_lang_section(\%data, \&write_help_scripts_section, "help scripts") if ($help);
write_lang_section(\%data, \&write_l10n_files_section, "l10n files") if ($l10n);
write_lang_section(\%data, \&write_help_files_section, "help files") if ($help);
write_generated_section_end();
openSUSE Build Service is sponsored by