#!/bin/sh
#
# generate_menu for Fluxbox
#
# Copyright (c) 2005 Dung N. Lam <dnlam@users.sourceforge.net>
# Copyright (c) 2002-2004 Han Boetes <han@mijncomputer.nl>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

# Portability notes:
# To guarantee this script works on all platforms that support fluxbox
# please keep the following restrictions in mind:
#
# - don't use [ "a" == "a" ]; use [ "a" = "a" ]    (found with help from FreeBSD user relaxed)
# - don't use if ! command;, use command; if [ $? -ne 0 ];
# - don't use [ -e file ] use [ -r file ]
# - don't use $(), use ``
# - don't use ~, use ${HOME}
# - don't use id -u or $UID, use whoami
# - getopts won't work on all platforms, but the config-file can
#   compensate for that.
# - OpenBSD and Solaris grep do not have the -m option
# - various software like grep/sed/perl may be not present or not
#   the version you have. for example grep '\W' only works on gnu-grep.
#   Keep this in mind, use bare basic defaults.
# - Do _NOT_ suggest to use #!/bin/bash. Not everybody uses bash.
#   Non portable features like getopts in this script can be achieved in
#   other ways.


# Functions
display_usage() {
    cat << EOF
Usage: fluxbox-generate_menu [-kgrBh] [-t terminal] [-w url] [-b browser]
         [-m menu-title] [-o /path] [-u /path] [-p /path] [-n /path] [-q /path]
         [-d /path ] [-ds] [-i /path] [-is] [-do]
EOF
}

display_help() {
    display_usage
    cat << EOF

Options:

    -B  Enable backgrounds menu
    -do Enable doas commands
    -r  Don't remove empty menu-entries; for templates

    -d  Other path(s) to recursively search for *.desktop files
    -ds Wider search for *.desktop files (takes more time)
    -i  Other path(s) to search for icons
        e.g., "/usr/share/icons/Adwaita/16x16/*"
    -is Wider search for icons (worth the extra time)
    -in Skip icon search

    -t  Favourite terminal
    -w  Homepage for console-browsers. Default is fluxbox.org
    -b  Favourite browser
    -m  Menu-title; default is "Fluxbox"
    -o  Outputfile; default is ~/.fluxbox/menu
    -u  User sub-menu; default is ~/.fluxbox/usermenu

    -h  Display this help
    -a  Display the authors of this script

  Only for packagers:

    -p  Prefix; default is /usr

Files:
    ~/.fluxbox/usermenu     Your own submenu which will be included in the menu
    ~/.fluxbox/menuconfig   rc file for fluxbox-generate_menu

EOF
}

#'
display_authors() {
    cat << EOF

fluxbox-generate_menu was brought to you by:

    Henrik Kinnunen:    Project leader.
    Han Boetes:         Packaging, debugging and scripts.
    Filippo Pappalardo: Italian locales and -t option.
    $WHOAMI:            Innocent bystander.

EOF
}

testoption() {
    if [ -z "$3" -o -n "`echo $3|grep '^-'`" ]; then
        echo "Error: The option $2 requires an argument." >&2
        exit 1
    fi
    case $1 in
        ex) # executable
            if find_it "$3"; then
                :
            else
                echo "Error: The option $2 needs an executable as argument, and \`$3' is not." >&2
            fi
            ;;
        di) # directory
            if [ -d "$3" ]; then
                :
            else
                echo "Error: The option $2 needs a directory as argument, and \`$3' is not." >&2
            fi
            ;;
        fl) # file
            if [ -r "$3" ]; then
                :
            else
                echo "Error: The option $2 needs a readable file as argument, and \`$3' is not." >&2
            fi
            ;;
        sk) # skip
            :
            ;;
    esac
}

find_it() {
    [ -n "$1" ] && hash $1 2> /dev/null && shift && "$@"
}

find_it_options() {
    [ -n "$1" ] && hash $1 2> /dev/null
}

#echo "replaceWithinString: $1, $2, $3" >&2
#echo ${1//$2/$3} # causes error in BSD even though not used
replaceWithinString(){
    echo $1 | awk "{ gsub(/$2/, \"$3\"); print }"
}

convertIcon(){
    if [ ! -f "$1" ] ; then 
        echo "Icon file not found: $1" >&2
        return 1
    fi

    if [ "$1" = "$2" ]; then
        # $dnlamVERBOSE "Files are in the same location: $1 = $2" >&2
        # not really an error; just nothing to do.
        return 0;
    fi

    local BASENAME
    BASENAME="${1##*/}"

    # make sure it is an icon by checking if it has an extension
    if [ "$BASENAME" = "${BASENAME%%.*}" ]; then
        # $dnlamVERBOSE "File $1 does not have a filename extention." >&2
        return 1;
    fi

    # don't have to convert xpm files
    case "$1" in
        *.xpm)
            echo "$1"
            return 0;
        ;;
    esac

    # may not have to convert png if imlib is enabled
    if [ "$PNG_ICONS" = "yes" ]; then
        case "$1" in
            *.png)
                echo "$1"
                return 0;
            ;;
        esac
    fi

    # convert all others icons and save it as xpm format under directory $2
    entry_icon="$2/${BASENAME%.*}.xpm"
    if [ -f "${entry_icon}" ]; then
        : echo "File exists. To overwrite, type: convert \"$1\" \"$entry_icon\"" >&2
    else
        if hash convert 2> /dev/null; then
            convert "$1" "$entry_icon"
            # echo convert "$1" , "$entry_icon" >> $ICONMAPPING
        else
            echo "Please install ImageMagick's convert utility" >&2
        fi
    fi
    echo "$entry_icon"
}

removePath(){
    execname="$1"
    progname="${execname%% *}"
    # separate program name and its parameters
    if [ "$progname" = "$execname" ]; then
        # no params
        # remove path from only program name
        execname="${progname##*/}"
    else
        params="${execname#* }"
        # remove path from only program name
        execname="${progname##*/} $params"
    fi
    echo $execname
}

doSearchLoop(){
    for ICONPATH in "$@"; do
        ## $dnlamVERBOSE ": $ICONPATH" >> $ICONMAPPING
          [ -d "$ICONPATH" ] || continue
        #echo -n "."
        # # $dnlamVERBOSE ":: $ICONPATH/$temp_icon" >> $ICONMAPPING
        if [ -f "$ICONPATH/$temp_icon" ]; then
            echo "$ICONPATH/$temp_icon"
            return 0;
        else # try different extensions; 
            # remove extension
            iconNOext="${temp_icon%%.*}"
            [ -d "$ICONPATH" ] && for ICONEXT in .xpm .png .gif ; do
                ## echo "::: $ICONPATH/$iconNOext$ICONEXT" >> $ICONMAPPING
                realpath=`find "$ICONPATH" -type f -name "$iconNOext$ICONEXT" | head -n 1`
                if [ -n "$realpath" ]; then
                    echo $realpath
                    return 0;
                fi
            done
        fi
    done
    #echo "done"
    return 1
}

doSearch(){
    # remove '(' from '(fluxbox ...) | ...'
    execname=`replaceWithinString "$1" "\("`
    temp_icon="$2"
    # $dnlamVERBOSE "# Searching for icon $temp_icon for $execname" >> $ICONMAPPING

    # check in $ICONMAPPING before searching directories
    entry_icon=`grep "^\"${execname}\"" $ICONMAPPING | head -n 1 | grep -o '<.*>'`
    if [ -n "$entry_icon" ]; then
        entry_icon=`replaceWithinString "$entry_icon" "<"`
        entry_icon=`replaceWithinString "$entry_icon" ">"`
        echo $entry_icon
        return 0;
    fi
    # echo "$ICONMAPPING for $execname: $entry_icon"

    # the following paths include a user-defined variable, listing paths to search for icons
    # echo -n "for $temp_icon"
    eval doSearchLoop $USER_ICONPATHS \
      "$FB_ICONDIR" \
      "/usr/share/${execname%% *}" \
      ${OTHER_ICONPATHS} \


}

