#!/usr/bin/perl -w
=head1 NAME

=head1 SYNOPSIS

=head1 DESCRIPTION

Perl script to replace logo bitmaps/pngs in standard square sizes (Linux only).
It runs a 'find' to get all qt*logo.png, determines their size by running the 'file'
command and copies the matching replacement from a fixed location.

Adapt the variables pointing to the new logos and run it from the root of the repository.

v 1.0 5.8.2016
v 1.1 5.8.2016 non-std sizes

QTBUG-55137

=cut

use strict;
use IO::File;
use File::Find;
use File::Spec;
use File::Copy;
use Cwd;

# -- Adapt below vars to location of the new icons
my $root = $ENV{'HOME'} . '/work/logo/screen/qt_logo';
my $fileprefix= 'qt_logo_green_';

die ('Invalid root' . $root) unless -d $root;

my $foundCount = 0;
my $replacedCount = 0;

sub findHandler
{
    my ($fileName) = $_;
    return unless $fileName =~ /qt-?logo.png/;
    $foundCount++;
    my $o = `file "$File::Find::name"`;
    if ($o =~ /^.* (\d+) x (\d+),/ ) {
	my ($w, $h) = ($1, $2);
	my $oldSize = -s $File::Find::name;
	print $File::Find::name,' ', $w, 'x', $h, "\n";
	my $replacement = $root . '/' . $fileprefix . $w;
	$replacement .= 'x' . $h unless $w == $h;
	$replacement .= '.png';
	if (-f $replacement) {
	    my $newSize = -s $replacement;
	    print "### Replacing\n    ", $File::Find::name, ' ', $oldSize, 'bytes ',"\n    by ",
	          $replacement, ' ', $newSize, "bytes\n";
	    File::Copy::copy($replacement, $File::Find::name) or die ('Fail!');
	    $replacedCount++
	} else {
	    warn ($File::Find::name . ' has nonstandard size ' . $w . 'x' . $h);
	}
    }
}

find({ wanted => \&findHandler}, getcwd());

print 'Replaced ', $replacedCount, '/', $foundCount, "\n";
