summaryrefslogtreecommitdiff
path: root/development/sort_desktop_files.pl
blob: e553b9eeff836bf82a54a34d5cb700535d4dc230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/perl -w
# Copyright (c) 2002,2003 by 
# Bruno Rodrigues <bruno.rodrigues@litux.org>
# under the GNU GPL license 2.0 or newer
# sort_desktop_files.pl,v 1.1.2.1 2003/07/01 02:22:56 davipt Exp
# -----------------------------------------------------
# This script reads a .desktop file and sorts its values
# in the following format:
# [Desktop Entry]
# field1=val1  \
# field2=val2   > Sorted by field name
# ...          /
# Name=name
# Comment=comment
# Name[lang1]=...    \
# Comment[lang1]=...  > Sorted by language name
# Name[lang2]=...    /

use strict;
use vars qw{$line %data %tr};

%data = (); %tr = (); 

# Read first line and make sure it's "[Desktop Entry]"
$line = <>; chop($line);
die "E: File does not start with [Desktop Entry] ($line)\n" unless $line =~ /^\[Desktop Entry\]$/;

my $end=0;
while($line = <>) {
	# Ignore fields without values
	next if $line =~ /^\s*([^\s]+)\s*=\s*$/; 

	# Ignore empty line in the end
	if($line =~ /^\s*$/) { 
		if($end==0) { $end=1; next;
		} else { die "E: Empty line in middle of file\n";
		}
	}

	# Die if line is not "field = value"
	die "E: Error in line - not field=value ($line)\n" unless $line =~ /^\s*([^\s]+)\s*=\s*(.+?)\s*$/;

	my ($key, $data) = ($1, $2);

	# Grab Name[lang] and Comment[lang]
	if($key =~ /^(Name|Comment)\[(.+)\]$/i) {
		$tr{$2}{$1} = $data;
	} 
	# Grab Name and Comment
	elsif($key =~ /^(Name|Comment)$/i) {
		$tr{"0"}{$1} = $data;
	} 
	# Die if there is other field[x]
	elsif($key =~ /\[|\]/) {
		die "E: Error in line - unknown field with [] ($line)\n";
	} 
	# Grab regular fields
	else {
		$data{$1} = $2;
	}
}


print "[Desktop Entry]\n";
foreach my $key (sort keys %data) {
	print "$key=".$data{$key}."\n";
}
foreach my $key (sort keys %tr) {
	my %tr2 = %{$tr{$key}};
	foreach my $k2 (reverse sort keys %tr2) {
		print $k2;
		print "[$key]" unless $key eq "0";
		print "=". $tr2{$k2}. "\n";
	}
}