#!/bin/sh
# 151225: support for jwm menuheights is inconsistent in recent versions
# of fixmenus, however, 01micko's implementation supports this parameter.
# JWMDesk calls this cutdown version of 01micko's fixmenus, for jwm only.

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

[ -f $HOME/.jwm/menuheights ] && . $HOME/.jwm/menuheights

# remove csh shell files #precaution
CSH=$(find /etc/profile.d -name *.csh)
[ "$CSH" ] && rm /etc/profile.d/*.csh

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
 do
  EXECMENU="`echo -n "$ONELINE" | sed -e 's/^[ \t]*//' | grep -o '^PUPPYMENU.*' | cut -f 2-5 -d ' '`"
  if echo "$ONELINE" | grep -q "MENHEIGHT" ;then #131213 designed to be backward compatible
   [ "$MENHEIGHT" ] && echo $ONELINE | sed "s%MENHEIGHT%$MENHEIGHT%" >> $ONEDEST \
   || echo $ONELINE | sed "s%MENHEIGHT%16%" >> $ONEDEST
  elif [ "$EXECMENU" = "" ];then
   echo "$ONELINE" >> $ONEDEST
  else
   ${EXECMENU} ${MENHEIGHT} >> ${ONEDEST}
  fi
 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

###END###