searchForIcon(){
    # remove '&' and everything after it
    entry_exec="${1%%&*}"
    entry_icon="$2"
    # $dnlamVERBOSE echo "searchForIcon \"$entry_exec\" \"$entry_icon\"" >&2

    # get the basename and parameters of entry_exec -- no path
    entry_exec=`removePath "${entry_exec}"`
    [ -z "$entry_exec" ] && { echo "Exec is NULL $1 with icon $2"; return 1; }

    # search for specified icon if it does not exists
    if [ -n "$entry_icon" ] && [ ! "$entry_exec" = "$entry_icon" ] && [ ! -f "$entry_icon" ]; then
        # to search for icon in other paths,
        # get basename
        temp_icon="${entry_icon##*/}"
        # remove parameters
        temp_icon="${temp_icon#* }"
        # clear entry_icon until temp_icon is found
        unset entry_icon

        if [ ! -f "$entry_icon" ]; then
            entry_icon=`doSearch "$entry_exec" "$temp_icon"`
        fi
    fi

    # remove parameters
    execname="${entry_exec%% *}"

    # echo "search for icon named $execname.{xpm,png,gif}"
    if [ ! -f "$entry_icon" ]; then
        entry_icon=`doSearch "$entry_exec" "$execname"`
    fi

    # -----------  done with search ------------
    # $dnlamVERBOSE echo "::: $entry_icon" >&2

    # convert icon file, if needed
    if [ -f "$entry_icon" ] && [ -n "yes$ConvertIfNecessary" ]; then
        entry_icon=`convertIcon "$entry_icon" "$USERFLUXDIR/icons"`
        # $dnlamVERBOSE echo ":::: $entry_icon" >&2
    fi

    # remove path to icon; just get basename
    icon_base="${entry_icon##*/}"
    # remove extension
    icon_base="${icon_base%%.*}"
    # echo "^.${entry_exec}.[[:space:]]*<.*/${icon_base}\....>" 
    if [ -f "$entry_icon" ]; then
    # if icon exists and entry does not already exists, add it
        if ! grep -q -m 1 "^.${entry_exec}.[[:space:]]*<.*/${icon_base}\....>" $ICONMAPPING 2> /dev/null; then
            printf "\"${entry_exec}\" \t <${entry_icon}>\n" >> $ICONMAPPING
        else 
            : echo "#    mapping already exists for ${entry_exec}" >> $ICONMAPPING
        fi
    else
        echo "# No icon file found for $entry_exec" >> $ICONMAPPING
    fi
}

toSingleLine(){ echo "$@"; }
createIconMapping(){
    # $dnlamVERBOSE "# creating `date`" >> $ICONMAPPING
    # $dnlamVERBOSE "# using desktop files in $@" >> $ICONMAPPING
    # $dnlamVERBOSE "# searching for icons in `eval toSingleLine $OTHER_ICONPATHS`" >> $ICONMAPPING
    # need to determine when to use .fluxbox/icons/$execname.xpm over those listed in iconmapping
    # $dnlamVERBOSE echo "createIconMapping: $@"
    for DIR in "$@" ; do
        if [ -d "$DIR" ]; then
            # $dnlamVERBOSE echo "# ------- Looking in $DIR" >&2 
            # >> $ICONMAPPING
            find "$DIR" -type f -name "*.desktop" | while read DESKTOP_FILE; do 
                # echo $DESKTOP_FILE; 
                #entry_name=`grep '^[ ]*Name=' $DESKTOP_FILE | head -n 1`
                #entry_name=${entry_name##*=}
                entry_exec=`grep '^[ ]*Exec=' "$DESKTOP_FILE" | head -n 1`
                entry_exec=${entry_exec##*=}
                entry_exec=`replaceWithinString "$entry_exec" "\""`
                if [ -z "$entry_exec" ]; then
                    entry_exec=${DESKTOP_FILE%%.desktop*}
                fi

                entry_icon=`grep '^[ ]*Icon=' "$DESKTOP_FILE" | head -n 1`
                entry_icon=${entry_icon##*=}

                # $dnlamVERBOSE echo "--- $entry_exec $entry_icon" >&2
                case "$entry_icon" in
                    "" | mime_empty | no_icon )
                        : echo "no icon for $entry_exec"
                    ;;
                    *)
                        searchForIcon "$entry_exec" "$entry_icon"
                    ;;
                esac
            done
        fi
    done
    # $dnlamVERBOSE "# done `date`" >> $ICONMAPPING
}

lookupIcon() {
    if [ ! -f "$ICONMAPPING" ]; then
        echo "!!! Icon map file not found: $ICONMAPPING" >&2
        return 1
    fi

    execname="$1"
    shift
    [ -n "$1" ] && echo "!! Ignoring extra parameters: $*" >&2

    [ -z "$execname" ] && { echo "execname is NULL; cannot lookup"; return 1; }
    execname=`removePath "$execname"`

    #echo "grepping ${execname}"
    iconString=`grep "^\"${execname}\"" $ICONMAPPING | head -n 1 | grep -o '<.*>'`
    # $dnlamVERBOSE "lookupIcon $execname, $iconString" >&2

    if [ -z "$iconString" ] ; then
        iconString=`grep "^\"${execname%% *}" $ICONMAPPING | head -n 1 | grep -o '<.*>'`
    fi

    if [ -z "$iconString" ] && [ -z "$PARSING_DESKTOP" ] ; then
        ## $dnlamVERBOSE "lookupIcon: Searching ...  should only be needed for icons not gotten from *.desktop (manual-created ones): $execname" >&2
        searchForIcon "$execname" "$execname"
        [ -n "$entry_icon" ] && iconString="<$entry_icon>"
    fi

    # [ -n "$iconString" ] && echo "  Found icon for $execname: $iconString" >&2
    echo $iconString
}

append() {
     if [ -z "${INSTALL}" ]; then
        # $dnlamVERBOSE echo "append: $*" >&2
        iconString="`echo $* | grep -o '<.*>'`"
        # echo "iconString=$iconString" >&2
        if [ -z "$iconString" ] && [ -z "$NO_ICON" ]; then
            echo -n "      $* " >> ${MENUFILENAME}
            # get the program name between '{}' from parameters            
            execname="$*"
            execname=${execname#*\{}
            execname=${execname%%\}*}
            # $dnlamVERBOSE echo "execname=$execname" >&2
            # if execname hasn't changed from original $*, then no '{...}' was given
            if [ ! "$execname" = "$*" ]; then
                case "$execname" in
                    $DEFAULT_TERM*)
                        # remove quotes
                        execname=`replaceWithinString "$execname" "\""`
                        # remove "$DEFAULT_TERM -e "
                        # needed in case calling another program (e.g., vi) via "xterm -e"                    
                        execname=${execname##*$DEFAULT_TERM -e }
                    ;;
                esac
                # lookup execname in icon map file
                iconString=`lookupIcon "$execname"`
                #[ -n "$iconString" ] || echo "No icon found for $execname"
            fi
            echo "${iconString}" >> ${MENUFILENAME}
        else
            echo "      $*" >> ${MENUFILENAME}
        fi
    else
        echo "      $*" >> ${MENUFILENAME}
    fi
}

append_menu() {
    echo "$*" >> ${MENUFILENAME}
}

append_submenu() {
    [ "${REMOVE}" ] && echo >> ${MENUFILENAME} # only an empty line in templates
    append_menu "[submenu] ($1)"
}

append_menu_end() {
    append_menu '[end]'
    [ "${REMOVE}" ] && echo >> ${MENUFILENAME} # only an empty line in templates
}

menu_entry() {
    if [ -f "$1" ]; then
        #                   space&tab here
        entry_name=`grep '^[     ]*Name=' "$1" | head -n 1 | cut -d = -f 2`
        entry_exec=`grep '^[     ]*Exec=' "$1" | head -n 1 | cut -d = -f 2`
        if [ -n "$entry_name" -a -n "$entry_exec" ]; then
            append "[exec] ($entry_name) {$entry_exec}"
        fi
    fi
}

menu_entry_dir() {
    for b in  "$*"/*.desktop; do
        menu_entry "${b}"
    done
}

menu_entry_dircheck() {
    if [ -d "$*" ]; then
        menu_entry_dir "$*"
    fi
}


# recursively build a menu from the listed directories
# the dirs are merged
recurse_dir_menu () {
    ls "$@"/ 2>/dev/null | sort | uniq | while read name; do
        for dir in "$@"; do
            if [ -n "$name" -a -d "$dir/$name" ]; then
                # recurse
                append_submenu "${name}"
                # unfortunately, this is messy since we can't easily expand
                # them all. Only allow for 3 atm. Add more if needed
                recurse_dir_menu ${1:+"$1/$name"}  ${2:+"$2/$name"} ${3:+"$3/$name"}
                append_menu_end
                break; # found one, it'll pick up all the rest
            fi
            # ignore it if it is a file, since menu_entry_dir picks those up
        done
    done

    # Make entries for current dir after all submenus
    for dir in "$@"; do
        menu_entry_dircheck "${dir}"
    done
}


normal_find() {
    while [ "$1" ]; do
        find_it $1     append "[exec]   ($1) {$1}"
        shift
    done
}

cli_find() {
    while [ "$1" ]; do
        find_it $1     append "[exec]   ($1) {${DEFAULT_TERM} -e $1}"
        shift
    done
}

doas_find() {
    [ "${DOAS}" = yes ] || return
    while [ "$1" ]; do
        find_it $1     append "[exec]   ($1 (as root)) {${DEFAULT_TERM} -e doas $1}"
        shift
    done
}

clean_up() {
[ -f "$ICONMAPPING" ] && rm -f "$ICONMAPPING"

# Some magic to clean up empty menus
rm -f ${MENUFILENAME}.tmp
touch ${MENUFILENAME}.tmp
counter=10 # prevent looping in odd circumstances
until [ $counter -lt 1 ] || \
    cmp ${MENUFILENAME} ${MENUFILENAME}.tmp >/dev/null 2>&1; do
    [ -s ${MENUFILENAME}.tmp ] && mv ${MENUFILENAME}.tmp ${MENUFILENAME}
    counter=`expr $counter - 1`
    grep -v '^$' ${MENUFILENAME}|sed -e "/^\[submenu].*/{
n
N
/^\[submenu].*\n\[end]/d
}"|sed -e "/^\[submenu].*/{
N
/^\[submenu].*\n\[end]/d
}" > ${MENUFILENAME}.tmp
done
rm -f ${MENUFILENAME}.tmp
}
# End functions


