1*cdf0e10cSrcweir#!/usr/bin/perl
2*cdf0e10cSrcweiruse File::Basename;
3*cdf0e10cSrcweir$basedir = dirname($0);
4*cdf0e10cSrcweir$productname = 'OpenOffice.org';
5*cdf0e10cSrcweir$mimedir = $basedir.'/../mimetypes';
6*cdf0e10cSrcweir
7*cdf0e10cSrcweir$FS= ' ';		# field seperator (space) - for documents.ulf
8*cdf0e10cSrcweir$, = "\n";		# set output field separator (newline)
9*cdf0e10cSrcweir$\ = "\n";		# set output record separator (newline)
10*cdf0e10cSrcweir
11*cdf0e10cSrcweir## get list of components and corresponding translations from documents.ulf
12*cdf0e10cSrcweiropen(DOCUMENTS_ULF, $ARGV[0]) || die 'Cannot open "documents.ulf".';
13*cdf0e10cSrcweirwhile (<DOCUMENTS_ULF>) {
14*cdf0e10cSrcweir	if (/^\[/) {
15*cdf0e10cSrcweir		# section starts
16*cdf0e10cSrcweir		s/^\[(.*)]/$1/;
17*cdf0e10cSrcweir		chomp;
18*cdf0e10cSrcweir		$module = $_;
19*cdf0e10cSrcweir    } else {
20*cdf0e10cSrcweir		# translated strings
21*cdf0e10cSrcweir		($lang,$junk,$comment) = split($FS, $_, 3);
22*cdf0e10cSrcweir		$comment =~ s/^"(.*)"$/$1/;
23*cdf0e10cSrcweir		$comment =~ s/%PRODUCTNAME/$productname/;
24*cdf0e10cSrcweir		chomp $lang;
25*cdf0e10cSrcweir		chomp $comment;
26*cdf0e10cSrcweir		if ($lang eq "en-US") {
27*cdf0e10cSrcweir			$value = "    <comment>$comment</comment>";
28*cdf0e10cSrcweir		} else {
29*cdf0e10cSrcweir			$value = '    <comment xml:lang="'.$lang.'">'.$comment.'</comment>';
30*cdf0e10cSrcweir		}
31*cdf0e10cSrcweir		push(@{$mimehash{$module}}, $value) unless $lang eq "";
32*cdf0e10cSrcweir	}
33*cdf0e10cSrcweir}
34*cdf0e10cSrcweirclose DOCUMENTS_ULF;
35*cdf0e10cSrcweir
36*cdf0e10cSrcweir## creating the xml on stdout
37*cdf0e10cSrcweirprint '<?xml version="1.0" encoding="UTF-8"?>';
38*cdf0e10cSrcweirprint '<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">';
39*cdf0e10cSrcweir
40*cdf0e10cSrcweirforeach $component (sort(keys %mimehash)) {
41*cdf0e10cSrcweir	print STDERR "Processing entries for $component";
42*cdf0e10cSrcweir	# mimetype and glob
43*cdf0e10cSrcweir	getMimedata($component);
44*cdf0e10cSrcweir	print '  <mime-type type="'.$mimetype.'">';
45*cdf0e10cSrcweir	print (sort({customsort($a) cmp customsort($b)} @{$mimehash{$component}}));
46*cdf0e10cSrcweir	print '    <glob pattern="'.$glob.'"/>';
47*cdf0e10cSrcweir    if ( $component =~ /oasis/ ) {
48*cdf0e10cSrcweir        print '    <magic'.( $mimetype =~ /-/  ? ' priority="60"' : '').'>';
49*cdf0e10cSrcweir        print '        <match type="string" offset="38" value="'.$mimetype.'"/>';
50*cdf0e10cSrcweir        print '    </magic>';
51*cdf0e10cSrcweir    }
52*cdf0e10cSrcweir	print '  </mime-type>';
53*cdf0e10cSrcweir}
54*cdf0e10cSrcweirprint '</mime-info>';
55*cdf0e10cSrcweir
56*cdf0e10cSrcweirsub customsort {
57*cdf0e10cSrcweir		# sort <comment> before <comment xml:lang...>
58*cdf0e10cSrcweir		$in = shift;
59*cdf0e10cSrcweir		$in =~ tr/>/A/;
60*cdf0e10cSrcweir		$in =~ tr/ /Z/;
61*cdf0e10cSrcweir		return $in;
62*cdf0e10cSrcweir}
63*cdf0e10cSrcweir
64*cdf0e10cSrcweir## get mime-type and glob from ../mimetype/*.desktop
65*cdf0e10cSrcweirsub getMimedata {
66*cdf0e10cSrcweir	$desktop_name = shift;
67*cdf0e10cSrcweir	$desktop_file = $mimedir.'/'.$desktop_name.'.desktop';
68*cdf0e10cSrcweir	$mimetype = "";
69*cdf0e10cSrcweir	$glob = "";
70*cdf0e10cSrcweir	open(DESKTOP_FILE, $desktop_file) || die 'Cannot open "desktop"-file.'.$!;
71*cdf0e10cSrcweir	while (<DESKTOP_FILE>) {
72*cdf0e10cSrcweir	    ## get mimetype
73*cdf0e10cSrcweir	    if (/^MimeType=/) {
74*cdf0e10cSrcweir		    s/^MimeType=(.*)\n$/$1/;
75*cdf0e10cSrcweir		    $mimetype = "$_";
76*cdf0e10cSrcweir	    }
77*cdf0e10cSrcweir	    ## get glob
78*cdf0e10cSrcweir	    if (/^Patterns=/) {
79*cdf0e10cSrcweir		    s/^Patterns=(.*)\n$/\1/;
80*cdf0e10cSrcweir		    $glob = "$_";
81*cdf0e10cSrcweir	    }
82*cdf0e10cSrcweir	}
83*cdf0e10cSrcweir	close DESKTOP_FILE;
84*cdf0e10cSrcweir	# die if we cannot determine the glob-pattern or mimtetype
85*cdf0e10cSrcweir	die "Could not get mime-type fom $desktop_file" if ($mimetype eq "");
86*cdf0e10cSrcweir	die "Could not get glob-pattern fom $desktop_file" if ($glob eq "");
87*cdf0e10cSrcweir}
88*cdf0e10cSrcweir
89*cdf0e10cSrcweir## END vim: set ts=4:
90