# Uage: Module FvwmPerl -l fvwm-menu-desktop-config.fpl

# This script generates an FvwmForm similar to the FvwmForm-Desktop by
# Dan Espen but inserts the found xdg menus dynamically into the Form
# before processed.
# Author: Thomas Funk <t.funk@web.de>
# Version: 1.4

package MenuConfig;
use File::Basename;
use strict;
use warnings;

my $modname = 'FvwmForm-XDGMenu-Config';
my $configtmp = "$ENV{'FVWM_USERDIR'}/.XDGMenu-Config.fvwm2rc";

open(MSG ,">$configtmp") || die "Error $!";

my $all = `fvwm-menu-desktop --get-menus all`;
my $selected = `fvwm-menu-desktop --get-menus desktop`;

my @all_filelist = split(/ /,$all);
my @selected_filelist = split(/ /,$selected);

my %all_menus = ();
my %selected__menus = ();
my $max_length = 0;
foreach my $path (@selected_filelist) {
    my ($filename, $directories, $suffix) = fileparse($path, qr/\.[^.]*/);
    push (@{$selected__menus{$directories}}, $filename);
}

my $i = 1;
foreach my $path (@all_filelist) {
    # qr matched against the end of the $filename.
    # The matching portion is removed and becomes the $suffix.
    my ($filename, $directories, $suffix) = fileparse($path, qr/\.[^.]*/);
    my $name = "MEN" . $filename;
    push (@{$all_menus{$directories}{$i}}, ($filename, $name, "off"));
    next if !defined $selected__menus{$directories};
    foreach my $hit (@{$selected__menus{$directories}}) {
        if ($filename eq $hit) {
            pop (@{$all_menus{$directories}{$i}});
            push (@{$all_menus{$directories}{$i}}, "on");
        }
    }
    $max_length = length($filename) if ($max_length < length($filename));
    $i++;
}

my $fvwmform_commands = "
DestroyModuleConfig  ${modname}: *
*${modname}: Title      \"\$[gt.Fvwm XDGMenu Config]\"
*${modname}: WarpPointer
#*${modname}: Line       center
#*${modname}: Text       \"\$[gt.Fvwm XDGMenu Config]\"
#*${modname}: Line
*${modname}: Separator
*${modname}: Line       center
*${modname}: Text       \"\$[gt.Available Menus]\"
*${modname}: Line
";

if (scalar keys %all_menus != 0) {
    foreach my $key (sort( keys %all_menus)) {
        $fvwmform_commands .= "
*${modname}: Line       left
*${modname}: Text       \"\$[gt.Menus in]\"
*${modname}: Text       \" $key\"
*${modname}: Line       left
*${modname}: Selection  meth multiple
";
        my $m_count = 0;
        foreach my $count (sort(keys %{$all_menus{$key}})) {
            my $menu = $all_menus{$key}{$count};
            my $newstring = $menu->[0] . ' ' x eval($max_length-length($menu->[0]));
            $fvwmform_commands .= "*${modname}: Choice  $menu->[1] $menu->[1] $menu->[2] \"$newstring\"\n";
            $m_count++;
            if ($m_count == 4) {
                $fvwmform_commands .= "
*${modname}: Line       left
*${modname}: Selection  meth multiple
";
                $m_count = 0;
            }
        }
        $fvwmform_commands .= "
*${modname}: Line     left
*${modname}: Text     \" \"
";
    }
}
else {
    $fvwmform_commands .= "
*${modname}: Line    center
*${modname}: Text    \"\$[gt.No menus found! Check why from within a terminal with]\"
*${modname}: Line    center
*${modname}: Text    \"'fvwm-menu-desktop -v'\"
*${modname}: Line    left
*${modname}: Text    \" \"
";
}

$fvwmform_commands .= "
*${modname}: Separator
*${modname}: Line       center
*${modname}: Text       \"\$[gt.Options]\"
*${modname}: Button     continue \"\$[gt.Help]\"
*${modname}: Command    Module FvwmForm FvwmForm-XDGOptionsHelp
*${modname}: Line

*${modname}: Line       Left
*${modname}: Text       \"\$[gt.Include in Menu?    ]\"
*${modname}: Selection  SelItype single
*${modname}: Choice     IncludeConfig IncludeConfig off \"\$[gt.Config   ]\"
*${modname}: Choice     IncludeRegen IncludeRegen off \"\$[gt.Regenerate  ]\"
*${modname}: Choice     IncludeBoth IncludeBoth on \"\$[gt.Both   ]\"
*${modname}: Choice     IncludeNone IncludeNone off \"\$[gt.None  ]\"

*${modname}: Line       Left
*${modname}: Text       \"\$[gt.Use Icons?          ]\"
*${modname}: Selection  SelItype single
*${modname}: Choice     IconsOn  IconsOn    off \"\$[gt.Yes      ]\"
*${modname}: Choice     IconsOff IconsOff   on  \"\$[gt.No   ]\"

