1#!@PERL@ -w
2# -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4  -*-
3
4#############################################################################
5## Copyright (C) 2005-2007 Novell, Inc.
6## Copyright (C) 2005-2007 Rodney Dawes
7##
8## Authors: Rodney Dawes <dobey.pwns@gmail.com>
9##
10
11use strict;
12use XML::Simple;
13use Getopt::Long;
14
15my $PROGRAM = "icon-name-mapping";
16
17my $condir;
18my $LN_S = ($^O eq 'MSWin32' ? 'cp' : 'ln -s');
19my $mapdir = $ENV{INU_DATA_DIR} || "@DATADIR@";
20
21############################################################################
22my @default_getopt_config = ("permute", "pass_through", "bundling",
23			     "no_auto_abbrev", "no_ignore_case");
24
25Getopt::Long::Configure (@default_getopt_config);
26GetOptions ("help|h" => \&usage,
27	    "context|c=s" => \$condir);
28
29
30############################################################################
31
32sub tls_load_mapping {
33    my $filename = shift;
34
35    my $mapping = XML::Simple::XMLin ($filename,
36				      keyattr => [ qw(dir) ],
37				      forcearray => [ qw(context icon link) ]);
38    return $mapping;
39}
40
41sub tls_map_icons {
42    my $mapping = shift;
43    my $dirname = shift;
44
45    print "Setting up icon mapping for: $dirname\n";
46    chomp (my $cwd = `pwd`);
47    chdir $dirname;
48    foreach my $icon (@{$mapping->{context}->{$dirname}->{icon}}) {
49        if (-f "$icon->{name}.png") {
50            foreach my $legacy (@{$icon->{link}}) {
51                system ("$LN_S $icon->{name}.png $legacy.png")
52                    if (! -e "$legacy.png");
53            }
54        } elsif (-f "$icon->{name}.svg") {
55            foreach my $legacy (@{$icon->{link}}) {
56                system ("$LN_S $icon->{name}.svg $legacy.svg")
57                    if (! -e "$legacy.svg");
58            }
59        }
60
61        if (-f "$icon->{name}.icon") {
62            foreach my $legacy (@{$icon->{link}}) {
63                system ("$LN_S $icon->{name}.icon $legacy.icon")
64                    if (! -e "$legacy.icon");
65            }
66        }
67    }
68    chdir $cwd;
69}
70
71sub usage {
72    print "Usage: $PROGRAM [OPTIONS] ...
73
74  -c, --context=<dirname>       Set up mapping for Context <dirname>
75
76This utility must be run from the <theme>/<size> directory, with a
77context passsed in as the argument.
78
79";
80
81    exit 1;
82}
83
84if (not defined $condir) {
85    usage ();
86} else {
87    my $iconmap = tls_load_mapping ("$mapdir/legacy-icon-mapping.xml");
88    tls_map_icons ($iconmap, $condir);
89}
90