#!/bin/sh

# Based on docs/setup.txt

#
# See setup for user tweakables.
#
. ./setup

export LANG='';

echo "Cleaning $OOINSTDIR";
rm -Rf $OOINSTDIR;

AUTORESPONSE=$OOBUILDDIR/autoresponse.conf

echo "Create auto-response file"
echo "[ENVIRONMENT]
INSTALLATIONMODE=INSTALL_NETWORK
INSTALLATIONTYPE=STANDARD
DESTINATIONPATH=$OOINSTDIR
OUTERPATH=
LOGFILE=
LANGUAGELIST=<LANGUAGE>

[JAVA]
JavaSupport=preinstalled_or_none" > $AUTORESPONSE

echo "Building $OOINSTDIR/ooo-wrapper$BINSUFFIX";
sed -e "s|@OOINSTBASE@|$OOINSTBASE|g
	s|@SYSCONFDIR@|$SYSCONFDIR|g
	s|@BINSUFFIX@|$BINSUFFIX|g
	s|@VERSION@|$VERSION|g
	s|@OOOINSTALLDIRNAME@|$OOOINSTALLDIRNAME|g
	s|@VENDORNAME@|$VENDORNAME|g" $TOOLSDIR/bin/ooo-wrapper.in \
		>| "$OOBUILDDIR/ooo-wrapper$BINSUFFIX" || exit 1;
mkdir -p $PREFIX/bin

# Skip the versioning and linking dance of the wrapper script for Red Hat
if test "z$VENDORNAME" = "zRedHat"; then
	cp -f $OOBUILDDIR/ooo-wrapper$BINSUFFIX $PREFIX/bin/ooffice
	chmod +x $PREFIX/bin/ooffice
	for app in calc draw impress html math writer; do
		ln -sf /usr/bin/ooffice $PREFIX/bin/oo${app}
	done
else
	mkdir -p $PREFIX/man/man1
	cp -f $OOBUILDDIR/ooo-wrapper$BINSUFFIX $PREFIX/bin
	chmod +x $PREFIX/bin/ooo-wrapper$BINSUFFIX
	for app in calc draw impress html math writer ffice; do
		ln -sf ooo-wrapper${BINSUFFIX} $PREFIX/bin/oo${app}${BINSUFFIX}
		if test "z$VENDORNAME" = "zXimian"; then
			echo ".so man1/openoffice$BINSUFFIX.1" >| $PREFIX/man/man1/oo${app}$BINSUFFIX.1;
		fi
	done
fi

if test "z$VENDORNAME" != "zRedHat"; then
	mkdir -p $MANDIR/man1
	echo "Generating man page ...";
	sed -e "s|@BINSUFFIX@|$BINSUFFIX|g" $TOOLSDIR/man/openoffice.1.in \
		>| "$OOBUILDDIR/openoffice$BINSUFFIX.1" || exit 1;
	cp -f $OOBUILDDIR/openoffice$BINSUFFIX.1 $MANDIR/man1
fi

echo "Building $OOINSTDIR/install-dict";
sed -e "s|@OOINSTBASE@|$OOINSTBASE|g" $TOOLSDIR/bin/install-dict.in >| "$OOBUILDDIR/install-dict" || exit 1;
mkdir -p $OOINSTDIR
cp -f $OOBUILDDIR/install-dict $OOINSTDIR
chmod +x $OOINSTDIR/install-dict

mkdir -p $SYSCONFDIR/openoffice

if test "z$VENDORNAME" = "zRedHat"; then
	cp -f $TOOLSDIR/etc/redhat-autoresponse$BINSUFFIX.conf $SYSCONFDIR/openoffice/autoresponse.conf
else
	cp -f $TOOLSDIR/etc/autoresponse$BINSUFFIX.conf $SYSCONFDIR/openoffice
fi

