#!/usr/bin/perl

# Modification History

# Changed on 05/07/11 by  (dane):
# - new option kde_config for alternate name of optional
#   kde-config command.
# - use kde4-config if kde-config isn't found.
# - if no kde-config command at all, use some defaults.

# Changed on 11/30/10 by  (dane):
# - changed DIR filehandles to scalars, they're stepping on each other.
#   was a bug in xdg-menu.
# - changed default (only) format to fvwm2 and removed logic for other wms.
# - move mainline before all subs (move it back?).
# - removed the xdg_menu cache logic.
# - Remove $flag, $MENU_TYPE, $MENU_DESTROY for gtk vs. fvwm menu mode.
#   Actually removed all GTK MENU logic.
# - Added option to menu to regenerate the menus.
# - Remove xdg option root_menu.
# - Added fvwm-menu option to use Fvwm Icons package.
# - Style options from fvwmmenudesktop not carried over,
#   WM hints are sufficient to set mini-icon and icon.
# - Removed prototypes.  None were needed.
# - All filehandle names made local.
# - New option for su_gui, do path check for alternatives.

# - Change menu prefix from xdg-menu to FvwmMenu
#
# FIXME:
# - No testing for wm_icons has been done (is that fvwm-themes?).
# - Docs need to be updated.
# - fvwm-icons: okay, maybe the cat isn't a good default for Exec...
# - Running alacarte, I can see there are Application menus, and System menus.
#   Figure out the options to get both.
# - There are good programs like alacarte that are marked GNOME-ONLY.
#   Figure out how to get them in menus.
# - I think this should default to mini-icons enabled, but which
#   icon package should be assumed?
# - Need a way to select the /usr/share/icons icon sets with themes like
#   default/hicolor and size like 16x16 and 24x24.
# - Regenerate menus should prompt for a new command line.
# - sidepics, etc not tested.
# - iconname::: syntax needs investigation, I think some of it is obsolete,
#   needs a verifier, and some defaults.
# - Far too many lexical globals not bound to subroutines (c.f.
#    modularisation.)
# - I don't see the point to the path arguments.  I think they should
#   be obsoleted.
# - If I pass --fvwm-icons I don't need to set --enable-mini-icons.
#   Therefore I think enable mini-icons is pointless.
# - The "check" functions are useless.  Since this module doesn't get the
#   built in path, or the users path, it can't do the check.
# - Looks like there is no such thing as fvwm_toptitle, must have been a gtk thing?

# ---------------------------------------------------------------------------
#  fvwm-menu-desktop
#  See the man page fvwm-menu-desktop.1 for instructions.
#
#  Created on 22/07/1999 by Olivier Chapuis
#
#  Updated on 15/08/1999 by Mikhael Goikhman
#
#  Updated on 24/12/2010 by Dan Espen for xdg menus (see copyright below)
# ---------------------------------------------------------------------------
# COPYING
#
# The script is distributed by the same terms as fvwm itself.
# See GNU General Public License for details.
#
#----------------------------------------------------------------------------

# 25/12/2010 re-written, based heavily on
# xdg-menu for archlinux. based on suse xdg-menu written by <nadvornik@suse.cz>
# Sergej Pupykin <pupykin.s@gmail.com>
#
# >> Copyright (c) 2003 SuSE Linux AG, Nuernberg, Germany. All rights reserved.
# >>
# >> Author: nadvornik@suse.cz

# FILES:
# This script uses the following files if present:
#   /usr/share with /applications/
#   /opt/gnome/share with /applications/
#   kde-config --path xdgdata-apps with /applications/
#   /opt/kde3/share with /desktop-directories/
#   /opt/gnome/share with /desktop-directories/
#   /etc/opt/kde3/share/applnk
#   /opt/kde3/share/applnk
#   kde-config --path apps
#   /etc/xdg

