#!/bin/sh

# make-mozilla-win32-release 0.1
# creating a .zip Windows Mozilla build from an en-US .zip package + a locale XPI file

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

# define variables
buildfile=/mnt/mozilla/mozilla-win32-1.7.5.zip
xpifile=/mnt/mozilla/mozilla-1.7.5.de-AT.langpack.xpi
xpitemp=/mnt/mozilla/xpi/buildtemp
buildtemp=/mnt/win32sys/Programme/Netscape/Mozilla/buildtemp
newbuild=/mnt/mozilla/mozilla-1.7.5.de-AT.win32.zip
usesplash=/mnt/mozilla/projects/splash/mozilla.bmp
adddir=/mnt/mozilla/jar/add-files/bin/
xpichrome=chrome
xpidefaults=defaults
xpisplugins=searchplugins
xpimyspell=myspell
langOnly="de"
regName="AT"

langName="$langOnly-$regName"
addcomponents="inspector chatzilla venkman calendar"
#addcomponents="inspector chatzilla calendar"
curplatform="win"
#mozdir="bin"
mozdir="mozilla"

echo accessing build file...
if [ ! -e $buildtemp ] ; then
  mkdir $buildtemp
fi
cd $buildtemp
if [ $? != 0 ] ; then
  echo could not access $buildtemp...
  exit 1
else
  echo removing old files in `pwd`...
  rm -R *
fi
echo extracting files from $buildfile...
unzip -q -o $buildfile

echo accessing XPI file...
if [ ! -e $xpitemp ] ; then
  mkdir $xpitemp
fi
cd $xpitemp
if [ $? != 0 ] ; then
  echo could not access $xpitemp...
  exit 1
else
  echo removing old files in `pwd`...
  rm -R *
fi
echo extracting files from $xpifile...
unzip -q -o $xpifile

echo removing unneeded directories and files from build...
cd $buildtemp/$mozdir/chrome
dirs=`find -type d -maxdepth 1 | sed s:\./::`
for curdir in $dirs
do
  if [ $curdir != "icons" -a $curdir != "." ] ; then
    rm -R $curdir
  fi
done
rm en-*.jar
rm US.jar
cd ..
rm -R defaults/profile/US
rm -R defaults/messenger/US

echo copying XPI contents to build...
cp $xpitemp/$xpichrome/$langName.jar $buildtemp/$mozdir/chrome/
cp $xpitemp/$xpichrome/$langOnly-$curplatform.jar $buildtemp/$mozdir/chrome/
cp $xpitemp/$xpichrome/$regName.jar $buildtemp/$mozdir/chrome/

cp -r $xpitemp/$xpidefaults/* $buildtemp/$mozdir/defaults/
cp -r $buildtemp/$mozdir/defaults/profile/$regName/* $buildtemp/$mozdir/defaults/profile/
cp -r $buildtemp/$mozdir/defaults/messenger/$regName/* $buildtemp/$mozdir/defaults/messenger/

cp $xpitemp/$xpimyspell/* $buildtemp/$mozdir/components/myspell/

cp $xpitemp/$xpisplugins/* $buildtemp/$mozdir/searchplugins/

echo correcting installed-chrome.txt file...
# we're converting all newlines to "~" for saving the file in a variable but reconverting to newlines for sed
instchr=`sed -e "s:en-US.jar!/locale/en-US:$langName.jar!/locale/$langName:" $buildtemp/$mozdir/chrome/installed-chrome.txt | tr "\n" "~"`
instchr=`echo "$instchr" | tr "~" "\n" | sed -e "s:en-$curplatform.jar!/locale/en-US:$langOnly-$curplatform.jar!/locale/$langName:" | tr "\n" "~"`
for comp in $addcomponents
do
  grep "$comp" $buildtemp/$mozdir/chrome/installed-chrome.txt > /dev/null
  if [ $? != 0 ] ; then
    # if we don't have a line for this component in installed-chrome.txt, we add one
    instchr="$instchr~locale,install,url,jar:resource:/chrome/$langName.jar!/locale/$langName/$comp/"
  else
    instchr=`echo "$instchr" | tr "~" "\n" | sed -e "s:$comp.jar!/locale/en-US:$langName.jar!/locale/$langName:" | tr "\n" "~"`
  fi
done
instchr=`echo "$instchr" | tr "~" "\n" | sed -e "s:US.jar!/locale/US:$regName.jar!/locale/$regName:" | tr "\n" "~"`
# the grep in the line below removes all empty line, as every legal line has to contail some "," signs
echo "$instchr" | tr "~" "\n" | grep "," > $buildtemp/$mozdir/chrome/installed-chrome.txt

echo copying splash screen to build...
cp $usesplash $buildtemp/$mozdir/mozilla.bmp

if [ -e $adddir/* ] ; then
  echo adding additional files...
  cp -a $adddir/* $buildtemp/$mozdir/
fi

echo packing up new build file...
cd $buildtemp
zip -rX9Dq $newbuild $mozdir

echo new build should be done and ready for release in $newbuild
echo
