Check-in [22fb98e847]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview

SHA1 Hash:22fb98e8476e65a55f52c05938bfec54644dc8c6
Date: 2012-02-11 00:05:33
User: BarryK
Comment:/usr/sbin/fixdesk overhauled to ensure integrity of translation

Tags And Properties
Changes

Changes to woof-code/rootfs-skeleton/usr/sbin/fixdesk

@@ -7,97 +7,135 @@
 #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.
 
-#WARNING
-#developers/translators beware, if the target files are translated, say to 'de' (and you are running Puppy with locale de_DE),
-#and you then edit desk_strings.de TRANSLATION variables, this script will be upset, as won't be able to fully convert the
-#target file back to 'en'.
-#Solution: if you edit a TRANSLATION variable in desk_strings.de, also make the change in the target file ...now, that is sensible!
-# ...i will put code into MoManager to do this automatically, so prefer use MoManager rather than edit desk_strings.de directly.
-# ...you can also do it manually "fixdesk en" will translate all target files back to english, then edit desk_strings.de,
-#    then run "fixdesk de" to translate target files back to de using the updated desk_strings.de.
+#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.
-mkdir -p /tmp/momanager
+#this script may be called from /usr/sbin/momanager, with passed param...
+[ $1 ] && LANG1="$1" #want to translate to this language.
 
 FNDSSSS="`find /usr/share/sss/${SSSDOMAIN} -mindepth 1 -maxdepth 1 -type f -name "${SSSDOMAIN}.*" 2>/dev/null`" #ex: desk_strings.de\ndesk_strings.ru\n
-
-#this script may be called from /usr/sbin/momanager, with passed param...
-[ $1 ] && LANG1="$1" #want to translate to this language.
 
 #unlike fixmenus which processes raw english files and generates result in required language,
 #fixdesk operates upon files in-place. problem when rerun fixdesk, need to determine what language
 #the file was previously translated to...
+
+#code-block below this one will determine what lang the target files are in.
+#but, during development of the translations, this may fail. so, keep a backup of
+#orig en file...
+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
+ 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
+
+#now attempt to determine language target files are in...
+TRANS=''
+echo -n "" > /tmp/momanager/fixdesk-trans
 if [ ! "$FNDSSSS" ];then
  TRANS='en' #there are no translation sss-files, so target files must still be in english.
- echo -n "$TRANS" > /tmp/momanager/fixdesk-trans
 else
  for ADESK in $FNDSSSS
  do
   ENSUCCESSCNT=0; TRSUCCESSCNT=0; TRTRYCNT=0; FLAGDONE='no'
   echo -n "$ENSUCCESSCNT" > /tmp/momanager/fixdesk-ensuccesscnt
   echo -n "$TRSUCCESSCNT" > /tmp/momanager/fixdesk-trsuccesscnt
   echo -n "$TRTRYCNT" > /tmp/momanager/fixdesk-trtrycnt
-  TRANS='en'
-  echo -n "$TRANS" > /tmp/momanager/fixdesk-trans
   ALANG1="`echo -n "$ADESK" | rev | cut -f 1 -d '.' | rev`"
-  ONETPL='_root_Choices_ROX-Filer_PuppyPin' #section that is known to exist in sss-file.
-  sPTN="/^\[${ONETPL}\]/,/^$/p" #this is a multi-line block find expression.
+  #xASECTIONID is a section that is known to exist in sss-file...
+  #allow any character to be the 'marker' for / in the section-id...
+  MARKERCHAR="`echo -n "$xASECTIONID" | cut -c 1`"
+  TARGETFILE="`echo -n "$xASECTIONID" | tr "${MARKERCHAR}" '/'`" #ex: /root/Choices/ROX-Filer/PuppyPin
+  [ ! -f "$TARGETFILE" ] && continue #precaution.
+  sPTN="/^\[${xASECTIONID}\]/,/^$/p" #this is a multi-line block find expression.
   CODEBLOCK="`sed -n "$sPTN" /usr/share/sss/${SSSDOMAIN}/${SSSDOMAIN}.${ALANG1} | sed -e '/^#/d' -e '/^$/d' -e '/^\[/d'`" #extracts just the relevant block of lines.
+  [ ! "$CODEBLOCK" ] && continue #precaution.
   echo "$CODEBLOCK" |
   while read ALINE
   do
    eval "$ALINE" #ex: CODESTRING='label="INSERTMARKER"' ; ENGLISH='trash'     ; TRANSLATION='müll'
