#!/bin/sh

# convjars 2.31
# converting MozillaTranslator .jar files to working locale packs

# working for MozillaTranslator 5.03
# (c) Robert Kaiser <kairo@kairo.at>
# This script can be used and modified free by anyone, as long as the copyright line stays intact.
# There is no warranty in any way that this script does not harm your system.
# Note: Even version 2.0 of this script was lost by the script itself deleting various important data in my home directory,
# I can't guarantee this might not happen also to you (though I tried to avoid such situations)! Use is at your own risk!

# define variables
jarwork=/mnt/mozilla/jar/work
jarmt=/mnt/mozilla/jar/mt
adddir=/mnt/mozilla/jar/add-files
xpihome=/mnt/mozilla/xpi/nightly
xpichrome=$xpihome/chrome
instchrome=/opt/mozilla-build/chrome
origfilePath=/opt/mozilla-build/chrome/
newxpi=/mnt/mozilla/de-AT.xpi
locVer="1.8a6"
langOnly="de"
regName="AT"
XPIVer=0.`date +%Y%m%d%H`

langName="$langOnly-$regName"
mainPackExtract="$langName $langOnly-inspector $langOnly-chatzilla $langOnly-venkman $langOnly-calendar $langOnly-sroaming"
jarfiles="$langName $regName $langOnly-unix $langOnly-win $langOnly-mac"
instlines=""

for curjar in $jarfiles
do
  echo $curjar: processing...
  mypacks=""
  cd $jarwork/$curjar
  if [ $? != 0 ] ; then
    cd $jarwork
    if [ $? != 0 ] ; then
      exit 1
    fi
    mkdir $curjar
    cd $curjar
  else
    echo $curjar: removing old files in `pwd`...
    rm -R *
  fi
  if [ $curjar = $langName ] ; then
    for curfile in $mainPackExtract
    do
      echo $curjar: unpacking jar file: $curfile...
      unzip -q -o $jarmt/$curfile.jar
    done

  else
    echo $curjar: unpacking jar file...
    unzip -q -o $jarmt/$curjar.jar
  fi
  if [ $? != 0 ] ; then
    exit 1
  fi
  if [ $curjar != $regName ] ; then
    locName=$langName
  else
    locName=$regName
    # echo $curjar: renaming directory $langName to $locName...
    # mv locale/$langName locale/$locName
  fi

  components=`find -name "contents.rdf" | sed s:\./locale/$locName/:: | sed s:/contents.rdf::`
  for comp in $components
  do
    mypacks="$mypacks $comp"

    # HACK: inspector has set localeVersion="0.9.4" !
#    if [ $comp = "inspector" ] ; then
#      echo $curjar/$comp: HACK - setting localeVersion to 0.9.4 for inspector...
#      sed "s/$locVer/0.9.4/" locale/$locName/$comp/contents.rdf > locale/$locName/$comp/contents.new.rdf
#      mv locale/$locName/$comp/contents.new.rdf locale/$locName/$comp/contents.rdf
#    fi

    # HACK: chatzilla has no localeVersion!
#    if [ $comp = "chatzilla" ] ; then
#      echo $curjar/$comp: HACK - killing localeVersion for ChatZilla...
#      sed "s|<RDF:Description about=\"urn:mozilla:locale:de-AT:chatzilla\" chrome:localeVersion=\"$locVer\"/>||" locale/$locName/$comp/contents.rdf > locale/$locName/$comp/contents.new.rdf
#      mv locale/$locName/$comp/contents.new.rdf locale/$locName/$comp/contents.rdf
#    fi

    # HACK: MT 5.03 writes empty localeVersion!
    echo $curjar/$comp: HACK - inserting localeVersion...
    sed "s|chrome:localeVersion=\"\"|chrome:localeVersion=\"$locVer\"|" locale/$locName/$comp/contents.rdf > locale/$locName/$comp/contents.new.rdf
    mv locale/$locName/$comp/contents.new.rdf locale/$locName/$comp/contents.rdf

    if [ $comp != "global" -a $comp != "global-region" ] ; then
      echo $curjar/$comp: removing global author, localeVersion and previewURL...
      cat locale/$locName/$comp/contents.rdf | tr "\n" "~" | sed -e s/chrome:author.*chrome:name/chrome:name/ -e "s/~ *chrome:localeVersion.*<chrome:packages>/>~    <chrome:packages>/" | tr "~" "\n" > locale/$locName/$comp/contents.new.rdf
      mv locale/$locName/$comp/contents.new.rdf locale/$locName/$comp/contents.rdf
    fi
  done

  if [ -e $adddir/$curjar ] ; then
    echo $curjar: adding additional files...
    cp -a $adddir/$curjar/* .
  fi
  echo $curjar: rebuilding jar file...
  rm $xpichrome/$curjar.jar
  zip -rX0Dq $xpichrome/$curjar.jar *
  if [ $instchrome != "/dev/null" ] ; then
    echo $curjar: copying jar file to $instchrome...
    cp $xpichrome/$curjar.jar $instchrome
  fi

  echo $curjar: creating install.js registering entries...
  if [ $curjar = $langName ] ; then
    ftype="cf"; lName="localeName";
  else
    if [ $curjar = $regName ] ; then
      ftype="rf"; lName="regionName";
    else
      ftype="pf"; lName="localeName";
    fi
  fi
  if [ $curjar = "$langOnly-unix" ] ; then
    instlines="$instlines~~    if (platformNode == 'unix') {"
  fi
  if [ $curjar = "$langOnly-win" ] ; then
    instlines="$instlines~~    if (platformNode == 'win') {"
  fi
  if [ $curjar = "$langOnly-mac" ] ; then
    instlines="$instlines~~    if (platformNode == 'mac') {"
  fi
  for pack in $mypacks
  do
    instlines="$instlines~      registerChrome(chromeType, $ftype, $lName + \"$pack/\");"
  done
  if [ $curjar = "$langOnly-unix" ] ; then
    instlines="$instlines~    }"
  fi
  if [ $curjar = "$langOnly-win" ] ; then
    instlines="$instlines~    }"
  fi
  if [ $curjar = "$langOnly-mac" ] ; then
    instlines="$instlines~    }"
  fi

  echo
done

echo XPI: building new locale pack...
cd $xpihome
if [ $? != 0 ] ; then
  exit 1
fi
#instlines=`echo $instlines | tr "~" "\n"`
sed "s/x.y.z/$XPIVer/" install.jst > install.jstmp
sed "s|//rC|$instlines|" install.jstmp | tr "~" "\n" > install.js
rm install.jstmp
rm $newxpi
zip -rX9Dq $newxpi * -x install.jst

echo done. finished. go back to work.
echo

echo General: clean up MozillaTranslator temporary files...
cd /tmp
rm MT_*.tmp
echo cleaned 'em up. everything is shiny now. don't worry, be happy now.
echo