echo "Installing system files ...";
if test "z$VENDORNAME" = "zRedHat" ; then
	# Install .desktop files for Red Hat distributions
	mkdir -p $PREFIX/share/applications
	for i in openoffice-printeradmin openoffice-setup redhat-drawing \
			redhat-math redhat-presentations redhat-word-processor redhat-spreadsheet; do
		cp -f /usr/share/desktop-menu-patches/$i.desktop $PREFIX/share/applications/$i.desktop
		echo "StartupNotify=true" >> $PREFIX/share/applications/$i.desktop
	done

	# Icons are copied into the local install directory from the specfile...
	mkdir -p $PREFIX/share/pixmaps
	cp $TOOLSDIR/desktop/0*.png $PREFIX/share/pixmaps
	cp $TOOLSDIR/desktop/5*.png $PREFIX/share/pixmaps
	cp $TOOLSDIR/desktop/ooo_*.png $PREFIX/share/pixmaps
else
	mkdir -p $PREFIX/share/gnome/ximian/applications
	cp $TOOLSDIR/desktop/*$BINSUFFIX.desktop $PREFIX/share/gnome/ximian/applications

	mkdir -p $PREFIX/share/pixmaps
	cp $TOOLSDIR/desktop/ximian-openoffice-*.png $PREFIX/share/pixmaps
fi

# Disable odk stuff for now
if test "disable" = "this"; then
    echo "Installing the ODK";
    ODK_SRC=$OOBUILDDIR/odk$BUILD_MAGIC;
    ODK_INCLUDE=$OOINSTDIR/include
    echo " unzip"; 
    rm -Rf $ODK_SRC
    tar -C $OOBUILDDIR -xzf $OOBUILDDIR/solver/$BUILD_MAGIC/$ARCHITECTURE/bin/odk$BUILD_MAGIC.tar.gz;
    echo " setup $OOINSTDIR"; 
    mkdir -p $ODK_INCLUDE
    mkdir -p $OOINSTDIR
    mkdir -p $OOINSTDIR/utils
    mkdir -p $OOINSTDIR/program
    mkdir -p $OOINSTDIR/idl
    mkdir -p $OOINSTDIR/xml
    mkdir -p $OOINSTDIR/share/doc/openoffice$BINSUFFIX
    mkdir -p $PREFIX/lib/pkgconfig
    echo " re-arrange files";
    cp -a $ODK_SRC/include/* $ODK_INCLUDE
    cp -a $ODK_SRC/linux/lib/* $OOINSTDIR/program
    cp -a $ODK_SRC/linux/bin/* $OOINSTDIR/utils
    cp -a $ODK_SRC/idl/* $OOINSTDIR/idl
    cp -a $ODK_SRC/docs/* $OOINSTDIR/share/doc/openoffice$BINSUFFIX
    cp -a $ODK_SRC/examples $OOINSTDIR/share/doc/openoffice$BINSUFFIX
    cp -a $ODK_SRC/xml/* $OOINSTDIR/xml
    echo " create pkgconfig file"; 
    echo "
libdir=$OOINSTBASE/program
includedir=$OOINSTBASE/include
idlinclude=$OOINSTBASE/idl
xmlinclude=$OOINSTBASE/xml
toolsdir=$OOINSTBASE/utils

Name: openoffice$BINSUFFIX
Description: The OpenOffice.org infrastructure
Version: $VERSION
Libs: -L\${libdir} -lprot_uno_uno
Cflags: -I\${includeddir}" > $PREFIX/lib/pkgconfig/openoffice$BINSUFFIX.pc
fi

INSTALLER_PATH="`tcsh -f -c "cd $OOBUILDDIR; source $OOBUILDDIR/*.Set; echo \"$OOBUILDDIR/instsetoo/\\$INPATH/01/normal\""`";
export DISPLAY=''; # clobber;
echo "Execute from $INSTALLER_PATH to $OOINSTDIR [$LANG]";
cd $INSTALLER_PATH || exit 1;

./setup -net -v -r:$AUTORESPONSE -nogui || exit 1;
echo "Done";

# install vcl module for more widget sets if needed
if test "${OOO_WIDGET_SETS/*,*/comma}" = "comma" ; then
    IFS_saved="$IFS"
    IFS=","
    for widgetset in ${OOO_WIDGET_SETS} ; do
	install -m 755 -d "$OOINSTDIR/program/widgets/$widgetset" || exit 1;
	install -m 755 "$OOBUILDDIR/vcl/$ARCHITECTURE.$widgetset/lib/"libvcl* \
		       "$OOINSTDIR/program/widgets/$widgetset/" || exit 1;
    done
    IFS="$IFS_saved"

    # remove the library installed by ./setup
    rm "$OOINSTDIR/program/"libvcl* || exit 1;
    
    # link the default variant
    ln -s ${OOO_WIDGET_SETS%%,*} "$OOINSTDIR/program/widgets/default" || exit 1;