*${modname}: Text       \"\$[gt.  ]\"
*${modname}: Text       \"\$[gt.Icon size:          ]\"
*${modname}: Input      Size 2 \"\"
*${modname}: Text       \"\$[gt. (Default is 24)]\"

*${modname}: Line       Left
*${modname}: Text       \"\$[gt.Use Titles?         ]\"
*${modname}: Selection  SelItype single
*${modname}: Choice     TitlesOn  TitlesOn    on  \"\$[gt.Yes      ]\"
*${modname}: Choice     TitlesOff TitlesOff   off \"\$[gt.No   ]\"

*${modname}: Text       \"\$[gt.  ]\"
*${modname}: Text       \"\$[gt.Used Icon theme:    ]\"
*${modname}: Input      Theme 20 \"\"

*${modname}: Line       left
*${modname}: Text       \"\$[gt.Directory Icon:     ]\"
*${modname}: Input      DirIcon 20 \"gnome-fs-directory\"

*${modname}: Text       \"\$[gt.  ]\"
*${modname}: Text       \"\$[gt.Application Icon:   ]\"
*${modname}: Input      AppIcon 20 \"gnome-applications\"

*${modname}: Line       left
*${modname}: Text       \"\$[gt.Top Menu Name:      ]\"
*${modname}: Input      Title 20 \"XDGMenu\"

*${modname}: Text       \"\$[gt.  ]\"
*${modname}: Text       \"\$[gt.Insert Menu Into:   ]\"
*${modname}: Input      InsertInto 20 \"\"

*${modname}: Line       left
*${modname}: Text       \"\$[gt.Terminal command:   ]\"
*${modname}: Input      TermCmd 30 \"xterm -e\"
*${modname}: Text       \"\$[gt. (Used to run Terminal entries)]\"

*${modname}: Line       left
*${modname}: Text       \"\$[gt.Icon directory:     ]\"
*${modname}: Input      IconDir 30 \"~/.fvwm/icons\"
*${modname}: Text       \"\$[gt. (Directory for converted icons)]\"

*${modname}: Line       left
*${modname}: Text       \"\$[gt.Output path:        ]\"
*${modname}: Input      Path 30 \"\$FVWM_USERDIR/.XDGMenu\"
*${modname}: Text       \"\$[gt. (Full path to store output)]\"

*${modname}: Line       left
*${modname}: Line
*${modname}: Separator

*${modname}: Line
*${modname}: Line       expand
*${modname}: Button     continue \"\$[gt.Export Menu]\"
*${modname}: Command    InfoStoreAdd XDGMenuList \"\\
";

foreach my $key (keys %all_menus) {
    foreach my $file (keys %{$all_menus{$key}}) {
        my $menu = $all_menus{$key}{$file};
        $fvwmform_commands .= "\$($menu->[1]\?$key$menu->[0].menu )\\\n";
    }
}

