#!/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=''

for i in $OOO_LANGS; do
    # We nowadays put *all* languages in a multilingual
    # installer.
    MULTILANGS="$MULTILANGS,$i"
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

if [ -z "$DONTBUILD" ]; then
    # Build the installers.

    # Edit the relevant target line in instsetoo_native/util/makefile.mk
    # to build the multilingual installer.
    gawk '/^ALLTAR :/ { n++;
			if (n==3) {
			  printf "ALLTAR : openoffice_en-US openoffice_en-US'"$MULTILANGS"'\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

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

OUTDIR=`mktemp -d`

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

# Construct the installer CD contents

# Figuring out the exact name used for the directory is hard, as it involves
# a md5sum of the language string in the exact order it was specified
# in the instsetoo_native/util/makefile.mk. So in the DONTBUILD case
# (i.e. when we have already built the installers, and just run this script
# afterwards, you have to pass that in as an environment variable

if [ "$DONTBUILD" -a -z "$MULTILANGDIR" ]; then
    echo You must set MULTILANGDIR to the md5sum hex string used for the languages in question
    exit 1
fi

if [ -z "$MULTILANGDIR" ]; then
    MULTILANGDIR=en-US`echo $MULTILANGS | sed -e 's/,/_/g'`
    if [ `expr length $MULTILANGDIR` -gt 32 ]; then
	MULTILANGDIR=`echo $MULTILANGDIR | md5sum | sed -e "s/ .*//g"`
    fi
fi

cp -pR wntmsci10.pro/OpenOffice/msi/install/$MULTILANGDIR/. $ISOROOT

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|*.EXE)
	    # Ignore presumably non-redistributable files
	    ;;
	*)
	    mkdir -p $SRCISOROOT/src
	    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
