core: Remove updateSQLResource.sh, cleanup docs
[quassel.git] / icons / import / import_theme.pl
1 #!/usr/bin/perl
2
3 # This script scans the Quassel source for required icons and imports the needed
4 # icons (and only them) from a full icon theme.
5 # Additional icons can be specified in whitelisted-icons; you can also blacklist icons.
6 #
7 # NOTE: Unless you are a Quassel developer and need to bump the icons we ship, you shouldn't
8 #       need to use this script!
9
10 # USAGE: ./import/import_theme.pl $srcthemedir $themename
11 #
12 # Examples:
13 #   import_theme.pl ~/oxygen-icons oxygen
14 #   import_theme.pl ~/breeze-icons/icons breeze
15 #   import_theme.pl ~/breeze-icons/icons-dark breeze-dark
16
17 use strict;
18 use warnings;
19
20 use File::Basename;
21 use File::Find;
22 use File::Spec;
23
24 my $scriptroot = File::Basename::dirname(File::Spec->rel2abs($0));
25 my $srcroot = File::Spec->catdir($scriptroot, "..", "..");
26 my $srcdir = File::Spec->catdir($srcroot, "src");
27
28 my $srcthemedir = shift;
29 my $themename = shift;
30
31 # Sanity checks
32 die "Theme directory must be given\n" unless length $srcthemedir;
33 die "Theme directory \"$srcthemedir\" not found\n" unless -e $srcthemedir and -d $srcthemedir;
34 die "Theme name must not be empty\n" unless length $themename;
35
36 my $destbasedir  = File::Spec->catdir($srcroot, "3rdparty", "icons");
37 my $destthemedir = File::Spec->catdir($destbasedir, $themename);
38
39 my $whitelistfile = "$scriptroot/whitelisted-icons";
40 my $blacklistfile = "$scriptroot/blacklisted-icons";
41
42 my %req_icons;
43 my %found_icons;
44 my %whitelist;
45 my %blacklist;
46
47 # Add whitelisted icons
48 open WHITELIST, "<$whitelistfile" or die "Could not open $whitelistfile\n";
49 while(<WHITELIST>) {
50   s/#.*//;
51   next unless my ($name) = /([-\w]+)\s*/;
52   $req_icons{$name} = 1;
53 }
54 close WHITELIST;
55
56 # Load the icon blacklist
57 open BLACKLIST, "<$blacklistfile" or die "Could not open $blacklistfile\n";
58 while(<BLACKLIST>) {
59   s/#.*//;
60   next unless my ($name) = /([-\w]+)\s*/;
61   $blacklist{$name} = 1;
62 }
63 close BLACKLIST;
64
65 # We now grep the source for icon::get() to find required icons
66 print "Grepping $srcdir for required icons...\n";
67 my @results = `grep -r icon::get $srcdir`;
68 foreach(@results) {
69   next unless my (@names) = /\W+icon::get\((?:\"([-\w]+)\")|(?:\{\s*\"([-\w]+)\"(?:,\s*\"([-\w]+)\")*\s*\})/;
70   foreach(@names) {
71     $req_icons{$_} = 1 unless not defined or exists $blacklist{$_};
72   }
73 }
74
75 # Clean old output dir
76 print "Removing old $destthemedir...\n";
77 system("rm -rf $destthemedir");
78
79 # Now copy the icons
80 my %scalables;
81
82 print "Copying icons from $srcthemedir...\n";
83 opendir (BASEDIR, "$srcthemedir") or die "Could not open theme basedir\n";
84 my $scalableFound = 0;
85 foreach my $parent (readdir BASEDIR) {
86   next unless (-d "$srcthemedir/$parent");
87   $scalableFound = $scalableFound ? 1 : $parent eq 'scalable';
88   next if $parent eq '.' or $parent eq '..' or $parent eq 'scalable' or $parent =~ /\..*/;
89   my $ischildcat = $parent =~ /\d+x\d+/ ? 1 : 0;
90   opendir (SIZEDIR, "$srcthemedir/$parent") or die "Could not open dir $parent\n";
91   foreach my $child (readdir SIZEDIR) {
92     next if $child eq '.' or $child eq '..';
93     my $cat = $ischildcat ? $child : $parent;
94     opendir (CATDIR, "$srcthemedir/$parent/$child") or die "Could not open category dir\n";
95     foreach my $icon (readdir CATDIR) {
96       my $iconname = $icon;
97       $iconname =~ s/\.png$//;
98       $iconname =~ s/\.svg$//;
99       next unless exists $req_icons{$iconname};
100       $scalables{$cat}{$iconname} = 1;
101       system "mkdir -p $destthemedir/$parent/$child" and die "Could not create category dir\n";
102       system "cp -aL $srcthemedir/$parent/$child/$icon $destthemedir/$parent/$child"
103         and die "Error while copying file $parent/$child/$icon\n";
104       print "Copy: $srcthemedir/$parent/$child/$icon\n";
105       $found_icons{$iconname} = 1;
106     }
107     closedir CATDIR;
108   }
109   closedir SIZEDIR;
110 }
111 closedir BASEDIR;
112
113 # Copy scalables
114 if ($scalableFound) {
115   foreach my $cat (keys %scalables) {
116     system "mkdir -p $destthemedir/scalable/$cat" and die "Could not create category dir\n";
117     foreach my $scalable (keys %{$scalables{$cat}}) {
118       system "cp -aL $srcthemedir/scalable/$cat/$scalable.svgz $destthemedir/scalable/$cat/$scalable.svgz";
119     }
120   }
121 }
122
123 # Warn if we have still icons left
124 foreach my $icon (keys %req_icons) {
125   next if defined $found_icons{$icon};
126   print "Warning: Missing icon $icon\n";
127 }
128
129 # Copy license etc.
130 system "cp -aL $srcthemedir/AUTHORS* $srcthemedir/CONTRIBUTING* $srcthemedir/COPYING* $srcthemedir/index.theme $destthemedir/ | true";
131
132 # Generate .qrc
133 #my $qrcfile = $themename."_icon_theme.qrc";
134 #$qrcfile =~ s/-/_/g;
135 my $qrcfile = File::Spec->catdir($destbasedir, ($themename."_icon_theme.qrc") =~ s/-/_/gr);
136 print "Generating $qrcfile...\n";
137
138 my @file_list = ();
139
140 sub push_icon_path {
141   return unless /\.png$/ or /\.svg$/ or /^index.theme$/;
142   push @file_list, "    <file>".$File::Find::name =~ s|^$destbasedir/||gr."</file>";
143 }
144
145 find(\&push_icon_path, $destthemedir);
146 @file_list = sort(@file_list);
147 my $files = join "\n", @file_list;
148
149 my $qrc = "<RCC>\n"
150          ."  <qresource prefix=\"/icons\">\n"
151          ."$files\n"
152          ."  </qresource>\n"
153          ."</RCC>\n";
154
155 open QRC, ">$qrcfile" or die "Could not open $qrcfile for writing!\n";
156 print QRC $qrc;
157 close QRC;
158
159 print "Done.\n";