-   if [ "$ENGLISH" ];then
-    iPTN="s%INSERTMARKER%${ENGLISH}%"
-    TESTPTN="`echo -n "$CODESTRING" | sed -e "$iPTN"`"
-    [ "`grep "$TESTPTN" /root/Choices/ROX-Filer/PuppyPin`" != "" ] && ENSUCCESSCNT=$(($ENSUCCESSCNT + 1))
-   fi
    if [ "$TRANSLATION" ];then
-    if [ "$TRANSLATION" != "$ENGLISH" ];then #ignore cases where translation same as english.
-     TRTRYCNT=$(($TRTRYCNT + 1))
-     iPTN="s%INSERTMARKER%${TRANSLATION}%"
-     TESTPTN="`echo -n "$CODESTRING" | sed -e "$iPTN"`"
-     [ "`grep "$TESTPTN" /root/Choices/ROX-Filer/PuppyPin`" != "" ] && TRSUCCESSCNT=$(($TRSUCCESSCNT + 1))
+    if [ "$ENGLISH" ];then
+     if [ "$TRANSLATION" != "$ENGLISH" ];then #ignore cases where translation same as english.
+      TRTRYCNT=$(($TRTRYCNT + 1))
+      iPTN="s%INSERTMARKER%${TRANSLATION}%"
+      TESTPTN="`echo -n "$CODESTRING" | sed -e "$iPTN"`"
+      [ "`grep "$TESTPTN" "$TARGETFILE"`" != "" ] && TRSUCCESSCNT=$(($TRSUCCESSCNT + 1))
+      iPTN="s%INSERTMARKER%${ENGLISH}%"
+      TESTPTN="`echo -n "$CODESTRING" | sed -e "$iPTN"`"
+      [ "`grep "$TESTPTN" "$TARGETFILE"`" != "" ] && ENSUCCESSCNT=$(($ENSUCCESSCNT + 1))
+     fi
     fi
    fi
    echo -n "$ENSUCCESSCNT" > /tmp/momanager/fixdesk-ensuccesscnt
    echo -n "$TRSUCCESSCNT" > /tmp/momanager/fixdesk-trsuccesscnt
    echo -n "$TRTRYCNT" > /tmp/momanager/fixdesk-trtrycnt
   done
   ENSUCCESSCNT=`cat /tmp/momanager/fixdesk-ensuccesscnt`
   TRSUCCESSCNT=`cat /tmp/momanager/fixdesk-trsuccesscnt`
   TRTRYCNT=`cat /tmp/momanager/fixdesk-trtrycnt`
-  if [ $TRSUCCESSCNT -gt $ENSUCCESSCNT ];then
-   [ $TRSUCCESSCNT -eq $TRTRYCNT ] && FLAGDONE='yes' #100% hit.
-   DIFFCNT=$(($TRTRYCNT - $TRSUCCESSCNT))
-   [ $DIFFCNT -eq 1 ] && FLAGDONE='yes' #allow one failure.
-   if [ "$FLAGDONE" = "yes" ];then
-    TRANS="$ALANG1"
-    echo -n "$TRANS" > /tmp/momanager/fixdesk-trans
+  if [ $TRTRYCNT -gt 0 ];then
+   if [ $TRSUCCESSCNT -eq $TRTRYCNT ];then #100% hit.
+    echo -n "$ALANG1" > /tmp/momanager/fixdesk-trans
+    break
+   fi
+   if [ $ENSUCCESSCNT -eq $TRTRYCNT ];then #100% hit.
+    echo -n "en" > /tmp/momanager/fixdesk-trans
     break
    fi
   fi
  done
+ TRANS="`cat /tmp/momanager/fixdesk-trans`"
 fi
-TRANS="`cat /tmp/momanager/fixdesk-trans`"
+if [ ! "$TRANS" ];then
+ TRANS='en'
+ #restore backup en target files...
+ 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
+fi
 
 TRANS2EN='no'; EN2LANG1='no'
 if [ "$LANG1" != "en" ];then
  if [ "$TRANS" != "en" ];then
   #have to translate $TRANS back to en first, then to $LANG1 (even if $TRANS==$LANG1 as may need refresh)
@@ -119,17 +157,22 @@
 
 if [ "$TRANS2EN" = "yes" ];then
  for ASECTIONID in `grep '^\[' /usr/share/sss/${SSSFILE}/${SSSFILE}.${TRANS} | tr -d '[' | tr -d ']' | tr '\n' ' '` #ex: general _root_Choices_ROX-Filer_PuppyPin
  do
   [ "$ASECTIONID" = "general" ] && continue
+  ENSUCCESSCNT=0; TRTRYCNT=0
+  echo -n "$ENSUCCESSCNT" > /tmp/momanager/fixdesk-ensuccesscnt
+  echo -n "$TRTRYCNT" > /tmp/momanager/fixdesk-trtrycnt
   sPTN="/^\[${ASECTIONID}\]/,/^$/p" #this is a multi-line block find expression.
