#!/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.
#190103 new symlink /usr/sbin/fixinitrd. note, called from quicksetup -> lang2initrd  190104
#190105 called from lang2initrd, $2 is full new LANG
#190105 new /usr/share/sss/boot_strings, symlink /usr/sbin/fixboot to fixdesk
#190107 $3 optional path to mounted boot-partition
#190226 BOOT_SPECS in initrd no longer has INIT_LANG variable.

#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" ;;
 fixinitrd)  SSSDOMAIN="initrd_strings" ;; #190103
 fixboot)    SSSDOMAIN="boot_strings" ;; #190105 syslinux/refind menus.
 *)          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 ' '`"
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.
#190105 called from lang2initrd, $2 is full new LANG...
[ $2 ] && LANG="$2"
export LANG
#190107 optional path to mounted boot-partiton, has initrd etc...
MNTD_BP=""
if [ $3 ];then
 MNTD_BP="$3"
 BOOTMNTflg=0
fi

#190103
goodbye_func() {
 if [ "$SSSDOMAIN" == "initrd_strings" ];then #190103
  #close up initrd...
  cd /tmp/momanager/initrd-expanded
  if [ ! "$MNTD_BP" ];then #190107
   rm -f /mnt/${BOOT_DEV}/${BOOT_DIR}initrd
   find . | cpio -o -H newc > /mnt/${BOOT_DEV}/${BOOT_DIR}initrd
  else
   rm -f ${MNTD_BP}/initrd
   find . | cpio -o -H newc > ${MNTD_BP}/initrd
  fi
  sync
  cd ..
  rm -rf initrd-expanded
  rm -f initrd
  #unmount boot partition...
  if [ $BOOTMNTflg -eq 1 ];then
   sync
   busybox umount /mnt/${BOOT_DEV}
  fi
 fi
 if [ "$SSSDOMAIN" == "boot_strings" ];then #190105
  #unmount boot partition...
  if [ $BOOTMNTflg -eq 1 ];then
   sync
   busybox umount /mnt/${BOOT_DEV}
  fi
 fi
}

#190103
TRPREFIX=""
if [ "$SSSDOMAIN" == "initrd_strings" ];then
 #find initrd...
 BOOTMNTflg=0
 [ -f /tmp/momanager/initrd ] && rm -f /tmp/momanager/initrd
 if [ ! "$MNTD_BP" ];then #190107
  grep "/mnt/${BOOT_DEV} " /proc/mounts >/dev/null
  if [ $? -ne 0 ];then
   mkdir -p /mnt/${BOOT_DEV}
   busybox mount -t ${BOOT_FS} /dev/${BOOT_DEV} /mnt/${BOOT_DEV}
   if [ $? -ne 0 ];then
    exit 2
   fi
   BOOTMNTflg=1
  fi
  if [ ! -f /mnt/${BOOT_DEV}/${BOOT_DIR}initrd ];then
   busybox umount /mnt/${BOOT_DEV}
   exit 2
  fi
  cp -f /mnt/${BOOT_DEV}/${BOOT_DIR}initrd /tmp/momanager/
 else
  cp -f ${MNTD_BP}/initrd /tmp/momanager/
 fi
 #open up initrd...
 cd /tmp/momanager
 [ -d initrd-expanded ] && rm -rf initrd-expanded
 mkdir initrd-expanded
 cd initrd-expanded
 cpio -i -d -m -F ../initrd #expand
 TRPREFIX="/tmp/momanager/initrd-expanded"
 #190104 if init script uses getttext, then do not want to apply sss method...
 if grep '^export TEXTDOMAIN' /tmp/momanager/initrd-expanded/init >/dev/null;then
  mkdir -p /tmp/momanager/initrd-expanded/usr/lib/locale
  rm -rf /tmp/momanager/initrd-expanded/usr/lib/locale/* 2>/dev/null
  if [ "$LANG1" != "en" ];then
   cp -a /usr/lib/locale/${LANG1}_* /tmp/momanager/initrd-expanded/usr/lib/locale/ 2>/dev/null
   #these are very big, and do not seem to be needed...
   rm -f /tmp/momanager/initrd-expanded/usr/lib/locale/*/LC_COLLATE 2>/dev/null
   rm -f /tmp/momanager/initrd-expanded/usr/lib/locale/*/LC_CTYPE 2>/dev/null
  fi
  mkdir -p /tmp/momanager/initrd-expanded/usr/share/locale/${LANG1}/LC_MESSAGES
  [ -f /usr/share/locale/${LANG1}/LC_MESSAGES/easyinitrd.mo ] && cp -f /usr/share/locale/${LANG1}/LC_MESSAGES/easyinitrd.mo /tmp/momanager/initrd-expanded/usr/share/locale/${LANG1}/LC_MESSAGES/
  goodbye_func
  exit 0
 fi
fi

#190105 translate syslinux/refind menus...
if [ "$SSSDOMAIN" == "boot_strings" ];then
 BOOTMNTflg=0
 if [ ! "$MNTD_BP" ];then #190107
  grep "/mnt/${BOOT_DEV} " /proc/mounts >/dev/null
  if [ $? -ne 0 ];then
   mkdir -p /mnt/${BOOT_DEV}
   busybox mount -t ${BOOT_FS} /dev/${BOOT_DEV} /mnt/${BOOT_DEV}
   if [ $? -ne 0 ];then
    exit 2
   fi
   BOOTMNTflg=1
  fi
  TRPREFIX="/mnt/${BOOT_DEV}"
 else
  TRPREFIX="$MNTD_BP"
 fi
fi

#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
  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 "${TRPREFIX}${TARGETFILE}" ] && continue #precaution. 190103
   ##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 "${TRPREFIX}${TARGETFILE}" #190103
  done
 done
fi


if [ "$LANG1" = "en" ];then #momanager may call with 'en' passed param.
 goodbye_func #190103
 exit 0
fi

#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 "${TRPREFIX}${TARGETFILE}" ] && continue #precaution. 190103
 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 "${TRPREFIX}${TARGETFILE}" #190103
done

goodbye_func #190103
exit 0
###END###
