#!/usr/bin/perl -w

use strict;
use File::Spec::Functions qw(catdir catfile);

=pod

=head1 NAME

copy_gnome_icons

=head1 SYNOPSIS

For icons files starting with "gnome-":

  copy_gnome_icons *.png

For icons files starting with "gnome-mime-":

  copy_gnome_icons --strip gnome-mime- *.png

=head1 DESCRIPTION

This script is designed to convert a collection of GNOME MIME type icons to
the Bricoalge root and to the naming convention used by Bricolage. The icons
will then be used by the Bricolage UI, assuming that the C<USE_THUMBNAILS>
F<bricolage.conf> directive has been enabled.

Be sure to set the C<$BRICOLAGE_ROOT> environment variable if it is anything
other than F</usr/local/bricolage>. The icons will bee installed into the
F<comp/media/mime> directory under the Bricolage root.

The icons are named exactly as the corresponding MIME types are named, with
the resulting PNG files installed in appropriate subdirectories. For example,
an icon named "gnome-text-html.png" will be copied to
F<comp/media/mime/text/html.png>.

This script was developed using the GNOME MIME type icons downloaded from
L<http://developer.ximian.com/images/art-devel/mime-icons.tgz>. (linked to
from L<http://developer.ximian.com/themes/icons/>), but appears as though it
would work with the files in a F<mimetypes> subdirectory of most PNG icon
distributions downloadable from L<http://art.gnome.org/themes/icon/>
(the "NoiaWarm" theme's 48 x 48 MIME type icons look pretty nice with the
Bricolage UI color scheme). Updates happily accepted.

=head1 OPTIONS

=head2 C<--strip>

GNOME MIME type icon files typically start with a string that preceeds the
MIME type they're named for. Use the C<--strip> option to specify what that
string is that needs to be stripped from the file name. The remainder of the
file name is assumed to be the full MIME type name, with a dash in place of
the usual slash. So the GNOME MIME type icon file name for
"application/msword" might be F<gnome-application-msword.png> or
F<gnome-mime-application-msword.png> or something similar. In the former
example, the default value for C<--strip>, "gnome-", can be used to strip off
the "gnome-" and leave "appplication-msword.png". In the latter example
specify "gnome-mime-" for the C<--strip> option. Note that the C<--strip>
option can be a regular expression.

=head1 SEE ALSO

=over 4

=item L<http://developer.ximian.com/themes/icons/>

The Ximian GNOME Icons page.

=item L<http://developer.ximian.com/images/art-devel/mime-icons.tgz>

The canonical MIME type icons for GNOME.

=item L<http://art.gnome.org/themes/icon/index.php>

GNOME icon themes page, where several different collections of icon themes
(some of which include the required MIME type icons) can be downloaded.

=back

=head1 AUTHOR

David Wheeler <david@kineticode.com>

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2004 Kineticode, Inc. See L<Bric::License|Bric::License> for
complete license terms and conditions. GNOME icons files are distributed
separately and under their own licenses.

=cut

BEGIN {
    # $BRICOLAGE_ROOT defaults to /usr/local/bricolage
    $ENV{BRICOLAGE_ROOT} ||= "/usr/local/bricolage";

    # use $BRICOLAGE_ROOT/lib if exists
    my $lib = catdir($ENV{BRICOLAGE_ROOT}, "lib");
    if (-e $lib) {
        $ENV{PERL5LIB} = defined $ENV{PERL5LIB} ?
          "$ENV{PERL5LIB}:$lib" : $lib;
        unshift @INC, $lib;
    }

    # make sure Bric is found
    eval { require Bric };
    die <<"   END" if $@;
   ##################################################################

   Cannot load Bricolage libraries. Please set the environment
   variable BRICOLAGE_ROOT to the location of your Bricolage
   installation or set the environment variable PERL5LIB to the
   directory where Bricolage's libraries are installed.

   The specific error encountered was as follows:

   $@

   #####################################################################
   END
}

use File::Path qw(mkpath);
use File::Copy qw(cp);
use Bric::Config qw(MASON_COMP_ROOT);
use Getopt::Long qw(:config);
use File::Basename qw(basename);

my $strip = 'gnome-';

GetOptions(
    'strip|s=s' => \$strip,
);

my $mime_dir = catdir($ENV{BRICOLAGE_ROOT}, qw(comp media mime));

my %seen;
for my $file (@ARGV) {
    $_ = basename($file);
    s/\.png$// or next;
    s/^$strip// or next;
    my ($cat, $type) = split /-/, $_, 3;
    if ($type) {
        mkpath catdir($mime_dir, $cat) unless $seen{$cat}++;
        cp $file, catfile($mime_dir, $cat, "$type.png");
    } else {
        cp $file, catfile($mime_dir, "$cat.png");
    }
}



__END__