WHOAMI=`whoami`
[ "$WHOAMI" = root ] && PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin

# Check for Imlib2-support
if fluxbox@EXEEXT@ -info 2> /dev/null | grep -q "^IMLIB"; then
    PNG_ICONS="yes"
else
    # better assume to assume "no"
    PNG_ICONS="no"
fi

# menu defaults (if translation forget to set one of them)

MENU_ENCODING=UTF-8 # (its also ascii)

ABOUTITEM='About'
ANALYZERMENU='Analyzers'
BACKGROUNDMENU='Backgrounds'
BACKGROUNDMENUTITLE='Set the Background'
BROWSERMENU='Browsers'
BURNINGMENU='Burning'
CONFIGUREMENU='Configure'
EDITORMENU='Editors'
EDUCATIONMENU='Education'
EXITITEM='Exit'
FBSETTINGSMENU='Fluxbox menu'
FILEUTILSMENU='File utils'
FLUXBOXCOMMAND='Fluxbox Command'
GAMESMENU='Games'
GRAPHICMENU='Graphics'
LOCKSCREEN='Lock screen'
MISCMENU='Misc'
MULTIMEDIAMENU='Multimedia'
MUSICMENU='Audio'
NETMENU='Net'
NEWS='News'
OFFICEMENU='Office'
RANDOMBACKGROUND='Random Background'
REGENERATEMENU='Regen Menu'
RELOADITEM='Reload config'
RESTARTITEM='Restart'
RUNCOMMAND='Run'
SCREENSHOT='Screenshot'
STYLEMENUTITLE='Choose a style...'
SYSTEMSTYLES='System Styles'
SYSTEMTOOLSMENU='System Tools'
TERMINALMENU='Terminals'
TOOLS='Tools'
USERSTYLES='User Styles'
VIDEOMENU='Video'
WINDOWMANAGERS='Window Managers'
WINDOWNAME='Window name'
WORKSPACEMENU='Workspace List'
XUTILSMENU='X-utils'

# Check translation
case ${LC_ALL} in
    ru_RU*) #Russian locales

# Ah my Russian hero. Please help me update the translation
# $ cp fluxbox-generate-menu.in fluxbox-generate-menu.in.orig
# $ $EDITOR fluxbox-generate-menu.in
# $ diff -u fluxbox-generate-menu.in.orig fluxbox-generate-menu.in > fbgm.diff
# email fbgm.diff to han@mijncomputer.nl

        MENU_ENCODING=KOI8-R

        BACKGROUNDMENU=''
        BACKGROUNDMENUTITLE=' '
        BROWSERMENU=''
        CONFIGUREMENU=''
        EDITORMENU=''
        EXITITEM=''
        FBSETTINGSMENU='FB-'
        FILEUTILSMENU=' '
        FLUXBOXCOMMAND=' '
        GAMESMENU=''
        GRAPHICMENU=''
        LOCKSCREEN=' '
        MISCMENU=''
        MUSICMENU=''
        NETMENU=''
        OFFICEMENU=' '
        RANDOMBACKGROUND=' '
        REGENERATEMENU='  '
        RELOADITEM=''
        RESTARTITEM=''
        RUNCOMMAND=''
        SCREENSHOT=' '
        STYLEMENUTITLE=' '
        SYSTEMSTYLES=' '
        TERMINALMENU=''
        TOOLS=''
        USERSTYLES=' '
        WINDOWMANAGERS=' '
        WINDOWNAME=' '
        WORKSPACEMENU=' '
        XUTILSMENU='X-'
        ;;

    cs_CZ.ISO*) # Czech locales (ISO-8859-2 encodings)

        MENU_ENCODING=ISO-8859-2

        ABOUTITEM='O programu...'
        BACKGROUNDMENU='Pozad'
        BACKGROUNDMENUTITLE='Nastaven pozad'
        BROWSERMENU='Prohlee'
        BURNINGMENU='Vypalovn'
        CONFIGUREMENU='Konfigurace'
        EDITORMENU='Editory'
        EXITITEM='Ukonit'
        FBSETTINGSMENU='Fluxbox Menu'
        FILEUTILSMENU='Souborov utility'
        FLUXBOXCOMMAND='Pkaz Fluxboxu'
        GAMESMENU='Hry'
        GRAPHICMENU='Grafika'
        LOCKSCREEN='Zamknout obrazovku'
        MISCMENU='Rzn'
        MULTIMEDIAMENU='Multimdia'
        MUSICMENU='Audio'
        NETMENU='Internet'
        NEWS='News'
        OFFICEMENU='Kancel'
        RANDOMBACKGROUND='Nhodn pozad'
        REGENERATEMENU='Obnoven menu'
        RELOADITEM='Obnoven konfigurace'
        RESTARTITEM='Restart'
        RUNCOMMAND='Spustit program...'
        SCREENSHOT='Screenshot'
        STYLEMENUTITLE='Volba stylu...'
        SYSTEMTOOLSMENU='Systmov utility'
        SYSTEMSTYLES='Systmov styly'
        TERMINALMENU='Terminly'
        TOOLS='Nstroje'
        USERSTYLES='Uivatelsk styly'
        VIDEOMENU='Video'
        WINDOWMANAGERS='Okenn manaery'
        WINDOWNAME='Jmno okna'
        WORKSPACEMENU='Seznam ploch'
        XUTILSMENU='X-utility'
        ;;

    de_DE*) # german locales

        MENU_ENCODING=ISO-8859-15

        BACKGROUNDMENU='Hintergrundbilder'
        BACKGROUNDMENUTITLE='Hintergrundbild setzen'
        BROWSERMENU='Internet-Browser'
        CONFIGUREMENU='Einstellungen'
        EDITORMENU='Editoren'
        EXITITEM='Beenden'
        FBSETTINGSMENU='Fluxbox-Einstellungen'
        FILEUTILSMENU='Datei-Utilities'
        FLUXBOXCOMMAND='Fluxbox Befehl'
        GAMESMENU='Spiele'
        GRAPHICMENU='Grafik'
        LOCKSCREEN='Bildschirmsperre'
        MISCMENU='Sonstiges'
        MUSICMENU='Musik'
        NETMENU='Netzwerk'
        OFFICEMENU='Bueroprogramme'
        RANDOMBACKGROUND='Zufaelliger Hintergrund'
        REGENERATEMENU='Menu-Regeneration'
        RELOADITEM='Konfiguration neu laden'
        RESTARTITEM='Neustarten'
        RUNCOMMAND='Ausfhren'
        SCREENSHOT='Bildschirmfoto'
        STYLEMENUTITLE='Einen Stil auswaehlen...'
        SYSTEMSTYLES='Systemweite Stile'
        TERMINALMENU='Terminals'
        TOOLS='Helfer'
        USERSTYLES='Eigene Stile'
        WINDOWMANAGERS='Window Manager'
        WINDOWNAME='Window Name'
        WORKSPACEMENU='Arbeitsflaechenliste'
        XUTILSMENU='X-Anwendungen'
        ;;
    sv_SE*) #Swedish locales