fi

# strip binaries
if test "z$VENDORNAME" != "zRedHat" ; then
    echo "Stripping binaries";
    chmod 755 $OOINSTDIR/program/*.so
    strip $OOINSTDIR/program/*.so
    #
    if test -f $OOINSTDIR/program/widgets/default/libvcl*.so ; then
	chmod 755 $OOINSTDIR/program/widgets/*/*.so
	strip $OOINSTDIR/program/widgets/*/*.so
    fi
fi

cd $BUILDDIR || exit 1;

# remove OODESTDIR from installed files
rm -f list.fixdir.txt || exit 1;
test -d $OOINSTDIR/share/cde/types && find $OOINSTDIR/share/cde/types -name "*.dt" >>list.fixdir.txt
test -f $OOINSTDIR/share/gnome/net/.directory && echo $OOINSTDIR/share/gnome/net/.directory >>list.fixdir.txt
test -d $OOINSTDIR/share/gnome && find $OOINSTDIR/share/gnome -name "*.desktop" >>list.fixdir.txt
test -d $OOINSTDIR/share/kde && find $OOINSTDIR/share/kde -name "*.desktop" >>list.fixdir.txt
test -f $OOINSTDIR/share/config/javarc && echo $OOINSTDIR/share/config/javarc >>list.fixdir.txt
test -f $OOINSTDIR/program/setup.log && echo $OOINSTDIR/program/setup.log >>list.fixdir.txt
test -f $OOINSTDIR/program/instdb.ins &&  echo $OOINSTDIR/program/instdb.ins >>list.fixdir.txt
#
for file in `cat list.fixdir.txt` ; do
    perl -pi -e "s|$OOINSTDIR||g" $file
done

# create list of common files and directories
find $OOINSTDIR -type d -exec echo %dir {}/ \; >$BUILDDIR/common_list.txt || exit 1;
find $OOINSTDIR "(" -type f -or -type l ")" >>$BUILDDIR/common_list.txt || exit 1;

echo "Done...";

echo "Building lang-packs ...";

cd $TOOLSDIR/bin || exit 1;
./package-lang || exit 1;

echo "Installing localized help contents..."

$TOOLSDIR/bin/install-help || exit 1;

echo "Cleaning up list of files...";

cd $BUILDDIR

# remove English specific files
# remove everything from lang_en_list.txt
sed "s|^\(.*\)\$|^\1\$|" lang_en_list.txt >lang_en_list.txt.pattern
grep -v -f lang_en_list.txt.pattern common_list.txt >common_list.txt.new
mv common_list.txt.new common_list.txt
# add back directories which are still used
sed -n "s|^%dir \(.*\)\$|^\1|p" lang_en_list.txt >lang_en_list.txt.pattern
grep -o -f lang_en_list.txt.pattern common_list.txt | sort -u >common_list.txt.new
sed "s|^|%dir |" common_list.txt.new >>common_list.txt
rm lang_en_list.txt.pattern
# sort the list again
sort -u common_list.txt >common_list.txt.new
mv common_list.txt.new common_list.txt

# move English help to an extra list
grep "$OOINSTDIR/help/en" lang_en_list.txt >help_en_list.txt
grep -v "$OOINSTDIR/help/en" lang_en_list.txt >lang_en_list.txt.new
mv lang_en_list.txt.new lang_en_list.txt

# remove $DESTDIR
for file in *list.txt ; do
    perl -pi -e "s|$OODESTDIR||" $file
done

# Red Hat Post-install cleanup
if test "z$VENDORNAME" = "zRedHat" ; then
	# Fix openoffice/share/kde/net/applnk paths
	perl -pi -e "/^Module gid_Module_Optional_Kde/ .. /^End/ and s|YES|NO|g" $PREFIX/lib/$OOOINSTALLDIRNAME/program/instdb.ins
	perl -pi -e "/^Installation gid_Installation/ .. /^End/ and s|$PREFIX||g" $PREFIX/lib/$OOOINSTALLDIRNAME/program/instdb.ins
	perl -pi -e "/^Directory gid_Dir_Home_Gnome_Apps_Product/ .. /^End/ and s|OpenOffice\.org\ 1\.1\.0|OpenOffice\.org|g" $PREFIX/lib/$OOOINSTALLDIRNAME/program/instdb.ins
	perl -pi -e "/^Directory gid_Dir_Share_Kde_Net_Applnk_Product/ .. /^End/ and s|OpenOffice\.org\ 1\.1\.0|OpenOffice\.org|g" $PREFIX/lib/$OOOINSTALLDIRNAME/program/instdb.ins
	perl -pi -e "/^Directory gid_Dir_Kde_Share_Applnk_Product/ .. /^End/ and s|OpenOffice\.org\ 1\.1\.0|OpenOffice\.org|g" $PREFIX/lib/$OOOINSTALLDIRNAME/program/instdb.ins
	perl -pi -e "/^Procedure gid_Procedure_Kde_Inst/ .. /^End/ and s|OpenOffice\.org\ 1\.1\.0|OpenOffice\.org|g" $PREFIX/lib/$OOOINSTALLDIRNAME/program/instdb.ins
	perl -pi -e "/^Procedure gid_Procedure_Gnome_Install/ .. /^End/ and s|OpenOffice\.org\ 1\.1\.0|OpenOffice\.org|g" $PREFIX/lib/$OOOINSTALLDIRNAME/program/instdb.ins
	perl -pi -e "/^Procedure gid_Procedure_Gnome_Deinstall/ .. /^End/ and s|1\.1\.0||g" $PREFIX/lib/$OOOINSTALLDIRNAME/program/instdb.ins

	# KDE parts
	mkdir -p $PREFIX/share/icons
	cp -alf $PREFIX/lib/$OOOINSTALLDIRNAME/share/kde/net/share/icons/* \
	        $PREFIX/share/icons/

	# MIME-types
	# KDE
	mkdir -p $PREFIX/share/mimelnk
	# copy to global location
	cp -af  $PREFIX/lib/$OOOINSTALLDIRNAME/share/kde/net/share/mimelnk/* \
	        $PREFIX/share/mimelnk/

	## Fix instdb.ins, to *not* install local copies of these
	for entry in Kdeapplnk Kdemimetext Kdeicons Gnome_Apps Gnome_Icons Gnome2_Apps; do
		perl -pi -e "/^File gid_File_Extra_$entry/ .. /^End/ and (\
			s|^\tSize\s+\= .*|\tSize\t\t = 0;\r| or \
			s|^\tArchiveFiles\s+\= .*|\tArchiveFiles\t = 0;\r| or \
			s|^\tArchiveSize\s+\= .*|\tArchiveSize\t = 0;\r| or \
			s|^\tContains\s+\= .*|\tContains\t = ();\r| or \
			s|\t\t\t\t\t\".*|\r|g)" \
			$PREFIX/lib/$OOOINSTALLDIRNAME/program/instdb.ins
	done
fi

echo "Packaging succeeded";
exit 0;

