#!/bin/sh
#v2.14 Puppy now has XDG menus.
#this script builds the menus from template files.
#Any templates can be placed into /etc/xdg/templates, and the file must be
#named to show its final destination. For example, the template for JWM:
# _root_.jwmrc
#...the '_' will be converted to a '/', so the generated JWM config file is:
# /root/.jwmrc
# 5jan2008: fbpanel,lxpanel support developed by plinej.
#100404 BK added 'variconlinks' for my fbpanel pkg.
#100427 when called via /etc/rc.d/rc.update, HOME is '/' (needed by some of the menu generating apps).
#120207 translation of some SSS strings. refer /usr/share/sss/menu_strings/
#120208 fix in case this script gets called with LANG=C
#120209 this script now called from /usr/sbin/quicksetup, whenever locale is changed.
#120216 sss translation file now uses simple sed expressions.
#120524 bug fix.
#121124 ensure that all [ ] are escaped. see also /usr/sbin/fixdesk, lang2initrd.
#121125 revert 121124.
#121126 restore 121124, plus escape '.' chars in regex.
#130411 a bit faster (from 15 sec to 5 sec) L18L
#180705 pekwm pkg (see woofQ pekwm template) has 'xdg2pekwm'.

#100427
[ ! "$HOME" ] && HOME='/root'
[ "$HOME" = "/" ] && HOME='/root'
export HOME

if [ "$LANG" = "C" ];then #120208
 LANG="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
 export LANG
fi
LANG1="`echo -n $LANG | cut -f 1 -d '_'`"  #120207 ex: de

TEMPLATES="`ls -1 /etc/xdg/templates | tr '\n' ' '`"

for ONETPL in $TEMPLATES #ex: _root_.jwmrc
do
 [ "$ONETPL" = "README.txt" ] && continue
 ONEDEST="`echo -n "$ONETPL" | sed -e 's/_/\//g'`"
 ONESRC="/etc/xdg/templates/$ONETPL"
 echo "Generating $ONEDEST..."
 
 [ -f $ONEDEST ] && mv -f $ONEDEST ${ONEDEST}-previous

# cat $ONESRC |
 while read ONELINE #130411
 do
# EXECMENU="`echo -n "$ONELINE" | grep -o 'PUPPYMENU.*' | cut -f 2-5 -d ' '`"
  case $ONELINE in #130411
   *PUPPYMENU*)   
      EXECMENU="${ONELINE##*U }" #remove bits before the "U "    
      #echo EXECMENU=$EXECMENU
      $EXECMENU >> ${ONEDEST}     ;;
   *) echo "$ONELINE" >> $ONEDEST ;;
  esac
 done < $ONESRC #130411
#done  
 
 #120207 translate some strings... 120216...
 if [ "$LANG1" != "en" ];then
  if [ -f /usr/share/sss/menu_strings/menu_strings.${LANG1} ];then
   sPTN="/^\[${ONETPL}\]/,/^$/p" #this is a multi-line block find expression.
   CODEBLOCK="`sed -n "$sPTN" /usr/share/sss/menu_strings/menu_strings.${LANG1} | sed -e '/^#/d' -e '/%%/d' -e '/^$/d' -e '/^\[/d'`" #extracts just the relevant block of lines.
   if [ "$CODEBLOCK" ];then
    echo "$CODEBLOCK" > /tmp/fixmenus-translationblock
    #121124 ensure that all [ ] are escaped... 121125 revert... 121126 restore, plus escape '.' chars...
    sed -i -e 's%\[%\\[%g' -e 's$\]$\\]$g' -e 's%\\\\\[%\\[%g' -e 's%\\\\\]%\\]%g' /tmp/fixmenus-translationblock
    sed -i -e 's%\.%\\.%g' -e 's%\\\\\.%\\.%g' /tmp/fixmenus-translationblock #note: 2nd ptn gets rid of prior escape char, so there remains just one.
    sed -i -f /tmp/fixmenus-translationblock ${ONEDEST}
   fi
  fi
 fi

done

#w001 support for fbpanel, lxpanel, openbox, fluxbox, pekwm...
[ `which variconlinks` ] && variconlinks #100404 for my fbpanel pkg.
[ `which tempicons` ] && tempicons
[ `which fbpanel_menu_refresh` ] && fbpanel_menu_refresh
[ `which lxpanel_menu_refresh` ] && lxpanel_menu_refresh
[ `which jwm2fluxbox` ] && jwm2fluxbox  ##current fluxbox_menu_refresh doesn't support menu icons while this does
[ `which obmenu-refresh` ] && obmenu-refresh
[ `which jwm2pekwm` ] && jwm2pekwm
[ `which xdg2pekwm` ] && xdg2pekwm #180705

###END###