# Ah my Swedish hero. Please help me update the translation
# $ cp fluxbox-generate-menu.in fluxbox-generate-menu.in.orig
# $ $EDITOR fluxbox-generate-menu.in
# $ diff -u fluxbox-generate-menu.in.orig fluxbox-generate-menu.in > fbgm.diff
# email fbgm.diff to han@mijncomputer.nl

        MENU_ENCODING=ISO-8859-1

        BACKGROUNDMENU='Bakgrunder'
        BACKGROUNDMENUTITLE='Stt bakgrund'
        BROWSERMENU='Webblsare'
        CONFIGUREMENU='Konfiguration'
        EDITORMENU='Editorer'
        EXITITEM='Avsluta'
        FBSETTINGSMENU='FB-instllningar'
        FILEUTILSMENU='Filverktyg'
        FLUXBOXCOMMAND='Fluxbox kommando'
        GAMESMENU='Spel'
        GRAPHICMENU='Grafik'
        LOCKSCREEN='Ls skrm'
        MISCMENU='Blandat'
        MULTIMEDIAMENU='Multimedia'
        MUSICMENU='Musik'
        NETMENU='Internet'
        OFFICEMENU='Office'
        RANDOMBACKGROUND='Slumpmssig bakgrund'
        REGENERATEMENU='Generera meny'
        RELOADITEM='Ladda om konfig'
        RESTARTITEM='Starta om'
        RUNCOMMAND='Kr'
        SCREENSHOT='Skrmdump'
        STYLEMENUTITLE='Vlj en stil'
        SYSTEMSTYLES='Stiler'
        TERMINALMENU='Terminaler'
        TOOLS='Verktyg'
        USERSTYLES='Stiler'
        VIDEOMENU='Video'
        WINDOWMANAGERS='Fnsterhanterare'
        WINDOWNAME='Fnsternamn'
        WORKSPACEMENU='Arbetsytor'
        XUTILSMENU='X-program'
        ;;
    nl_*) #Nederlandse locales

        MENU_ENCODING=ISO-8859-15

        BACKGROUNDMENU='Achtergrond'
        BACKGROUNDMENUTITLE='Kies een achtergrond'
        BROWSERMENU='Browsers'
        CONFIGUREMENU='Instellingen'
        EDITORMENU='Editors'
        EXITITEM='Afsluiten'
        FBSETTINGSMENU='FB-Instellingen'
        FILEUTILSMENU='Verkenners'
        FLUXBOXCOMMAND='Fluxbox Commando'
        GAMESMENU='Spelletjes'
        GRAPHICMENU='Grafisch'
        LOCKSCREEN='Scherm op slot'
        MISCMENU='Onregelmatig'
        MUSICMENU='Muziek'
        NETMENU='Internet'
        OFFICEMENU='Office'
        RANDOMBACKGROUND='Willekeurige Achtergrond'
        REGENERATEMENU='Nieuw Menu'
        RELOADITEM='Vernieuw instellingen'
        RESTARTITEM='Herstart'
        RUNCOMMAND='Voer uit'
        SCREENSHOT='Schermafdruk'
        STYLEMENUTITLE='Kies een stijl'
        SYSTEMSTYLES='Systeem Stijlen'
        TERMINALMENU='Terminals'
        TOOLS='Gereedschap'
        USERSTYLES='Gebruikers Stijlen'
        WINDOWMANAGERS='Venster Managers'
        WINDOWNAME='Venster Naam'
        WORKSPACEMENU='Werkveld menu'
        XUTILSMENU='X-Gereedschap'
        ;;
    fi_FI*) #Finnish locales

        MENU_ENCODING=ISO-8859-1

        ABOUTMENU='Tietoja ohjelmasta'
        ABOUTITEM='Tietoja ohjelmasta'
        BACKGROUNDMENU='Taustakuvat'
        BACKGROUNDMENUTITLE='Mrit taustakuva'
        BROWSERMENU='Selaimet'
        CONFIGUREMENU='Asetukset'
        EDITORMENU='Editorit'
        EXITITEM='Lopeta'
        FBSETTINGSMENU='Fluxboxin asetukset'
        FILEUTILSMENU='Tiedostotykalut'
        FLUXBOXCOMMAND='Fluxbox komentorivi'
        GAMESMENU='Pelit'
        GRAPHICMENU='Grafiikka'
        LOCKSCREEN='Lukitse nytt'
        MISCMENU='Sekalaista'
        MUSICMENU='Musiikki'
        NETMENU='Verkko'
        OFFICEMENU='Toimisto-ohjelmat'
        RANDOMBACKGROUND='Satunnainen taustakuva'
        REGENERATEMENU='Pivit valikko'
        RELOADITEM='Pivit'
        RESTARTITEM='Kynnist uudelleen'
        RUNCOMMAND='Suorita'
        SCREENSHOT='Kuvakaappaus'
        STYLEMENUTITLE='Valitse tyyli'
        SYSTEMSTYLES='Jrjestelmn tyylit'
        TERMINALMENU='Terminaalit'
        TOOLS='Tykalut'
        USERSTYLES='Kyttjn tyylit'
        WINDOWMANAGERS='Ikkunointiohjelmat'
        WINDOWNAME='Ikkunan nimi'
        WORKSPACEMENU='Tyalueet'
        XUTILSMENU='X-Ohjelmat'
        ;;
    ja_JP*) #Japanese locales
# Ah my Japanese hero. Please help me update the translation
# $ cp fluxbox-generate-menu.in fluxbox-generate-menu.in.orig
# $ $EDITOR fluxbox-generate-menu.in
# $ diff -u fluxbox-generate-menu.in.orig fluxbox-generate-menu.in > fbgm.diff
# email fbgm.diff to han@mijncomputer.nl

        MENU_ENCODING=eucJP

        BACKGROUNDMENU='ط'
        BACKGROUNDMENUTITLE='طʤ'
        BROWSERMENU='֥饦'
        CONFIGUREMENU=''
        EDITORMENU='ǥ'
        EXITITEM='λ'
        FBSETTINGSMENU='Fluxbox'
        FILEUTILSMENU='ե'
        FLUXBOXCOMMAND='Fluxboxޥ'
        GAMESMENU=''
        GRAPHICMENU=''
        LOCKSCREEN='꡼å'
        MISCMENU=''
        MUSICMENU=''
        NETMENU='ͥåȥ'
        OFFICEMENU='ե(Office)'
        RANDOMBACKGROUND='ط()'
        REGENERATEMENU='˥塼ƹ'
        RELOADITEM='ɤ߹'
        RESTARTITEM='Ƶư'
        RUNCOMMAND='ޥɤμ¹'
        SCREENSHOT='꡼󥷥å'
        STYLEMENUTITLE='...'
        SYSTEMSTYLES=''
        TERMINALMENU='ߥʥ'
        TOOLS='ġ'
        USERSTYLES=''
        WINDOWMANAGERS='ɥޥ͡'
        WINDOWNAME='ɥ̾'
        WORKSPACEMENU='ڡ'
        XUTILSMENU='X桼ƥƥ'
        ;;
    fr_FR*) # french locales
# Ah my french hero. Please help me update the translation
# $ cp fluxbox-generate-menu.in fluxbox-generate-menu.in.orig
# $ $EDITOR fluxbox-generate-menu.in
# $ diff -u fluxbox-generate-menu.in.orig fluxbox-generate-menu.in > fbgm.diff
# email fbgm.diff to han@mijncomputer.nl

        MENU_ENCODING=ISO-8859-15

        ANALYZERMENU='Analyseurs'
        BACKGROUNDMENU="Fond d'cran"
        BACKGROUNDMENUTITLE="Changer le fond d'cran"
        BROWSERMENU='Navigateurs'
        CONFIGUREMENU='Configurer'
        EDITORMENU='diteurs'
        EXITITEM='Sortir'
        FBSETTINGSMENU='Configurer Fluxbox'
        FILEUTILSMENU='Outils fichiers'
        FLUXBOXCOMMAND='Commande Fluxbox'
        GAMESMENU='Jeux'
        GRAPHICMENU='Graphisme'
        LOCKSCREEN="Verrouiller l'cran"
        MISCMENU='Divers'
        MULTIMEDIAMENU='Multimdia'
        MUSICMENU='Musique'
        NETMENU='Rseau'
        OFFICEMENU='Bureautique'
        RANDOMBACKGROUND="Fond d'cran alatoire"
        REGENERATEMENU='Rgnrer le menu'
        RELOADITEM='Recharger la configuration'
        RESTARTITEM='Redmarrer Fluxbox'
        RUNCOMMAND='Run'
        SCREENSHOT="Capture d'cran"
        STYLEMENUTITLE='Choisir un style...'
        SYSTEMSTYLES='Styles Systme'
        SYSTEMTOOLSMENU='Outils Systme'
        TERMINALMENU='Terminaux'
        TOOLS='Outils'
        USERSTYLES='Styles Utilisateur'
        VIDEOMENU='Vido'
        WINDOWMANAGERS='Gestionnaires de fentres'
        WINDOWNAME='Nom de la fentre'
        WORKSPACEMENU='Liste des bureaux'
        XUTILSMENU='Outils X'
        ;;
    it_IT*) # italian locales

        MENU_ENCODING=ISO-8859-1

        BACKGROUNDMENU='Sfondi'
        BACKGROUNDMENUTITLE='Imposta lo sfondo'
        BROWSERMENU='Browsers'
        CONFIGUREMENU='Configurazione'
        EDITORMENU='Editori'
        EXITITEM='Esci'
        FBSETTINGSMENU='Preferenze'
        FILEUTILSMENU='Utilit'
        FLUXBOXCOMMAND='Comando Fluxbox'
        GAMESMENU='Giochi'
        GRAPHICMENU='Grafica'
        LOCKSCREEN='Blocca lo schermo'
        MISCMENU='Varie'
        MUSICMENU='Musica'
        NETMENU='Internet'
        OFFICEMENU='Office'
        RANDOMBACKGROUND='Sfondo casuale'
        REGENERATEMENU='Rigenera il menu'
        RELOADITEM='Rileggi la configurazione'
        RESTARTITEM='Riavvia'
        RUNCOMMAND='Esegui'
        SCREENSHOT='Schermata'
        STYLEMENUTITLE='Scegli uno stile'
        SYSTEMSTYLES='Stile'
        TERMINALMENU='Terminali'
        TOOLS='Attrezzi'
        USERSTYLES='Stile'
        WINDOWMANAGERS='Gestori finestre'
        WINDOWNAME='Nome della finestra'
        WORKSPACEMENU='Aree di lavoro'
        XUTILSMENU='Utilit X'
        ;;
    ro_RO*) # Romanian locales
