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