#!/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.
#150303 correct translate back to en, when files not static.

#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.

#150303
##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

#150303
#i commented-out the above code, as it is only suitable if the file to be translated is truly static.
#/root/Choices/ROX-Filer/PuppyPin certainly isn't! -- has background image, desktop icons in it.
#instead, convert a translated file back to 'en' by reverse translation...
#firstly, create the reverse-translation sss file...
for ASSSFILE in `find /usr/share/sss/${SSSDOMAIN} -mindepth 1 -maxdepth 1 -type f -name "${SSSDOMAIN}.*" | tr '\n' ' '`
do
 TRLANG="$(echo -n "$ASSSFILE" | rev | cut -f 1 -d '.' | rev)"
 [ -f /var/local/sss/usr/share/sss/${SSSDOMAIN}/${SSSDOMAIN}.${TRLANG}.reverse ] && continue
 echo "Creating ${SSSDOMAIN} '${TRLANG}' to 'en' reverse translation file..."
 echo -n '' > /tmp/sss-fixdesk-rev1
 cat $ASSSFILE |
 while read ALINE
 do
  CHAR2="$(echo -n "$ALINE" | cut -c 1,2)"
  if [ "$CHAR2" == "s%" ];then
   PARAM_EN="$(echo -n "$ALINE" | cut -f 2 -d '%')"
   PARAM_TR="$(echo -n "$ALINE" | cut -f 3 -d '%')"
   echo "s%${PARAM_TR}%${PARAM_EN}%" >> /tmp/sss-fixdesk-rev1
  else
   echo "$ALINE" >> /tmp/sss-fixdesk-rev1
  fi
 done
 mkdir -p /var/local/sss/usr/share/sss/${SSSDOMAIN}
 cp -a -f /tmp/sss-fixdesk-rev1 /var/local/sss/usr/share/sss/${SSSDOMAIN}/${SSSDOMAIN}.${TRLANG}.reverse
done
#now, restore the 'en' files...
if [ -d /var/local/sss/usr/share/sss/${SSSDOMAIN} ];then
 for ASECTIONID in `grep '^\[' /usr/share/sss/${SSSDOMAIN}/${SSSDOMAIN} | 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"`"
  echo "Restoring ${TARGETFILE} to 'en'..."
  #problem is, taking ex of /root/Choices/ROX-Filer/PuppyPin, if has been translated, need to know what lang, so can translate back to en...
  for ATRREV in `find /var/local/sss/usr/share/sss/${SSSDOMAIN} -mindepth 1 -maxdepth 1 -type f -name "${SSSDOMAIN}.*.reverse" | tr '\n' ' '`
  do
   #brute force, apply them all (likely only to be one anyway)...
   sPTN="/^\[${ASECTIONID}\]/,/^$/p" #this is a multi-line block find expression.
   CODEBLOCK="`sed -n "$sPTN" ${ATRREV} | 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
 done
fi


[ "$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
 [ "$ASECTIONID" = "general" ] && continue #150303
 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.
 echo "Translating ${TARGETFILE} to '${LANG1}'..." #150303
 #hack, really should not process /root/Choices/ROX-Filer/PuppyPin in sss-domain, only suited to static files... 150303 now ok.
 [ "$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###