# Ah my Romanian hero. Please help me update the translation
# $ cp fluxbox-generate-menu.in fluxbox-generate-menu.in.orig
# $ $EDITOR fluxbox-generate-menu.in
# $ diff -u fluxbox-generate-menu.in.orig fluxbox-generate-menu.in > fbgm.diff
# email fbgm.diff to han@mijncomputer.nl

        MENU_ENCODING=ISO-8859-15

        BACKGROUNDMENU='Fundaluri'
        BACKGROUNDMENUTITLE='Alege fundalul'
        BROWSERMENU='Navigatoare'
        CONFIGUREMENU='Configurare'
        EDITORMENU='Editoare'
        EXITITEM='Iesire'
        FBSETTINGSMENU='Meniul Fluxbox'
        FILEUTILSMENU='Utilitare de fisier'
        FLUXBOXCOMMAND='Comanda Fluxbox'
        GAMESMENU='Jocuri'
        GRAPHICMENU='Grafica'
        LOCKSCREEN='Incuie ecranul'
        MISCMENU='Diverse'
        MULTIMEDIAMENU='Multimedia'
        MUSICMENU='Muzica'
        NETMENU='Retea'
        OFFICEMENU='Office'
        RANDOMBACKGROUND='Fundal aleator'
        REGENERATEMENU='Regenereaza meniul'
        RELOADITEM='Reincarca configuratia'
        RESTARTITEM='Restart'
        RUNCOMMAND='Lanseaza'
        SCREENSHOT='Captura ecran'
        STYLEMENUTITLE='Alege un stil...'
        SYSTEMSTYLES='Stiluri sistem'
        TERMINALMENU='Terminale'
        TOOLS='Unelte'
        USERSTYLES='Stiluri utilizator'
        WINDOWMANAGERS='WindowManagers'
        WINDOWNAME='Nume fereastra'
        WORKSPACEMENU='Lista workspace-uri'
        XUTILSMENU='Utilitare X'
        ;;
    es_ES*) # spanish locales

        MENU_ENCODING=ISO-8859-15

        ABOUTITEM='Acerca'
        BACKGROUNDMENU='Fondos'
        BACKGROUNDMENUTITLE='Seleccionar Fondo'
        BROWSERMENU='Navegadores'
        BURNINGMENU='Herramientas de grabacin'
        CONFIGUREMENU='Configurar'
        EDITORMENU='Editores'
        EDUCATIONMENU='Educacin'
        EXITITEM='Salir'
        FBSETTINGSMENU='Men fluxbox'
        FILEUTILSMENU='Utilidades'
        FLUXBOXCOMMAND='Comandos de Fluxbox'
        GAMESMENU='Juegos'
        GRAPHICMENU='Grficos'
        LOCKSCREEN='Bloquear Pantalla'
        MISCMENU='Varios'
        MULTIMEDIAMENU='Multimedia'
        MUSICMENU='Msica'
        NETMENU='Red'
        NEWS='Noticias'
        OFFICEMENU='Oficina'
        RANDOMBACKGROUND='Fondo Aleatoreo'
        REGENERATEMENU='Regenerar Men'
        RELOADITEM='Reconfigurar'
        RESTARTITEM='Reiniciar'
        RUNCOMMAND='Ejecutar'
        SCREENSHOT='Captura de Pantalla'
        STYLEMENUTITLE='Escoge un Estilo...'
        SYSTEMSTYLES='Estilos del Sistema'
        TERMINALMENU='Terminales'
        TOOLS='Herramienta'
        USERSTYLES='Estilos del Usuario'
        VIDEOMENU='Video'
        WINDOWMANAGERS='Gestores de Ventanas'
        WINDOWNAME='Nombre de Ventana'
        WORKSPACEMENU='Lista de Escritorios'
        XUTILSMENU='Utilidades X'
        ;;
    pl_PL*) # Polish locales
# Ah my Russian hero. Please help me update the translation
# $ cp fluxbox-generate-menu.in fluxbox-generate-menu.in.orig
# $ $EDITOR fluxbox-generate-menu.in
# $ diff -u fluxbox-generate-menu.in.orig fluxbox-generate-menu.in > fbgm.diff
# email fbgm.diff to han@mijncomputer.nl

        MENU_ENCODING=ISO-8859-2

        BACKGROUNDMENU='Tapety'
        BACKGROUNDMENUTITLE='Ustaw tapet'
        BROWSERMENU='Przegldarki'
        CONFIGUREMENU='Konfiguracja'
        EDITORMENU='Edytory'
        EXITITEM='Wyjcie'
        FBSETTINGSMENU='Menu Fluxbox'
        FILEUTILSMENU='Narzdzia do plikw'
        FLUXBOXCOMMAND='Polecenia Fluxbox'
        GAMESMENU='Gry'
        GRAPHICMENU='Grafika'
        LOCKSCREEN='Zablokuj ekran'
        MISCMENU='Rne'
        MULTIMEDIAMENU='Multimedia'
        MUSICMENU='Muzyka'
        NETMENU='Sie'
        OFFICEMENU='Aplikacje biurowe'
        RANDOMBACKGROUND='Losowa tapeta'
        REGENERATEMENU='Wygeneruj menu'
        RELOADITEM='Odwie konfiguracj'
        RESTARTITEM='Restartuj'
        RUNCOMMAND='Uruchom...'
        SCREENSHOT='Zrzut ekranu'
        STYLEMENUTITLE='Wybierz styl...'
        SYSTEMSTYLES='Style systemowe'
        TERMINALMENU='Terminale'
        TOOLS='Narzdzia'
        USERSTYLES='Style uytkownika'
        WINDOWMANAGERS='Menadery okien'
        WINDOWNAME='Nazwy okien'
        WORKSPACEMENU='Lista pulpitw'
        XUTILSMENU='Narzdzia X'
        ;;
    pt_PT*) # Portuguese locales

        MENU_ENCODING=ISO-8859-1

        ABOUTMENU="Sobre"
        BACKGROUNDMENU='Imagens de Fundo'
        BACKGROUNDMENUTITLE='Definir Imagem de Fundo'
        BROWSERMENU='Browsers'
        BURNINGMENU='Ferramentas de Gravao'
        CONFIGUREMENU='Configurao'
        EDITORMENU='Editores'
        EDUCATIONMENU='Educao'
        EXITITEM='Sair'
        FBSETTINGSMENU='Menu Fluxbox'
        FILEUTILSMENU='Utilitrios de Ficheiros'
        FLUXBOXCOMMAND='Comando Fluxbox'
        GAMESMENU='Jogos'
        GRAPHICMENU='Grficos'
        LOCKSCREEN='Trancar Ecr'
        MISCMENU='Misc.'
        MULTIMEDIAMENU='Multimdia'
        MUSICMENU='udio'
        NETMENU='Rede'
        NEWS='Notcias'
        OFFICEMENU='Escritrio'
        RANDOMBACKGROUND='Imagem Aleatria'
        REGENERATEMENU='Regenerar Menu'
        RELOADITEM='Recarregar configurao'
        RESTARTITEM='Reiniciar'
        RUNCOMMAND='Executar'
        SCREENSHOT='Capturar Ecr'
        STYLEMENUTITLE='Escolha um estilo...'
        SYSTEMSTYLES='Estilos do Sistema'
        SYSTEMTOOLSMENU='Ferramentas de Sistema'
        TERMINALMENU='Terminais'
        TOOLS='Ferramentas'
        USERSTYLES='Estilos do Utilizador'
        VIDEOMENU='Vdeo'
        WINDOWMANAGERS='Gestores de Janelas'
        WINDOWNAME='Nome da Janela'
        WORKSPACEMENU='Lista de reas de Trabalho'
        XUTILSMENU='Utilitrios X'
        ;;
    nb_NO*) # Norwegian locales

        MENU_ENCODING=UTF-8

        ABOUTITEM='Om'
        BACKGROUNDMENU='Bakgrunner'
        BACKGROUNDMENUTITLE='Velg bakgrunn'
        BROWSERMENU='Nettlesere'
        CONFIGUREMENU='Oppsett'
        EDITORMENU='Tekstredigeringsprogram'
        EDUCATIONMENU='Lek og lær'
        EXITITEM='Avslutt'
        FBSETTINGSMENU='FluxBox-meny'
        FILEUTILSMENU='Filverktøy'
        FLUXBOXCOMMAND='FluxBox-kommando'
        GAMESMENU='Spill'
        GRAPHICMENU='Grafikk'
        LOCKSCREEN='Lås skjermen'
        MISCMENU='Diverse'
        MULTIMEDIAMENU='Multimedia'
        MUSICMENU='Lyd'
        NETMENU='Nett'
        NEWS='Nyheter'
        OFFICEMENU='Kontor'
        RANDOMBACKGROUND='Tilfeldig bakgrunn'
        REGENERATEMENU='Regen Menu'
        RELOADITEM='Last oppsett på nytt'
        RESTARTITEM='Start på nytt'
        RUNCOMMAND='Kjør'
        SCREENSHOT='Ta bilde'
        STYLEMENUTITLE='Velg en stil . . .'
        SYSTEMSTYLES='System-stiler'
        TERMINALMENU='Terminaler'
        TOOLS='Verktøy'
        USERSTYLES='Bruker-stiler'
        VIDEOMENU='Video'
        WINDOWMANAGERS='Vindusbehandlere'
        WINDOWNAME='Vindunavn'
        WORKSPACEMENU='Liste over arbeidsområder'
        XUTILSMENU='X-verktøy'
        ;;
    *)
        ;;
