#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;
use Linux::DesktopFiles;
use List::Util 'first';
use Data::Printer;

my $d = Linux::DesktopFiles->new(
    with_icons => 1,
    full_icon_paths => 1,
    skip_svg_icons => 1,
    icon_dirs_last => ['/usr/share/icons/gnome'],
);

my $f = $d->parse_desktop_files();

for my $cat (keys %$f) {
    my @apps=@{$f->{$cat}};
    my $menu_name='/Applications/'.ucfirst(lc($cat)).'Panel';
    say "DestroyMenu $menu_name";
    say "AddToMenu $menu_name";
    for my $app (@apps) {
        next unless $app->{Icon};
        printf qq{+ "%%%s%%\$[gt.%s]" Exec exec "%s"\n},
            $app->{Icon},$app->{Name},$app->{Exec};
    }
    say '';
}

my $buttons=0;
for my $cat (keys %$f) {
    my @apps=@{$f->{$cat}};
    my $app=first { $_->{Icon} } @apps;
    next unless $app;
    ++$buttons;

    my $menu_name='/Applications/'.ucfirst(lc($cat)).'Panel';
    printf qq{*ApplicationPanel: (1x1, Size 22 22, Icon "%s", \\\n},$app->{Icon};
    printf qq{Action (Mouse 1) Exec exec "%s", \\\n},$app->{Exec};
    printf qq{Action (Mouse 3) Menu %s \$[MenuPosition_S] )\n},$menu_name;
}

say "SetEnv ApplicationPanelLength $buttons";
