#!/usr/bin/perl -w # This script changes the definite article of ProductName use strict; use File::Copy; my $args = join ' ', @ARGV; my $max_level = 2; sub init_lang_data($$$$$) { my ($pdata, $lang_code, $long_name, $level, $group) = @_; my $curLang; my $upstreamLang; # we support the syntax: package_lang_code[/lo_source_lang_code] if ($lang_code =~ m/([\w\-]+)\/?([\w\-]+)?/) { $curLang = "$1"; if (defined $2) { $upstreamLang = "$2"; } else { $upstreamLang = $curLang; } } else { die "Syntax error: unknown format of the language code: $lang_code, line $."; } % {$pdata->{'lang'}{$curLang}} = (); my $pld = \% {$pdata->{'lang'}{$curLang}}; $pld->{'long_name'} = $long_name; $pld->{'level'} = $level; $pld->{'group'} = $group; $pld->{'locale'} = $upstreamLang; $pld->{'locale'} =~ s/-/_/g; $pld->{'package-help'} = (); $pdata->{'max_group'} = $group if ($group > $pdata->{'max_group'}); ($level <= $max_level) || die "Error: Maximum allowed level is $max_level, line $."; foreach my $l ($level .. $max_level) { @ {$pdata->{'level_langs_list'}[$l]} = () unless (defined $pdata->{'level_langs_list'}[$l]); push @ {$pdata->{'level_langs_list'}[$l]}, $upstreamLang; $l++; } return $pld; } sub read_data($$) { my ($data_file, $pdata) = @_; # pointer to locale data my $pld = undef; $pdata->{'max_group'} = 0; $pdata->{'level_langs_list'} = (); @ {$pdata->{'level_langs_list'}[$_]} = () foreach (0 .. $max_level); open DATA , "< $data_file" or die "Can't open '$data_file'\n"; while( my $line = ){ chomp $line; # ignore comments $line =~ s/#.*$//; if ( $line =~ m/^\s*\%lang\s+([\w\-\/]+)\s+(\w+)\s+(\d+)\s+(\d+)\s*$/ ) { # long name without without quotation marks, e.g. %lang de German 1 1 # the language code might be different for the packages for upstream file list, e.g. %lang gu-IN/gu Gujarati 2 3 $pld = init_lang_data($pdata, "$1", "$2", "$3", "$4"); } elsif ( $line =~ m/^\s*\%lang\s+([\w\-\/]+)\s+\"([\w\s]+)\"\s+(\d+)\s+(\d+)\s*$/ ) { # long name with with quotation marks, e.g. %lang en-ZA "South Africa English" 2 2 $pld = init_lang_data($pdata, "$1", "$2", "$3", "$4"); } elsif ( $line =~ /^\s*\%poor-help\s*$/ ) { $pld->{'poor-help'} = 1; } elsif ( $line =~ /^\s*\%package\s+(.*)$/ ) { push @ {$pld->{'package'}}, "$1"; } elsif ( $line =~ /^\s*$/ ) { # ignore empty line } else { die "Synrax error in $data_file, line $.\n"; } } close(DATA); } ################################## # conditional lines sub write_level_begin($$$) { my ($curLevel, $newLevel, $outp) = @_; if ($curLevel != $newLevel) { ${$outp} .= "%endif\n" if ($curLevel > 0); ${$outp} .= "%if %test_build_langs >= $newLevel\n" if ($newLevel > 0); ${$outp} .= "\n" if ($curLevel > 0); } return $newLevel; } sub write_level_end($$) { my ($curLevel, $outp) = @_; if ($curLevel > 0) { ${$outp} .= "%endif\n"; } return 0; } ############################## # simple sections sub write_do_not_edit_section($) { my $out = "###################################################################\n" . "## DO NOT EDIT THIS SPEC FILE\n" . "## Generated by:\n" . "## perl lo-help-gen-spec $args\n" . "###################################################################"; return $out; } ############################## # all langs sections sub write_metadata_section($$) { my ($pdata, $curLang) = @_; my $pld = \% {$pdata->{'lang'}{$curLang}}; my $out = ""; return $out if (defined $pld->{'poor-help'}); my $oldPackage; if ("$curLang" eq "en-US") { $oldPackage = "OpenOffice_org"; } else { $oldPackage = "OpenOffice_org-$curLang"; } $out .= "%package -n libreoffice-help-$curLang\n"; $out .= "Summary: $pld->{'long_name'} Help Localization for LibreOffice\n"; $out .= "Group: Productivity/Office/Suite\n"; $out .= "Provides: locale(libreoffice:$pld->{'locale'})\n"; $out .= "%if 0%{?suse_version} > 01120\n"; $out .= "Requires(post): coreutils\n"; $out .= "Requires(post): grep\n"; $out .= "Requires(post): libreoffice >= 3.5\n"; $out .= "Requires(postun): coreutils\n"; $out .= "Requires(postun): grep\n"; $out .= "Requires(postun): libreoffice >= 3.5\n"; $out .= "%endif\n"; $out .= "%if 0%{?suse_version} && 0%{?suse_version} <= 01120\n"; $out .= "PreReq: coreutils\n"; $out .= "PreReq: grep\n"; $out .= "PreReq: libreoffice >= 3.5\n"; $out .= "%endif\n"; $out .= "Requires: libreoffice = %{version}\n"; foreach my $line (@{$pld->{'package-help'}}) { $out .= "$line\n"; } $out .= "# compat stuff\n"; $out .= "Provides: OpenOffice_org-help-$curLang = %version\n"; $out .= "Obsoletes: OpenOffice_org-help-$curLang <= %version\n"; $out .= "Provides: $oldPackage:%_prefix/ooo-2.0/help/$curLang/default.css\n"; $out .= "\n"; $out .= "%description -n libreoffice-help-$curLang\n"; $out .= "$pld->{'long_name'} help localization for LibreOffice. The other localized stuff\n"; $out .= "is in libreoffice-l10n-$curLang.\n"; $out .= "\n"; return $out; } sub write_install_scripts_section($$) { my ($pdata, $curLang) = @_; my $pld = \% {$pdata->{'lang'}{$curLang}}; my $out = ""; return $out if (defined $pld->{'poor-help'}); $out .= "# $curLang\n"; $out .= "\n"; $out .= "%posttrans -n libreoffice-help-$curLang\n"; $out .= "%_datadir/%lo_home/link-to-ooo-home %_datadir/%lo_home/help_$pld->{'locale'}_list.txt || true\n"; $out .= "\n"; $out .= "%preun -n libreoffice-help-$curLang\n"; $out .= "test \"\$1\" = \"0\" && cp %_datadir/%lo_home/help_$pld->{'locale'}_list.txt %_datadir/%lo_home/help_$pld->{'locale'}_list.txt.postun || true\n"; $out .= "\n"; $out .= "%postun -n libreoffice-help-$curLang\n"; $out .= "test \"\$1\" = \"0\" && %_datadir/%lo_home/link-to-ooo-home --unlink %_datadir/%lo_home/help_$pld->{'locale'}_list.txt.postun || true\n"; $out .= "rm -f %_datadir/%lo_home/help_$pld->{'locale'}_list.txt.postun 2>/dev/null\n"; return $out; } sub write_files_section($$) { my ($pdata, $curLang) = @_; my $pld = \% {$pdata->{'lang'}{$curLang}}; my $out = ""; return $out if (defined $pld->{'poor-help'}); $out .= "%files -f file-lists/help_$pld->{'locale'}_list.txt -n libreoffice-help-$curLang\n"; $out .= "%defattr(-,root,root)\n"; $out .= "\n"; return $out; } ######################################################## # universal writing functions sub write_level_langs($$$) { my ($pdata, $group, $level) = @_; my $out = ""; foreach my $curLang (sort keys %{$pdata->{'lang'}}) { my $pld = \% {$pdata->{'lang'}{$curLang}}; if ( ($group == $pld->{'group'}) && ($level >= $pld->{'level'}) ) { $out .= " $pld->{'locale'}"; } } $out =~ s/^ //; $out =~ s/_/-/g; return $out; } # the text is not repeated for each locale sub write_simple_section($$) { my ($pdata, $write_section) = @_; return & {$write_section} ($pdata); } # the text is repeated for each locale # it is used on all distributions sub write_locale_sections($$$) { my ($pdata, $group, $write_single_lang_section) = @_; my $curLevel = 0; my $out = ""; foreach my $curLang (sort keys %{$pdata->{'lang'}}) { my $pld = \% {$pdata->{'lang'}{$curLang}}; if ($pld->{'group'} == $group) { $curLevel = write_level_begin($curLevel, $pld->{'level'}, \$out); $out .= & {$write_single_lang_section} ($pdata, $curLang); } } $curLevel = write_level_end($curLevel, \$out); return $out; } ######################################################## # main write function sub write_specs($$) { my ($pdata, $spec_template) = @_; my $do_not_edit = write_simple_section ($pdata, \&write_do_not_edit_section); foreach my $group (0 .. $pdata->{'max_group'}) { my $metadata = write_locale_sections ($pdata, $group, \&write_metadata_section); my $install_scripts = write_locale_sections ($pdata, $group, \&write_install_scripts_section); my $files = write_locale_sections ($pdata, $group, \&write_files_section); if ($metadata || $install_scripts || $files) { my $spec = $spec_template; $spec =~ s/.in$//; $spec =~ s/groupX/group$group/; print "Generating $spec...\n"; open TEMPLATE , "< $spec_template" or die "Can't open '$spec_template'\n"; open SPEC , "> $spec" or die "Can't open '$spec for writing'\n"; while( my $line =