#!/bin/sh

# make-mozilla-linux-release 0.1
# creating a .tar.gz Linux Mozilla build from an en-US .tar.gz 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-i686-pc-linux-gnu-1.7.5.tar.gz
#buildfile=/mnt/mozilla/mozilla-i686-pc-linux-gnu-gtk2+xft-1.8a5.tar.gz
xpifile=/mnt/mozilla/mozilla-1.7.5.de-AT.langpack.xpi
xpitemp=/mnt/mozilla/xpi/buildtemp
buildtemp=/opt/mozilla1.7.5
newbuild=/mnt/mozilla/mozilla-1.7.5.de-AT.linux-i686-gtk1.tar.gz
#newbuild=/mnt/mozilla/mozilla-1.8a5.de-AT.linux-i686.tar.bz2
usesplash=/mnt/mozilla/projects/splash/splash.xpm
adddir=/mnt/mozilla/jar/add-files/bin/
xpichrome=chrome
xpidefaults=defaults
xpisplugins=searchplugins
xpimyspell=myspell
langOnly="de"
regName="AT"
extract_cmd="tar -xzf"
#pack_cmd="tar -czf"
#extract_cmd="tar -xjf"
pack_cmd="tar -cjf"

langName="$langOnly-$regName"
addcomponents="inspector chatzilla venkman calendar"
#addcomponents="inspector chatzilla calendar"
curplatform="unix"
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...
$extract_cmd $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
chmod -R a+rw .

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/splash.xpm

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

echo packing up new build file...
cd $buildtemp
$pack_cmd $newbuild $mozdir

chown robert.users $newbuild

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