#!/bin/bash
# copyleft L18L, 2010 in the hope to be useful donated to BarryK
# convert package descriptions to users´ language using gettext
#                                              or using Distributer's Translations
# to be called from script with parameter fltrVERSION where needed
# called from filterpkgs.sh findnames.sh and finduserinstalledpkgs.sh
#        [ "${LANG:0:2}" != "en" ] && . `dirname $0`/i18n "/tmp/filterpkgs.results"
#        [ "${LANG:0:2}" != "en" ] && . `dirname $0`/i18n "/tmp/installedpkgs.results"
#
# i18n gettext by L18L
# 21aug10 shinobar: i18n_head
# 31aug10 bugfix distro
# 7sep10 L18L: TEXTDOMAIN
#
inputfile=$1
#export TEXTDOMAINDIR=/usr/share/locale
#export TEXTDOMAIN=package_descr
#. /usr/bin/gettext.sh
#LANG="`cat /etc/profile|grep '^LANG='`"
#eval $LANG
APPDIR=$(dirname $0)
source "$APPDIR/i18n_head"
LANG2=${LANG:0:2}
[ -f /tmp/petget_filterversion ] && fltrVERSION="`cat /tmp/petget_filterversion`"
distro="`echo -n $fltrVERSION  | cut -f 1 -d '-' `"
version="`echo -n $fltrVERSION | cut -f 2 -d '-' `"
branch="`echo -n $fltrVERSION  | cut -f 3 -d '-' `"
#
if [ "$distro" = "debian" ] || [ "$distro" = "ubuntu" ] 	# 31aug10
then # use existing translation from $distro
 #
 function getShortDescription () { # same in installprevie.sh
  local PackageInfo=$(grep -F -x -A 2 "Package: $1" "$transFILE") # 2 lines context
  pkgdescr="`echo \"${PackageInfo}\" | sed -n '1,/^\$searchPATTERN/p' | sed '$!d'  `" # delete all exept last line
  ShortDescription="${pkgdescr:16}" # 'Description-<2-letter-languagecode>:' cut off
 }

 # ${TEXTDOMAINDIR}/${LANG2} contains Translationfile named $fltrVERSION  (downloaded from ftp://ftp.filearena.net/pub/)
 export transFILE="${TEXTDOMAINDIR}/${LANG2}/Package_Descriptions/${fltrVERSION}"

 if [ ! -f "$transFILE" ]
 then # download Translation from $downloadURL 
  downloadURL="ftp://ftp.filearena.net/pub/${distro}/dists/${version}/${branch}/i18n/Translation-${LANG2}"
  #echo $downloadURL
  #exit
  urxvt -title "Puppy $T_titledown" -bg orange -fg black -geometry 80x10 -e wget ${downloadURL}
  sync;
  [ ! -d `dirname ${transFILE}` ] && mkdir `dirname ${transFILE}`
  if [ -f "Translation-${LANG2}" ] 
  then # successfully downloaded
   echo -n "Package: dummy=very-last-entry-needed-for-last-entry" >> "Translation-${LANG2}"
   mv "Translation-${LANG2}" "$transFILE"
  fi
 fi # end download Translation from $downloadURL 

 # Translation in TEXTDOMAINDIR/LANG2
 mv $inputfile  ${inputfile}.ubuntu
 searchPATTERN="Description-${LANG2}: "
 while read line
 do
   pkgname="`echo -n \"$line\" | cut -f 1 -d '|'`"
   getShortDescription "`echo $pkgname | cut -d '_' -f1 `"
   [ "$ShortDescription" = '' ] && ShortDescription="`echo -n \"$line\" | cut -f 2 -d '|'`" # English is better than nothing
   echo -e "${pkgname}|${ShortDescription}" >> $inputfile
 done < ${inputfile}.ubuntu
 rm -f ${inputfile}.ubuntu
 # end Translation in TEXTDOMAINDIR/LANG2
# end use existing translation from $distro

else # translations in .mo file
 export TEXTDOMAIN=package_descr 
 if [ -f "${TEXTDOMAINDIR}/${LANG:0:5}/LC_MESSAGES/${TEXTDOMAIN}.mo" ] || [ -f "${TEXTDOMAINDIR}/${LANG:0:2}/LC_MESSAGES/${TEXTDOMAIN}.mo" ]
 #                               ---^                                                                  ---^   
 then # only if /usr/share/locale/<yourlanguage>/LC_MESSAGES/TEXTDOMAIN.mo file exists
  mv $inputfile  ${inputfile}.BarryK
  while read line
  do
    pckgname="`echo -n "$line" | cut -f 1 -d '|'`"
    pckgdesc="`echo -n "$line" | cut -f 2 -d '|'`"
    [ "$pckgdesc" = "" ] && pckgdesc="`gettext \"no description available\"`"
    pckgdesc_LANG="`gettext \"$pckgdesc\"`"
    echo -e "$pckgname|$pckgdesc_LANG" >> $inputfile
  done < ${inputfile}.BarryK
  rm -f ${inputfile}.BarryK
 fi

fi # end translations in .mo file
# end i18n