esac

# Set Defaults
USERFLUXDIR="${HOME}/.fluxbox"
MENUFILENAME="${MENUFILENAME:=${USERFLUXDIR}/menu}"
MENUTITLE="${MENUTITLE:=Fluxbox}"
HOMEPAGE="${HOMEPAGE:=fluxbox.org}"
USERMENU="${USERMENU:=${USERFLUXDIR}/usermenu}"
MENUCONFIG="${MENUCONFIG:=${USERFLUXDIR}/menuconfig}"
DOAS="no"

# Read the menuconfig file if it exists or else create it.
# But not during install time, use envvar for sun
if [ ! "${INSTALL}" = Yes ]; then
    if [ -r ${MENUCONFIG} ]; then
        . ${MENUCONFIG}
    else
        if [ ! "$WHOAMI" = root ]; then # this is only for users.
            if touch ${MENUCONFIG}; then
                cat << EOF > ${MENUCONFIG}
# This file is read by fluxbox-generate_menu.  If you don't like a
# default you can change it here.  Don't forget to remove the # in front
# of the line.

# Your favourite terminal. Put the command in quotes if you want to use
# options. Put a backslash in before odd chars
# MY_TERM='sakura --tint \#123456'
# MY_TERM='xterm -tint \$(random_color)'

# Your favourite browser. You can also specify options.
# MY_BROWSER=iceweasel-uxp

# Name of the outputfile
# MENUFILENAME=${USERFLUXDIR}/menu

# MENUTITLE=\`fluxbox@EXEEXT@ -version|cut -d " " -f-2\`

# standard url for console-browsers
# HOMEPAGE=fluxbox.org

# location with your own menu-entries
# USERMENU=~/.fluxbox/usermenu

# Put the launcher you would like to use here
# LAUNCHER=fbrun@EXEEXT@
# LAUNCHER=fbgm

# Options for fbrun
# FBRUNOPTIONS='-font 10x20 -fg grey -bg black -title run'

# --- PREFIX'es
# These are prefixes; So if fluxbox is installed in /usr/bin/fluxbox
# your prefix is: /usr

# fluxbox-generate already looks in /usr/X11R6, /usr, /usr/local and /opt so
# there should be no need to specify them.
#
# PREFIX=/usr


# Separate the list of background dirs with colons ':'
# BACKGROUND_DIRS="${USERFLUXDIR}/backgrounds/:/usr/share/fluxbox/backgrounds/:/usr/share/wallpapers"


# --- Boolean variables.
# Setting a variable to ``no'' won't help. Comment them out if you don't
# want them. Settings are overruled by the command-line options.

# Include all backgrounds in your backgrounds-directory
# BACKGROUNDMENUITEM=yes

# Enable doas commands
# DOAS=yes

# Don't cleanup the menu
# REMOVE=no

# Don't add icons to the menu
# NO_ICON=yes

EOF
            else
                echo "Warning: I couldn't create ${MENUCONFIG}" >&2
            fi
        fi
    fi
fi

BACKUPOPTIONS=$@
if [ -n "$BACKUPOPTIONS" ]; then
    FBGM_CMD="fluxbox-generate_menu $BACKUPOPTIONS"
else
    FBGM_CMD=fluxbox-generate_menu
