#!/bin/sh
#(c) Copyright Barry Kauler, Feb 2012 bkhome.org
#licence GPL3 (refer /usr/share/doc/legal)
#this script has been created to translate English <-> Non-English certain files that are related to the desktop in some way.
#the files to be translated, and the translations, are defined in /usr/share/sss/desk_strings/
#note, the translations in /usr/share/sss are managed by MoManager (see Utility menu).
#fixdesk has to be called whenever the locale is changed.
#note, the SSS domain 'menu_strings' (/usr/share/sss/menu_strings) is read by /usr/sbin/fixmenus.
#120209 first release.
#120209 this script now called from /usr/sbin/quicksetup, whenever locale is changed.
#120209 convert this script into generic handler of sss-domains, allow symlinks (ex: fixscripts now a symlink to fixdesk).
#120211 major overhaul to ensure integrity of translation of target files back to en.
#120212 bugfix.
#120216 major rewrite, sss translation file now uses simple sed expressions.
#121124 ensure that all [ ] are escaped. see also /usr/sbin/fixmenus, lang2initrd.
#121125 revert 121124.
#121126 restore 121124, plus escape '.' chars in regex.

#Note to developers:
#you can run "fixdesk en" or "fixdesk de" in a terminal to convert the target files to en or de
#(or other language). this is convenient for testing your translations.

SSSDOMAIN="desk_strings"    #this was it prior to 120209.
SSSSCRIPT=$(basename $0)    #but now can have symlinks to fixdesk. ex: fixscripts
case $SSSSCRIPT in
 fixdesk) SSSDOMAIN="desk_strings" ;;
 fixscripts) SSSDOMAIN="script_strings" ;;
 *)          SSSDOMAIN="$SSSSCRIPT" ;;  #for the future.
esac
SSSFILE="${SSSDOMAIN}"      #filenames are same as the domain ex: /usr/share/sss/script_strings/script_strings.de
mkdir -p /tmp/momanager
mkdir -p /var/local/sss
. /etc/rc.d/PUPSTATE

if [ "$LANG" = "C" ];then #in case caller script did this.
 LANG="`grep '^LANG=' /etc/profile | cut -f 2 -d '=' | cut -f 1 -d ' '`"
 export LANG
fi
LANG1="`echo -n $LANG | cut -f 1 -d '_'`"  #ex: de
[ ! -f /usr/share/sss/${SSSDOMAIN}/${SSSDOMAIN}.$LANG1 ] && LANG1='en' #sanity check. if translation sss-file not exist, change files back to english.
#this script may be called from /usr/sbin/momanager, with passed param...
[ $1 ] && LANG1="$1" #want to translate to this language.

#keep a backup of original en target files...
echo "Files being translated:"
PATHPREFIX=""
[ $PUPMODE -gt 3 ] && PATHPREFIX="/initrd/pup_ro2"
for ASECTIONID in `grep '^\[' /usr/share/sss/${SSSFILE}/${SSSFILE} | tr -d '[' | tr -d ']' | tr '\n' ' '` #ex: general _root_Choices_ROX-Filer_PuppyPin
do
 [ "$ASECTIONID" = "general" ] && continue
 xASECTIONID="$ASECTIONID"
 #allow any character to be the 'marker' for / in the section-id...
 MARKERCHAR="`echo -n "$ASECTIONID" | cut -c 1`"
 TARGETFILE="`echo -n "$ASECTIONID" | tr "${MARKERCHAR}" '/'`" #ex: /root/Choices/ROX-Filer/PuppyPin
 echo " ${TARGETFILE}" #useful info when running this script in a terminal.
 TARGETPATH="`dirname "$TARGETFILE"`"
 if [ ! -f  /var/local/sss"${TARGETFILE}" ];then
  mkdir -p /var/local/sss"${TARGETPATH}"
  cp -a -f "$TARGETFILE" /var/local/sss"${TARGETPATH}"/ #should only happen once.
 fi
done

#always restore the files to orig english version...
for ASECTIONID in `grep '^\[' /usr/share/sss/${SSSFILE}/${SSSFILE} | tr -d '[' | tr -d ']' | tr '\n' ' '` #ex: general _root_Choices_ROX-Filer_PuppyPin
do
 [ "$ASECTIONID" = "general" ] && continue
 #allow any character to be the 'marker' for / in the section-id...
 MARKERCHAR="`echo -n "$ASECTIONID" | cut -c 1`"
 TARGETFILE="`echo -n "$ASECTIONID" | tr "${MARKERCHAR}" '/'`" #ex: /root/Choices/ROX-Filer/PuppyPin
 TARGETPATH="`dirname "$TARGETFILE"`"
 if [ -f  /var/local/sss"${TARGETFILE}" ];then #precaution.
  cp -a -f /var/local/sss"${TARGETFILE}" "${TARGETPATH}"/ #restore en file.
 fi
done

[ "$LANG1" = "en" ] && exit #momanager may call with 'en' passed param.

#now translate to $LANG1...
for ASECTIONID in `grep '^\[' /usr/share/sss/${SSSFILE}/${SSSFILE}.${LANG1} | tr -d '[' | tr -d ']' | tr '\n' ' '` #ex: general _root_Choices_ROX-Filer_PuppyPin
do
 sPTN="/^\[${ASECTIONID}\]/,/^$/p" #this is a multi-line block find expression.
 CODEBLOCK="`sed -n "$sPTN" /usr/share/sss/${SSSFILE}/${SSSFILE}.${LANG1} | sed -e '/^#/d' -e '/%%/d' -e '/^$/d' -e '/^\[/d'`" #extracts just the relevant block of lines.
 [ ! "$CODEBLOCK" ] && continue #precaution.
 #allow any character to be the 'marker' for / in the section-id...
 MARKERCHAR="`echo -n "$ASECTIONID" | cut -c 1`"
 TARGETFILE="`echo -n "$ASECTIONID" | tr "${MARKERCHAR}" '/'`" #ex: /root/Choices/ROX-Filer/PuppyPin
 [ ! -f "$TARGETFILE" ] && continue #precaution.
 #hack, really should not process /root/Choices/ROX-Filer/PuppyPin in sss-domain, only suited to static files...
 [ "$TARGETFILE" = "/root/Choices/ROX-Filer/PuppyPin" ] && echo -n "ICONWIPE" >/var/local/pup_event_icon_change_flag #see clean_desk_icons
 echo "$CODEBLOCK" > /tmp/fixdesk-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/fixdesk-translationblock
 sed -i -e 's%\.%\\.%g' -e 's%\\\\\.%\\.%g' /tmp/fixdesk-translationblock #note: 2nd ptn gets rid of prior escape char, so there remains just one.
 sed -i -f /tmp/fixdesk-translationblock "${TARGETFILE}"
done

###END###