$fvwmform_commands .= "\"
*${modname}: Command    PipeRead 'fvwm-menu-desktop \\
\$(IconsOn\?--enable-mini-icons )\\
\$(Size\?-s \$(Size) )\\
\$(TitlesOn\?--with-titles )\\
\$(TitlesOff\?--without-titles )\\
--include-items \$(IncludeConfig\?config)\\
\$(IncludeRegen\?regenerate)\\
\$(IncludeBoth\?both)\\
\$(IncludeNone\?none) \\
\$(TermCmd\?--term-cmd \"\$(TermCmd)\" )\\
\$(IconDir\?--mini-icon-dir \$(IconDir) )\\
\$(DirIcon\?--dir-icon \$(DirIcon) )\\
\$(AppIcon\?--app-icon \$(AppIcon) )\\
\$(InsertInto\?--insert-in-menu \$(InsertInto) )\\
\$(Title\?--title \$(Title) )\\
\$(Theme\?--theme \$(Theme) )\\
--set-menus \"\$[infostore.XDGMenuList]\" \\
\$(Path\? > \$(Path)) 2>> ~/.xsession-errors &&  echo \"Read \$(Path\? \$(Path))\"'

*${modname}: Button         continue \"\$[gt.Save and Regenerate]\"

# Before saving the data, remove any previously saved data:
*${modname}: Command DestroyModuleConfig ${modname}Default: *
*${modname}: Command !(/bin/echo \"# This file last created by ${modname} on: `/bin/date`.\") > \$FVWM_USERDIR/.${modname}
";

foreach my $key (keys %all_menus) {
    foreach my $count (keys %{$all_menus{$key}}) {
        my $menu = $all_menus{$key}{$count};
        $fvwmform_commands .= "*${modname}: Command !(/bin/echo '*${modname}Default: $menu->[1] \$($menu->[1]\?on) ') >> \$FVWM_USERDIR/.${modname}\n";
    }
}

$fvwmform_commands .= "
*${modname}: Command !(/bin/echo \\
  '*${modname}Default: IncludeConfig \$(IncludeConfig\?on) ' ; /bin/echo \\
  '*${modname}Default: IncludeRegen \$(IncludeRegen\?on) '  ; /bin/echo \\
  '*${modname}Default: IncludeBoth  \$(IncludeBoth\?on) '   ; /bin/echo \\
  '*${modname}Default: IncludeNone  \$(IncludeNone\?on) '   ; /bin/echo \\
  '*${modname}Default: IconsOn      \$(IconsOn\?on) '       ; /bin/echo \\
  '*${modname}Default: IconsOff     \$(IconsOff\?on) '      ; /bin/echo \\
  '*${modname}Default: Size         \$(Size) '              ; /bin/echo \\
  '*${modname}Default: TitlesOn     \$(TitlesOn\?on) '      ; /bin/echo \\
  '*${modname}Default: TitlesOff    \$(TitlesOff\?on) '     ; /bin/echo \\
  '*${modname}Default: Theme        \$(Theme) '                         \\
) >> \$FVWM_USERDIR/.${modname}

*${modname}: Command !(/bin/echo \\
  '*${modname}Default: Title        \$(Title) '         ; /bin/echo \\
  '*${modname}Default: InsertInto   \$(InsertInto) '    ; /bin/echo \\
  '*${modname}Default: Path         \$(Path) '          ; /bin/echo \\
  '*${modname}Default: TermCmd      \$(TermCmd) '       ; /bin/echo \\
  '*${modname}Default: IconDir      \$(IconDir) '       ; /bin/echo \\
  '*${modname}Default: DirIcon      \$(DirIcon) '       ; /bin/echo \\
  '*${modname}Default: AppIcon      \$(AppIcon) '       \\
) >> \$FVWM_USERDIR/.${modname}

*${modname}: Command PipeRead 'fvwm-menu-desktop'

*${modname}: Button     continue \"\$[gt.Save]\"

# Before saving the data, remove any previously saved data:
*${modname}: Command DestroyModuleConfig ${modname}Default: *
*${modname}: Command !(/bin/echo \"# This file last created by ${modname} on: `/bin/date`.\") > \$FVWM_USERDIR/.${modname}
";

foreach my $key (keys %all_menus) {
    foreach my $count (keys %{$all_menus{$key}}) {
        my $menu = $all_menus{$key}{$count};
        $fvwmform_commands .= "*${modname}: Command !(/bin/echo '*${modname}Default: $menu->[1] \$($menu->[1]\?on) ') >> \$FVWM_USERDIR/.${modname}\n";
    }
}

$fvwmform_commands .= "
*${modname}: Command !(/bin/echo \\
  '*${modname}Default: IncludeConfig \$(IncludeConfig\?on) ' ; /bin/echo \\
  '*${modname}Default: IncludeRegen \$(IncludeRegen\?on) '  ; /bin/echo \\
  '*${modname}Default: IncludeBoth  \$(IncludeBoth\?on) '   ; /bin/echo \\
  '*${modname}Default: IncludeNone  \$(IncludeNone\?on) '   ; /bin/echo \\
  '*${modname}Default: IconsOn      \$(IconsOn\?on) '       ; /bin/echo \\
  '*${modname}Default: IconsOff     \$(IconsOff\?on) '      ; /bin/echo \\
  '*${modname}Default: Size         \$(Size) '              ; /bin/echo \\
  '*${modname}Default: TitlesOn     \$(TitlesOn\?on) '      ; /bin/echo \\
  '*${modname}Default: TitlesOff    \$(TitlesOff\?on) '     ; /bin/echo \\
  '*${modname}Default: Theme        \$(Theme) '                         \\
) >> \$FVWM_USERDIR/.${modname}

*${modname}: Command !(/bin/echo \\
  '*${modname}Default: Title        \$(Title) '         ; /bin/echo \\
  '*${modname}Default: InsertInto   \$(InsertInto) '    ; /bin/echo \\
  '*${modname}Default: Path         \$(Path) '          ; /bin/echo \\
  '*${modname}Default: TermCmd      \$(TermCmd) '       ; /bin/echo \\
  '*${modname}Default: IconDir      \$(IconDir) '       ; /bin/echo \\
  '*${modname}Default: DirIcon      \$(DirIcon) '       ; /bin/echo \\
  '*${modname}Default: AppIcon      \$(AppIcon) '       \\
) >> \$FVWM_USERDIR/.${modname}

*${modname}: Button         restart    \"\$[gt.Reset]\"
*${modname}: Command        Nop
*${modname}: Button         continue \"\$[gt.Help]\"
*${modname}: Command        Module FvwmForm FvwmForm-XDGMenuHelp
*${modname}: Button         quit       \"\$[gt.Close]\"
*${modname}: Command        Nop

# Tell ${modname} to read vars from .${modname}Default file:
*${modname}: UseData .${modname} *${modname}Default
";

# We are just asked to open a window, give fvwm a chance to do it
::unlock();

$fvwmform_commands =~ s/^\s+//;    # trim leading whitespace

print MSG "$fvwmform_commands\n\n";
close(MSG);

::command(qq[
    Read $configtmp
    FvwmForm ${modname}
]);

sleep(2);
unlink $configtmp

# Local Variables:
# mode: perl
# End:
