Index: trunk/tools/webfonts-tools/webfonts-mover.pl |
— | — | @@ -0,0 +1,113 @@ |
| 2 | +#!/usr/bin/perl |
| 3 | + |
| 4 | +use 5.010; |
| 5 | + |
| 6 | +use strict; |
| 7 | +use warnings; |
| 8 | + |
| 9 | +use English '-no_match_vars'; |
| 10 | +use Getopt::Long; |
| 11 | +use Carp; |
| 12 | +use File::Find; |
| 13 | +use File::Basename; |
| 14 | +use File::Path 'make_path'; |
| 15 | +use File::Copy; |
| 16 | +use Cwd 'abs_path'; |
| 17 | + |
| 18 | +our $VERSION = 0.1; |
| 19 | + |
| 20 | +my $program_name = __FILE__; |
| 21 | + |
| 22 | +my %source_fonts; |
| 23 | +my %fonts_to_move; |
| 24 | +my %affected_system_dirs; |
| 25 | + |
| 26 | +my $source_fonts_dir = '../../extensions/WebFonts/fonts'; |
| 27 | +my $removed_fonts_dir = 'removed_fonts'; |
| 28 | +my $system_fonts_dir = '/usr/share/fonts'; |
| 29 | +my $dry = 0; |
| 30 | +my $restore = 0; |
| 31 | + |
| 32 | +my $getopt_success = GetOptions( |
| 33 | + 'source_fonts_dir=s' => \$source_fonts_dir, |
| 34 | + 'removed_fonts_dir=s' => \$removed_fonts_dir, |
| 35 | + 'dry' => \$dry, |
| 36 | + 'restore' => \$restore, |
| 37 | +); |
| 38 | + |
| 39 | +if (not $getopt_success) { |
| 40 | + croak('Fatal error reading command-line options. Exiting.'); |
| 41 | +} |
| 42 | + |
| 43 | +$source_fonts_dir = abs_path($source_fonts_dir); |
| 44 | + |
| 45 | +File::Find::find(\&source_fonts, $source_fonts_dir); |
| 46 | + |
| 47 | +move_fonts($restore ? 'system' : 'removed'); |
| 48 | + |
| 49 | +# Rebuild the cache |
| 50 | +foreach my $system_dir (keys %affected_system_dirs) { |
| 51 | + if ($restore) { |
| 52 | + $system_dir =~ s{\A$removed_fonts_dir}{$system_fonts_dir}xms; |
| 53 | + } |
| 54 | + say "$program_name: rebuilding cache for dir $system_dir"; |
| 55 | + if (not $dry) { |
| 56 | + system "fc-cache -fv $system_dir"; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +exit; |
| 61 | + |
| 62 | +sub move_fonts { |
| 63 | + my ($target) = @_; |
| 64 | + |
| 65 | + File::Find::find(\&fonts_to_move, |
| 66 | + $restore ? $removed_fonts_dir : $system_fonts_dir); |
| 67 | + FONT_TO_MOVE: |
| 68 | + foreach my $font_to_move (keys %fonts_to_move) { |
| 69 | + my $target_dir = dirname($font_to_move); |
| 70 | + if ($restore) { |
| 71 | + $target_dir =~ s{\A$removed_fonts_dir}{$system_fonts_dir}xms; |
| 72 | + } |
| 73 | + else { |
| 74 | + $target_dir =~ s{\A$system_fonts_dir}{$removed_fonts_dir}xms; |
| 75 | + } |
| 76 | + |
| 77 | + say "$program_name: moving $font_to_move to $target_dir"; |
| 78 | + if (not $dry) { |
| 79 | + if (not -d $target_dir) { |
| 80 | + make_path($target_dir, { verbose => 1 }); |
| 81 | + } |
| 82 | + move($font_to_move, $target_dir); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return; |
| 87 | +} |
| 88 | + |
| 89 | +# Callback for File::Find |
| 90 | +sub source_fonts { |
| 91 | + if (/^.*[.]ttf\z/ixms) { |
| 92 | + $source_fonts{ basename($File::Find::name) } = |
| 93 | + abs_path($File::Find::name); |
| 94 | + } |
| 95 | + |
| 96 | + return; |
| 97 | +} |
| 98 | + |
| 99 | +# Callback for File::Find |
| 100 | +sub fonts_to_move { |
| 101 | + my $basename = basename($File::Find::name); |
| 102 | + my $dirname = dirname($File::Find::name); |
| 103 | + |
| 104 | + if (exists $source_fonts{$basename}) { |
| 105 | + $fonts_to_move{$File::Find::name} = 0; |
| 106 | + say "$program_name: found $basename in dir $dirname"; |
| 107 | + $affected_system_dirs{$dirname} = 0; |
| 108 | + } |
| 109 | + |
| 110 | + return; |
| 111 | +} |
| 112 | + |
| 113 | +__END__ |
| 114 | + |