1#!/usr/bin/perl
2# *************************************************************
3#
4#  Licensed to the Apache Software Foundation (ASF) under one
5#  or more contributor license agreements.  See the NOTICE file
6#  distributed with this work for additional information
7#  regarding copyright ownership.  The ASF licenses this file
8#  to you under the Apache License, Version 2.0 (the
9#  "License"); you may not use this file except in compliance
10#  with the License.  You may obtain a copy of the License at
11#
12#    http://www.apache.org/licenses/LICENSE-2.0
13#
14#  Unless required by applicable law or agreed to in writing,
15#  software distributed under the License is distributed on an
16#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17#  KIND, either express or implied.  See the License for the
18#  specific language governing permissions and limitations
19#  under the License.
20#
21# *************************************************************
22use File::Basename;
23$basedir = dirname($0);
24$productname = 'Apache OpenOffice';
25$mimedir = $basedir.'/../mimetypes';
26
27$FS= ' ';		# field seperator (space) - for documents.ulf
28$, = "\n";		# set output field separator (newline)
29$\ = "\n";		# set output record separator (newline)
30
31## get list of components and corresponding translations from documents.ulf
32open(DOCUMENTS_ULF, $ARGV[0]) || die 'Cannot open "documents.ulf".';
33while (<DOCUMENTS_ULF>) {
34	if (/^\[/) {
35		# section starts
36		s/^\[(.*)]/$1/;
37		chomp;
38		$module = $_;
39    } else {
40		# translated strings
41		($lang,$junk,$comment) = split($FS, $_, 3);
42		$comment =~ s/^"(.*)"$/$1/;
43		$comment =~ s/%PRODUCTNAME/$productname/;
44		chomp $lang;
45		chomp $comment;
46		if ($lang eq "en-US") {
47			$value = "    <comment>$comment</comment>";
48		} else {
49			$value = '    <comment xml:lang="'.$lang.'">'.$comment.'</comment>';
50		}
51		push(@{$mimehash{$module}}, $value) unless $lang eq "";
52	}
53}
54close DOCUMENTS_ULF;
55
56## creating the xml on stdout
57print '<?xml version="1.0" encoding="UTF-8"?>';
58print '<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">';
59
60foreach $component (sort(keys %mimehash)) {
61	print STDERR "Processing entries for $component";
62	# mimetype and glob
63	getMimedata($component);
64	print '  <mime-type type="'.$mimetype.'">';
65	print (sort({customsort($a) cmp customsort($b)} @{$mimehash{$component}}));
66	print '    <glob pattern="'.$glob.'"/>';
67    if ( $component =~ /oasis/ ) {
68        print '    <magic'.( $mimetype =~ /-/  ? ' priority="60"' : '').'>';
69        print '        <match type="string" offset="38" value="'.$mimetype.'"/>';
70        print '    </magic>';
71    }
72	print '  </mime-type>';
73}
74print '</mime-info>';
75
76sub customsort {
77		# sort <comment> before <comment xml:lang...>
78		$in = shift;
79		$in =~ tr/>/A/;
80		$in =~ tr/ /Z/;
81		return $in;
82}
83
84## get mime-type and glob from ../mimetype/*.desktop
85sub getMimedata {
86	$desktop_name = shift;
87	$desktop_file = $mimedir.'/'.$desktop_name.'.desktop';
88	$mimetype = "";
89	$glob = "";
90	open(DESKTOP_FILE, $desktop_file) || die 'Cannot open "desktop"-file.'.$!;
91	while (<DESKTOP_FILE>) {
92	    ## get mimetype
93	    if (/^MimeType=/) {
94		    s/^MimeType=(.*)\n$/$1/;
95		    $mimetype = "$_";
96	    }
97	    ## get glob
98	    if (/^Patterns=/) {
99		    s/^Patterns=(.*)\n$/\1/;
100		    $glob = "$_";
101	    }
102	}
103	close DESKTOP_FILE;
104	# die if we cannot determine the glob-pattern or mimtetype
105	die "Could not get mime-type fom $desktop_file" if ($mimetype eq "");
106	die "Could not get glob-pattern fom $desktop_file" if ($glob eq "");
107}
108
109## END vim: set ts=4:
110