#!/bin/bash

# localize fluxbox menu 
# 

# fluxbox init file
INIT="$HOME/.fluxbox/init"
# default menu for all none-translated locale
DFLT="$HOME/.fluxbox/menu-mx"
# directory of localized menus 
L10N="/usr/share/mxflux/menu-translations"

# trim region code from LANG
LC="${LANG%%_*}"
# in case LC is empty do nothing
[ x"$LC" = "x" ] && exit 1

# localized menu
LCMN="$L10N/menu-mx_$LC"

# default menu is in English
MENU="$DFLT"

# give localize menu to user 
if [ "$LC" != "en" ] && [ -r "$LCMN" ]; then
   MENU="$HOME/.fluxbox/menu-mx_$LC"
   if [ ! -e "$MENU" ]; then
        cp "$LCMN" "$MENU"
   fi
fi

# adjust init to use the menu 
# trim home 
MENU="${MENU#$HOME/}"

# check if menu is already in use, adjust in case
if ! grep -sq -E "^session.menuFile:[[:space:]]*(~|$HOME)/$MENU[[:space:]]*$" "$INIT"; then
   # replace menu in init
   sed -i -r "/^(session.menuFile:).*/s::\1    ~/$MENU:"  "$INIT"
fi

# fix to localize "All Apps" string


export TEXTDOMAIN=mx-desktop-menu-l10n
export TEXTDOMAINDIR="/usr/share/locale"
ALLAPPS=$(gettext "All Applications")
[ "$ALLAPPS" = "All Applications"  ] && ALLAPPS="All Apps"
if [ -w ~/$MENU ]; then 
    sed -i "/submenu/s/All Apps/$ALLAPPS/"  ~/$MENU
fi
if [ -x /usr/bin/mxfb-menu-generator ]; then 
        /usr/bin/mxfb-menu-generator
fi
exit 0