-  ENCODEBLOCK="`sed -n "$sPTN" /usr/share/sss/${SSSFILE}/${SSSFILE}.${TRANS} | sed -e '/^#/d' -e '/^$/d' -e '/^\[/d'`" #extracts just the relevant block of lines.
+  CODEBLOCK="`sed -n "$sPTN" /usr/share/sss/${SSSFILE}/${SSSFILE}.${TRANS} | sed -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
-  if [ -f "$TARGETFILE" ];then #precaution.
-   if [ "$CODEBLOCK" ];then #precaution.
+  [ ! -f "$TARGETFILE" ] && continue #precaution.
+  #if [ -f "$TARGETFILE" ];then #precaution.
+  # if [ "$CODEBLOCK" ];then #precaution.
     echo "$CODEBLOCK" |
     while read ALINE
     do
      eval "$ALINE" #ex: CODESTRING='<Program label="INSERTMARKER"'  ; ENGLISH='Power-off computer' ; TRANSLATION='Computer ausschalten'
      enPTN="s%INSERTMARKER%${ENGLISH}%"
@@ -137,27 +180,46 @@
      if [ "$TRANSLATION" ];then #otherwise, the target file will still have english.
       trPTN="s%INSERTMARKER%${TRANSLATION}%"
       trCODESTRING="`echo -n "$CODESTRING" | sed -e "$trPTN"`"
       fixPTN="s%${trCODESTRING}%${enCODESTRING}%"
       sed -i -e "$fixPTN" "${TARGETFILE}" #translates one line in destination file.
+      #was it successful?...
+      TRTRYCNT=$(($TRTRYCNT + 1))
+      iPTN="s%INSERTMARKER%${ENGLISH}%"
+      TESTPTN="`echo -n "$CODESTRING" | sed -e "$iPTN"`"
+      [ "`grep "$TESTPTN" "$TARGETFILE"`" != "" ] && ENSUCCESSCNT=$(($ENSUCCESSCNT + 1)) #test target file.
      fi
+     echo -n "$ENSUCCESSCNT" > /tmp/momanager/fixdesk-ensuccesscnt
+     echo -n "$TRTRYCNT" > /tmp/momanager/fixdesk-trtrycnt
     done
-   fi
+  # fi
+  #fi
+  #was it 100% success translating back to en?...
+  ENSUCCESSCNT=`cat /tmp/momanager/fixdesk-ensuccesscnt`
+  TRTRYCNT=`cat /tmp/momanager/fixdesk-trtrycnt`
+  if [ $ENSUCCESSCNT -eq $TRTRYCNT ];then
+   #100% success, so refresh the backup file...
+   cp -a -f "$TARGETFILE" /var/local/sss"${TARGETFILE}"
+  else
+   #get the en file from backup...
+   cp -a -f /var/local/sss"${TARGETFILE}" "$TARGETFILE"
   fi
  done
 fi
 
 if [ "$EN2LANG1" = "yes" ];then
  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.
-  ENCODEBLOCK="`sed -n "$sPTN" /usr/share/sss/${SSSFILE}/${SSSFILE}.${LANG1} | sed -e '/^#/d' -e '/^$/d' -e '/^\[/d'`" #extracts just the relevant block of lines.
+  CODEBLOCK="`sed -n "$sPTN" /usr/share/sss/${SSSFILE}/${SSSFILE}.${LANG1} | sed -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
-  if [ -f "$TARGETFILE" ];then #precaution.
-   if [ "$CODEBLOCK" ];then #precaution.
+  [ ! -f "$TARGETFILE" ] && continue #precaution.
+  #if [ -f "$TARGETFILE" ];then #precaution.
+  # if [ "$CODEBLOCK" ];then #precaution.
     echo "$CODEBLOCK" |
     while read ALINE
     do
      eval "$ALINE" #ex: CODESTRING='<Program label="INSERTMARKER"'  ; ENGLISH='Power-off computer' ; TRANSLATION='Computer ausschalten'
      enPTN="s%INSERTMARKER%${ENGLISH}%"
@@ -167,11 +229,11 @@
       trCODESTRING="`echo -n "$CODESTRING" | sed -e "$trPTN"`"
       fixPTN="s%${enCODESTRING}%${trCODESTRING}%"
       sed -i -e "$fixPTN" ${TARGETFILE} #translates one line in destination file.
      fi
     done
-   fi
-  fi
+  # fi
+  #fi
  done
 fi
 
 ###END###