use 5.008;
use strict;
use warnings;
use Getopt::Long;
use Encode;
use I18N::Langinfo qw(langinfo CODESET);
use POSIX qw(locale_h);
use Digest::MD5 qw(md5_hex);

# TA:  2011-04-16:  This module is non-standard, so bail if we don't find
# it.
eval "use XML::Parser; 1" or die "fvwm-menu-desktop:  XML::Parser not installed.\n";

my $xdg_data_dirs = $ENV{XDG_DATA_DIRS} || '';
my @PATH_DIRS = split(':',$ENV{PATH}); # for checking if applications exist

my $version = '2.6.5';
my $menu_prefix='FvwmMenu';
my $DefaultAppDirs;
my $DefaultDirectoryDirs;
my @KDELegacyDirs;
my $desktop_name = 'fvwm2';
my $language = '';
my $charset = 'iso-8859-1';
my $root_cmd;
my $kde_cmd;
my $die_on_error = 0;
my $verbose = 0;

my @language_keys;

#my @accessed_files;
my $TERM_CMD = "xterm -e";


my %Desktop_entries;
my %Directory_entries;

my $root_menu;
my $help;

# Default for the mini-icons is mini/ (relatively to the ImagePath)
my $MINI_ICONS_DIR = 'mini/';
# Then the default for icon is ImagePath (consistent with kde, fvwm2gnome
# and almost consistent with wm-icons)
my $ICONS_DIR = '';
# For png icons
my $PNG_ICONS = '';
my $TRAN_MINI_ICONS = 'mini/';
my $TRAN_ICONS = '';
my $MINI_ICONS = 0;             # mini-icon disabled  (enable =1)
my $TRAN       = 0;             # mini-icon translation disabled (enable =1)
my $wm_icons   = 0;             # use wm-icons compatible menu icon names
my $fvwm_icons   = 0;           # use fvwm-icons compatible menu icon names
# check icons
my $check_icons = "";
my $check_mini_icons = "";
my @check_icons_path = ();
my @check_mini_icons_path = ();
my %DI;
my %dmicon;
# Menu Style option
my $MENU_STYLE = "";
my @menus_for_style = ();
# default for style	', @ful$TRANlt fMenuSSaveror_style = ();use fvmini-icons isbase to or run i
my @chec_ = $is_app && $gapp &&5hec_ = $is_ap`Dmes
# chet is MenuB&&5hec with pp=XPM     meny# chet& $gappt$/l              b     ),pt$ %DI;
? $comma : $commb     ),ptact =~ s/%f/&es
my $MENU_STivier Chaons le optvwmMenu';
myenuSSaveror_si @menus_f1 COPYIonfig command at all, use some @mennd || "vi";  # thcom        n %d - file/as isbase touage = 0se to=*4t %d -PHhe scr || LRdowAttributes c wA
If thantedag%d - file/616 'll, usIRS = sl, commatLLSjtp~ndag%d - the ImagePa  n %d - filf %d - ibdir
     eresting usa filf _CMD , commaage = %d - ib_HDnee   eresting usa l, usId - ib_HDnee  CMD , coe =~ /ad %d - ib_HD usId - ib_H. usId - i"$menu_nameDnee  CMD , coe =~ /ai %d - sbo
	if ($title_g usaopt/gnome/sharr = $inee  ktop-directories/
#   /etc/opt/kde3/share/applnk
#   /opt/kaewindow.
ectories/
# $menu_nameDnee  CMD , coe =~ /ai %d - sbo
	if ($title_g usaopt/- ib_HDopt/- ib_HDopoe =~  usaopt/- ib_HDls:aop# $opoe =~  usao usaoaiyaking igo=~# $menu_n:aoppno leave onlaking igo=~#		&expanderesting usa/etc_lf %d -1 2 if_HDopent->_vp_icati
    $sing2 if_HDcati($title_gnuFunctoon $func_name\b_HDopoe =~  ues nothCAL;;
	ib_HDl_# dfu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                