fi
# Get options.
while [ $# -gt 0 ]; do
    case "$1" in
        -B) BACKGROUNDMENUITEM=yes; shift;;
        -in) NO_ICON=yes; shift;;
        -is) OTHER_ICONPATHS="
                /usr/share/icons
                /usr/share/icons/mini
                /usr/share/pixmaps
                /usr/local/share/icons
                /usr/local/share/icons/mini
                /usr/local/share/pixmaps
                /usr/share/xclass/icons
                /usr/share/xclass/pixmaps
                /usr/local/share/xclass/icons
                /usr/local/share/xclass/pixmaps
                /usr/X11R6/share/icons/default/16x16
                /usr/X11R6/share/icons/hicolor/16x16
                /usr/local/X11R6/share/icons/default/16x16
                /usr/local/X11R6/share/icons/hicolor/16x16
            "
            shift;;
        -ds) OTHER_DESKTOP_PATHS="
                /usr/share/mimelnk
                /usr/share/applications
                /usr/share/xsessions
                /usr/share/services
            "
            # /usr/share/apps \
            shift;;
        -i) USER_ICONPATHS=${2};
            #needs testing
            for aPath in $2; do
                testoption di $1 $aPath;
            done
            shift 2;;
        -d) USER_DESKTOP_PATHS=${2};
            #needs testing
            for aPath in $2; do
                testoption di $1 $aPath;
            done
            shift 2;;
        -t) MY_TERM=${2}; testoption ex $1 $2; shift 2;;
        -b) MY_BROWSER=${2}; testoption ex $1 $2; shift 2;;
        -o) MENUFILENAME=${2}; shift 2; CHECKINIT=NO ;;
        -p) PREFIX=${2}; testoption di $1 $2; shift 2;;
        -m) MENUTITLE=${2}; testoption sk $1 $2; shift 2;;
        -w) HOMEPAGE=${2}; testoption sk $1 $2; shift 2;;
        -u) USERMENU=${2}; testoption fl $1 $2; shift 2;;
	-do) DOAS=yes; shift;;
        -r) REMOVE=no; shift;;
        -h) display_help ; exit 0 ;;
        -a) display_authors ; exit 0 ;;
        --*) echo "fluxbox-generate_menu doesn't recognize -- gnu-longopts."
            echo 'Use fluxbox-generate_menu -h for a long help message.'
            display_usage
            exit 1 ;;
        -[a-zA-Z][a-zA-Z]*)
            # split concatenated single-letter options apart
            FIRST="$1"; shift
            set -- `echo "$FIRST" | sed 's/^-\(.\)\(.*\)/-\1 -\2/'` "$@"
            ;;
        -*)
            echo 1>&2 "fluxbox-generate_menu: unrecognized option "\`"$1'"
            display_usage
            exit 1
            ;;
        *)
            break
            ;;
    esac
done

# Check defaults

# Can we actually create ${MENUFILENAME}
touch ${MENUFILENAME} 2> /dev/null
if [ $? -ne 0 ]; then
    echo "Fatal error: can't create or write to $MENUFILENAME" >&2
    exit 1
fi

# backup menu
if [ -w "${MENUFILENAME}" ]; then
    if [ -f ${MENUFILENAME}.firstbak ]; then
        cp ${MENUFILENAME} ${MENUFILENAME}.firstbak
    fi
    if [ -s "${MENUFILENAME}" ]; then
       mv ${MENUFILENAME} ${MENUFILENAME}.bak
    fi
fi

# prefix
PREFIX="${PREFIX:=/usr}"
if [  -z "${PREFIX}" -o ! -d "${PREFIX}" ]; then
    hash fluxbox@EXEEXT@
    PREFIX=`hash | grep fluxbox | sed 's,.*\t/,/,' | sed 's,/bin/fluxbox@EXEEXT@$,,'`
fi

# Will remain $PREFIX if all else fails

if [ -z "${INSTALL}" ] && [ -z "${NO_ICON}" ]; then
    # [ -z "$dnlamVERBOSE" ] && dnlamVERBOSE=": echo"   # for debugging
    FB_ICONDIR="$USERFLUXDIR/icons"
    [ -r "$FB_ICONDIR" ] || mkdir "$FB_ICONDIR"
    ICONMAPPING="$USERFLUXDIR/iconmapping"

    checkDirs(){
        #echo checkDirs: $* >&2
        local CHECKED_DIRS=""
        for DIR in "$@"; do
            if [ -d "$DIR" ]; then
                # todo: should check if there are duplicates
                CHECKED_DIRS="$CHECKED_DIRS \"$DIR\""
            fi
        done
        #echo checkDirs - $CHECKED_DIRS >&2
        echo $CHECKED_DIRS
    }

    OTHER_ICONPATHS=`eval checkDirs $OTHER_ICONPATHS`
    OTHER_DESKTOP_PATHS=`eval checkDirs $OTHER_DESKTOP_PATHS`

    # $dnlamVERBOSE "Using USER_DESKTOP_PATHS=\"$USER_DESKTOP_PATHS\" and USER_ICONPATHS=\"$USER_ICONPATHS\""
    # $dnlamVERBOSE "Using OTHER_ICONPATHS=$OTHER_ICONPATHS"
    # $dnlamVERBOSE "Using OTHER_DESKTOP_PATHS=$OTHER_DESKTOP_PATHS"
    # $dnlamVERBOSE "Calling function: createIconMapping"

    # $dnlamVERBOSE "Creating $ICONMAPPING" >&2
    touch "$ICONMAPPING"
    eval createIconMapping $USER_DESKTOP_PATHS $OTHER_DESKTOP_PATHS
    # $dnlamVERBOSE "Done createIconMapping."
fi

# directory for the backgrounds
if [ -z "$BACKGROUND_DIRS" ]; then
    BACKGROUND_DIRS="${USERFLUXDIR}/backgrounds/:${PREFIX}/share/fluxbox/backgrounds/"
fi

# find the default terminal
if find_it_options $MY_TERM; then
    DEFAULT_TERM=$MY_TERM
else
    [ -n "$MY_TERM" ] && echo "Warning: you chose an invalid term." >&2
    #The precise order is up for debate.
    for term in rxvt-unicode wterm sakura st xterm; do
        if find_it_options $term; then
            DEFAULT_TERM=$term
            break
        fi
    done
fi
# a unix system without any terms. that's odd
if [ -z "$DEFAULT_TERM" ]; then
    cat << EOF >&2

Warning: I can't find any terminal-emulators in your PATH.  Please fix
your PATH or specify your favourite terminal-emulator with the -t option

EOF
    DEFAULT_TERM=xterm
fi

DEFAULT_TERMNAME=`echo $DEFAULT_TERM|awk '{print $1}'`
DEFAULT_TERMNAME=`basename $DEFAULT_TERMNAME`


# find the default browser
if find_it_options $MY_BROWSER; then
    DEFAULT_BROWSER=$MY_BROWSER
else
    [ -n "$MY_BROWSER" ] && echo "Warning: you chose an invalid browser." >&2
    #The precise order is up for debate.
    for browser in dillo elinks iceweasel-uxp links lynx midori netsurf otter-browser; do
        if find_it_options $browser; then
            DEFAULT_BROWSER=$browser
            break
        fi
    done
fi
DEFAULT_BROWSERNAME=`echo $DEFAULT_BROWSER|awk '{print $1}'`
if [ "x$DEFAULT_BROWSERNAME" != "x" ]; then
	DEFAULT_BROWSERNAME=`basename $DEFAULT_BROWSERNAME`
else
	DEFAULT_BROWSERNAME="iceweasel-uxp"
fi

if [ -z "$LAUNCHER" ]; then
    LAUNCHER=fbrun@EXEEXT@
fi
if [ -n "$FBRUNOPTIONS" ]; then
    # with this, LAUNCHER should be renamed LAUNCHER_NAME, but then there's
    # backwards-compatibility...
    LAUNCHER_CMD="$LAUNCHER $FBRUNOPTIONS"
else
    LAUNCHER_CMD=$LAUNCHER
fi

# if gxmessage exists, use it; else use xmessage
if find_it gxmessage; then
    XMESSAGE=gxmessage
else
    XMESSAGE=xmessage
fi

# Start of menu
cat << EOF > ${MENUFILENAME}
# Generated by fluxbox-generate_menu
#
# If you read this it means you want to edit this file manually, so here
# are some useful tips:
#
# - You can add your own menu-entries to ~/.fluxbox/usermenu
#
# - If you miss apps please let me know and I will add them for the next
#   release.
#
# - The -r option prevents removing of empty menu entries and lines which
#   makes things much more readable.
#
# - To prevent any other app from overwriting your menu
#   you can change the menu name in ~/.fluxbox/init to:
#     session.menuFile: ~/.fluxbox/my-menu

EOF

echo "[begin] (${MENUTITLE})" >> ${MENUFILENAME}

if [ -n "$MENU_ENCODING" ]; then
    append_menu "[encoding] {$MENU_ENCODING}"
fi

append "[exec] (${DEFAULT_TERMNAME}) {${DEFAULT_TERM}}"

case "$DEFAULT_BROWSERNAME" in
    elinks|links|lynx)  append "[exec] (${DEFAULT_BROWSERNAME}) {${DEFAULT_TERM} -e ${DEFAULT_BROWSER} ${HOMEPAGE}}" ;;
    iceweasel-uxp|dillo) append "[exec] (${DEFAULT_BROWSERNAME}) {${DEFAULT_BROWSER}}" ;;
    *) append "[exec] ($DEFAULT_BROWSERNAME) {$DEFAULT_BROWSER}" ;;
esac

find_it "${LAUNCHER}" append "[exec]   (${RUNCOMMAND}) {$LAUNCHER_CMD}"

append_submenu "${TERMINALMENU}"
    normal_find rxvt-unicode sakura st xterm
append_menu_end


append_submenu "${NETMENU}"
    append_submenu "${BROWSERMENU}"
        normal_find dillo iceweasel-uxp midori netsurf vncviewer
        find_it links       append "[exec]   (links-graphic) {links -driver x ${HOMEPAGE}}"
        find_it links       append "[exec]   (links) {${DEFAULT_TERM} -e links ${HOMEPAGE}}"
        find_it elinks      append "[exec]   (elinks) {${DEFAULT_TERM} -e elinks ${HOMEPAGE}}"
        find_it lynx        append "[exec]   (lynx) {${DEFAULT_TERM} -e lynx ${HOMEPAGE}}"
    append_menu_end

    append_submenu IM
        normal_find pidgin dino
        cli_find profanity
    append_menu_end

    append_submenu Mail
        normal_find icedove-uxp claws-mail
        cli_find mutt
    append_menu_end

    append_submenu News
        normal_find liferea
    append_menu_end

    append_submenu IRC
        normal_find hexchat
        cli_find weechat
    append_menu_end

    append_submenu P2P
        normal_find amule
    append_menu_end

    append_submenu FTP
        normal_find filezilla
        cli_find lftp
    append_menu_end

    append_submenu SMB
      normal_find pyneighborhood smbclient
    append_menu_end

    append_submenu "${ANALYZERMENU}"
	  normal_find nmap wireshark ettercap
	  doas_find nmap wireshark ettercap
    append_menu_end

    normal_find wpa_supplicant_gui

append_menu_end

append_submenu "${EDITORMENU}"
    normal_find emacs featherpad gvim geany lumina-textedit
    cli_find nano nvi vim vi
    find_it     emacs  append "[exec]   (emacs-nw) {${DEFAULT_TERM} -e emacs -nw}"
append_menu_end

append_submenu "${EDUCATIONMENU}"
    normal_find celestia oregano pymol stellarium xcircuit xdrawchem
    cli_find acl coq gnuplot grace grass maxima
append_menu_end

append_submenu "${FILEUTILSMENU}"
    normal_find lumina-fm rox spacefm xfe
    cli_find mc ranger
append_menu_end

append_submenu "${MULTIMEDIAMENU}"
       append_submenu "${GRAPHICMENU}"
               normal_find gimp gphoto inkscape krita gqview tuxpaint xpaint xlockmore xzgv
               find_it blender append "[exec] (blender) {blender -w}"
       append_menu_end

       append_submenu "${MUSICMENU}"
               normal_find audacious audacity deadbeef
               cli_find cmus
       append_menu_end


       append_submenu "${VIDEOMENU}"
           normal_find goggles vlc xcdroast xawtv
           find_it dvdrip append "[exec] (dvdrip) {nohup dvdrip}"
           cli_find mpv smplayer
       append_menu_end

       append_submenu "${XUTILSMENU}"
           normal_find xenocara-editres xenocara-viewres xenocara-xbiff xenocara-xclock xenocara-xfontsel xenocara-xload xenocara-xmag
           find_it xenocara-xrdb append "[exec] (Reload .Xdefaults) {xrdb -load \$HOME/.Xdefaults}"
       append_menu_end
append_menu_end


append_submenu "${OFFICEMENU}"
    normal_find xenocara-xcalc xenocara-xclock
    find_it gsimplecal append "[exec] (Calendar)   {gsimplecal}"

    # older <=1.1.3 apparently have stuff like swriter, not sowriter
    for ext in libreo oo; do
        find_it ${ext}ffice && (
            find_it ${ext}ffice        append "[exec] (LO Office)      {${ext}ffice}"
            find_it ${ext}base         append "[exec] (LO Base)          {${ext}base}"
            find_it ${ext}calc         append "[exec] (LO Calc)          {${ext}calc}"
            find_it ${ext}writer       append "[exec] (LO Writer)        {${ext}writer}"
            find_it ${ext}impress      append "[exec] (LO Impress)       {${ext}impress}"
            find_it ${ext}draw         append "[exec] (LO Draw)          {${ext}draw}"
            find_it ${ext}math         append "[exec] (LO Math)          {${ext}math}"
        )
    done

    normal_find abiword libwpd poppler gvim
    normal_find dia xfig
    normal_find gnumeric
append_menu_end

append_submenu "${GAMESMENU}"
    normal_find 7kaa allegro allegro-legacy angband antimicrox blobby2 \
                bombermaaan boswars box2d bullet dolphin-emu dosbox \
                dustrac endless-sky extremetuxracer flare-game freedm \
                freedoom freedroidrpg freeorion gigamesh gweled hex-a-hop \
                libgme libmanette lincity-ng megaglest megaglest-data \
                mgba-qt mgba-sdl minetest minetest-server neverball \
                openblok opensurge pingus pioneer pioneers plib scratch \
                scummvm scummvm-tools signus supertux supertuxkart surgescript \
                teeworlds trigger-rally warzone2100 warzone2100-legacy \
                wesnoth widelands wyrmsun xmahjongg xmoto
append_menu_end

append_submenu "${SYSTEMTOOLSMENU}"
  append_submenu "${BURNINGMENU}"
    normal_find cdw dvd+rw-tools simpleburn
  append_menu_end

  normal_find vsftpd
  cli_find top htop bpytop
append_menu_end



# We'll only use this once
ETCAPPLNK=/etc/X11/applnk
PARSING_DESKTOP="true"

unset PARSING_DESKTOP

#User menu
if [ -r "${USERMENU}" ]; then
    cat ${USERMENU} >> ${MENUFILENAME}
fi

append_submenu "${FBSETTINGSMENU}"
    append "[config] (${CONFIGUREMENU})"

    append_menu "[submenu] (${SYSTEMSTYLES}) {${STYLEMENUTITLE}}"
        append "[stylesdir] (${PREFIX}/share/fluxbox/styles)"
    append_menu_end

    append_menu "[submenu] (${USERSTYLES}) {${STYLEMENUTITLE}}"
        append "[stylesdir] (~/.fluxbox/styles)"
    append_menu_end

    # Backgroundmenu
    addbackground() {
                picturename=`basename "$1"`
                append "[exec] (${picturename%.???}) {fbsetbg -a \"$1\" }"
    }

    if [ "$BACKGROUNDMENUITEM" = yes ]; then
        IFS=: # set delimetor for find
        NUMBER_OF_BACKGROUNDS=`find $BACKGROUND_DIRS -follow -type f 2> /dev/null|wc -l`
        if [ "$NUMBER_OF_BACKGROUNDS" -gt 0 ]; then
            append_menu "[submenu] (${BACKGROUNDMENU}) {${BACKGROUNDMENUTITLE}}"
            append "[exec] (${RANDOMBACKGROUND}) {fbsetbg -r ${USERFLUXDIR}/backgrounds}"
            if [ "$NUMBER_OF_BACKGROUNDS" -gt 30 ]; then
                menucounter=1 ; counter=1
                append_menu "[submenu] (${BACKGROUNDMENU} $menucounter) {${BACKGROUNDMENUTITLE}}"
                find $BACKGROUND_DIRS -follow -type f|sort|while read i; do
                    counter=`expr $counter + 1`
                    if [ $counter -eq 30 ]; then
                        counter=1
                        menucounter=`expr $menucounter + 1`
                        append_menu_end
                        append_menu "[submenu] (${BACKGROUNDMENU} $menucounter) {${BACKGROUNDMENUTITLE}}"
                    fi
                    addbackground "$i"
                done
                append_menu_end
            else
                find $BACKGROUND_DIRS -follow -type f|sort|while read i; do
                addbackground "$i"
                done
            fi
            append_menu_end
        else
            echo "Warning: You wanted a background-menu but I couldn't find any backgrounds in:
    $BACKGROUND_DIRS" >&2
        fi
    fi

    append "[workspaces] (${WORKSPACEMENU})"

    append_submenu "${TOOLS}"
        normal_find fluxconf fluxkeys fluxmenu
        find_it fbpanel append "[exec] (Fluxbox panel) {fbpanel}"
        find_it $XMESSAGE append \
            "[exec] (${WINDOWNAME}) {xprop WM_CLASS|cut -d \\\" -f 2|$XMESSAGE -file - -center}"
        find_it import append "[exec] (${SCREENSHOT} - JPG) {import screenshot.jpg && display -resize 50% screenshot.jpg}"
        find_it import append "[exec] (${SCREENSHOT} - PNG) {import screenshot.png && display -resize 50% screenshot.png}"
        find_it ${LAUNCHER} append "[exec] (${RUNCOMMAND}) {$LAUNCHER_CMD}"
        find_it switch append "[exec] (gtk-theme-switch) {switch}"
        find_it switch2 append "[exec] (gtk2-theme-switch) {switch2}"
        find_it fluxbox-generate_menu append "[exec] (${REGENERATEMENU}) {$FBGM_CMD}"
    append_menu_end

    append_submenu "${WINDOWMANAGERS}"
    #hard to properly maintain since there are so many exceptions to the rule.
    for wm in blackbox enlightenment16 fvwm3 icewm openbox sawfish xenocara-twm; do
        find_it start${wm} append "[restart] (${wm}) {start${wm}}" ||\
            find_it ${wm} append "[restart] (${wm}) {${wm}}"
    done
        find_it startwindowmaker append "[restart] (windowmaker) {startwindowmaker}" ||\
            find_it wmaker append "[restart] (windowmaker) {wmaker}"
    append_menu_end
    find_it xlockmore append "[exec] (${LOCKSCREEN}) {xlock}" ||\
        find_it xscreensaver-command append "[exec] (${LOCKSCREEN}) {xscreensaver-command -lock}"
    append "[commanddialog] (${FLUXBOXCOMMAND})"
    append "[reconfig] (${RELOADITEM})"
    append "[restart] (${RESTARTITEM})"
    append "[exec] (${ABOUTITEM}) {(fluxbox@EXEEXT@ -v; fluxbox@EXEEXT@ -info | sed 1d) | $XMESSAGE -file - -center}"
    append "[separator]"
    append "[exit] (${EXITITEM})"

    append_menu_end

if [ -n "$MENU_ENCODING" ]; then
    append_menu "[endencoding]"
fi

append_menu_end

# this function removes empty menu items. It can not yet  remove  nested
# empty submenus :\

if [ ! "${REMOVE}" ]; then
    clean_up
fi

# escapes any parentheses in menu label
# e.g.,  "[exec] (iceweasel-uxp (web))" becomes  "[exec] (iceweasel-uxp (web\))"

sed 's/(\(.*\)(\(.*\)))/(\1 (\2\\))/' $MENUFILENAME > $MENUFILENAME.tmp
mv -f $MENUFILENAME.tmp $MENUFILENAME

if [ -z "$INSTALL" ]; then
    if [ -z "$CHECKINIT" ]; then
        INITMENUFILENAME=`awk '/menuFile/ {print $2}' $USERFLUXDIR/init`
        INITMENUFILENAME=`replaceWithinString "$INITMENUFILENAME" "~" "$HOME"`
        if [ ! "$INITMENUFILENAME" = "$MENUFILENAME" ]; then 
            echo "Note: In $USERFLUXDIR/init, your \"session.menuFile\" does not point to $MENUFILENAME but to $INITMENUFILENAME" >&2
        fi
    fi
    echo "Menu successfully generated: $MENUFILENAME"
    #echo "  Make sure \"session.menuFile: $MENUFILENAME\" is in $HOME/.fluxbox/init."
    echo 'Use fluxbox-generate_menu -h to read about all the latest features.'
fi
