#!/bin/bash

# Create two ISOs for Win32:
#	-multilingual installer and additional language pack installers
#	-source code

# Requires mkisofs, from cdrtools. Compiling that from source is a
# pain, thanks to the author's insistence that his configuration and
# make system is better than everybody's elses. One cdrtools
# distribution of binaries for Cygwin known to work is at
# http://www.sbox.tugraz.at/home/t/tplank/cdrtools-2.01-win32-bin.zip
# and tucked away for safety also in go-oo.org:~ooo .

# NOTE: very much a work in progress...

set -o errexit

. ./setup

ISOTEMPLATE=$SRCDIR/win32-iso-template-$DISTRO.zip
COUNTERFILE=win32-iso-counter

MULTILANGS=''
RESTLANGS=''

for i in $OOO_LANGS; do
    case $i in
    # This are the languages for which there exists localisations
    ar|cs|da|de|el|es|fi|fr|he|hu|it|nb|nl|pl|pt-BR|pt|ru|sv|tr)
	MULTILANGS="$MULTILANGS,$i";;
    en-US)
	;;
    *)
	RESTLANGS="$RESTLANGS $i";;
    esac
done

case "$MULTILANGS" in
*,*,*,*)
    ;;
*)
    echo "This doesn't seem like a multilingual build. To make ISOs"
    echo "you should have configured a real production build, that is"
    echo "with lots of languages. For the Novell Edition,"
    echo "use --with-distro=NovellWin32ISO"
    exit 1;;
esac

cd $OOBUILDDIR

. ./winenv.set.sh

COUNTER=0
[ -f $COUNTERFILE ] && COUNTER=`cat $COUNTERFILE`
COUNTER=`expr $COUNTER + 1`

cd instsetoo_native

ZIPNAMEPREFIX=OOo-$OOO_PACKAGEVERSION-$COUNTER

# Edit the relevant target line in instsetoo_native/util/makefile.mk
# to build the multilingual installer for those languages for which
# there exists XP localisations, and language pack installers for the
# rest.
gawk '/^ALLTAR :/ { n++;
                    if (n==3) {
                      printf "ALLTAR : openoffice_en-US openoffice_en-US'"$MULTILANGS"'";
                      split ("'"$RESTLANGS"'", restlangs);
                      for (i in restlangs) {
                        printf " ooolanguagepack_%s", restlangs[i];
                      }
                      printf "\n";
		      next;
                    }
                  }
      { print; }
' <util/makefile.mk >util/makefile.mk.new

if cmp util/makefile.mk util/makefile.mk.new; then
    :
else
    echo Edited instsetoo_native/util/makefile.mk:
    diff -u0 util/makefile.mk util/makefile.mk.new || true
    mv util/makefile.mk.new util/makefile.mk
fi

# Build the installers. For some reason one cannot use the "build"
# alias in this script.
perl $SOLARENV/bin/build.pl

OUTDIR=`mktemp -d`

ISOROOT=`mktemp -d`
SRCISOROOT=$TOOLSDIR/ooo-build-$CVSTAG

# Construct the installer CD contents

cp -pR wntmsci10.pro/OpenOffice/msi/install/en-US`echo $MULTILANGS | sed -e 's/,/_/g'`/. $ISOROOT

mkdir $ISOROOT/langpacks
for i in $RESTLANGS; do
    (
    cd wntmsci10.pro/OpenOffice_languagepack/msi/install/$i
    zip -q -r $ISOROOT/langpacks/$ZIPNAMEPREFIX-$i.zip .
    )
done

cd $ISOROOT
if [ -f $ISOTEMPLATE ]; then
    unzip $ISOTEMPLATE
else
    echo "No template with contents for the installer ISO ($ISOTEMPLATE) found."
fi

cp $OOBUILDDIR/readlicense_oo/html/THIRDPARTYLICENSEREADME.html .

echo "[autorun]" > autorun.inf
echo "open=setup.exe" >> autorun.inf

# Build the installer ISO

mkisofs -quiet -J -r -V OOO-$OOO_PACKAGEVERSION-$COUNTER -o $OUTDIR/OOo-$OOO_PACKAGEVERSION-$COUNTER.iso .

# Construct the source code CD contents

cd $TOOLSDIR
make distdir >/dev/null
if [ -f download.list ]; then
    while read FILENAME; do
	case "$FILENAME" in
	*.exe)
	    # Ignore presumably non-redistributable files
	    ;;
	*)
	    cp -p src/$FILENAME $SRCISOROOT/src
	    ;;
	esac
    done <download.list
else
    echo "Missing download.list, did you remove it?"
    exit 1
fi

cd $SRCISOROOT

# Build the source code ISO

mkisofs -quiet -J -r -V OOO-src-$OOO_PACKAGEVERSION-$COUNTER -o $OUTDIR/OOo-$OOO_PACKAGEVERSION-src.iso .

cd $TOOLSDIR

echo Installer ISO in $OUTDIR/OOo-$OOO_PACKAGEVERSION-$COUNTER.iso
echo Source code ISO in $OUTDIR/OOo-src-$OOO_PACKAGEVERSION-$COUNTER.iso

# All done. Increment counter, remove temporary folders

rm -rf $ISOROOT
rm -rf $SRCISOROOT

# Store incremented counter
cd $OOBUILDDIR
echo $COUNTER >$COUNTERFILE
