#!/bin/sh

# Based on docs/setup.txt

#
# See setup for user tweakables.
#
. ./setup
. $OOBUILDDIR/*.[sS]et.sh
. ./setup

export LANG='';

# if a JDK from JPackage project (http://www.jpackage.org/) was used
# for build, we want to have rather the JRE in the default configuration.
# It's because a JRE is enough to use java support in OOo and it does
# not work if only a JRE from Jpackage project is installed but the JDK is
# configured.
test "$JAVA_HOME" = "/usr/lib/jvm/java" -a -d "/usr/lib/jvm/jre" && \
  export JAVA_HOME="/usr/lib/jvm/jre"

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|@OOO_BUILDVERSION@|$OOO_BUILDVERSION|g
	s|@OOOINSTALLDIRNAME@|$OOOINSTALLDIRNAME|g
	s|@VENDORNAME@|$VENDORNAME|g" $TOOLSDIR/bin/ooo-wrapper.in \
		>| "$OOBUILDDIR/ooo-wrapper$BINSUFFIX" || exit 1;
mkdir -p $PREFIX/bin

create_simple_wrapper()
{
    cat <<EOT >$2
#!/bin/sh

# Keep in ~sync with ooo-wrapper

SystemInstallDir="$OOINSTBASE"

if [ -n "\$GNOME_DESKTOP_SESSION_ID" -a -d "\$SystemInstallDir/program/resource.gnome" ]; then
    icon_set="gnome"
elif [ -n "\$KDE_FULL_SESSION" -a -d "\$SystemInstallDir/program/resource.kde" ]; then
    icon_set="kde"
fi

if [ -z "\$icon_set" ]; then
    if [ -d "\$SystemInstallDir/program/resource.default" ]; then
	icon_set="default"
    elif [ -d "\$SystemInstallDir/program/resource.kde" ]; then
	icon_set="kde"
    elif [ -d "\$SystemInstallDir/program/resource.gnome" ]; then
	icon_set="gnome"
    else
	icon_set="default"
    fi
fi
    
if [ -d "\$SystemInstallDir/program/resource.\$icon_set" ]; then
    OOO_PREFERRED_RESOURCE_PATH="\$SystemInstallDir/program/resource.\$icon_set"
    export OOO_PREFERRED_RESOURCE_PATH
fi

exec "$1" "\$@"
EOT
    chmod 755 $2
}

# 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 $MANDIR/man1
	cp -f $OOBUILDDIR/ooo-wrapper$BINSUFFIX $PREFIX/bin
	chmod +x $PREFIX/bin/ooo-wrapper$BINSUFFIX
	for app in calc draw fromtemplate impress html math web writer ffice; do
		ln -sf ooo-wrapper${BINSUFFIX} $PREFIX/bin/oo${app}${BINSUFFIX}
		if test "z$VENDORNAME" = "zNovell" -o "z$VENDORNAME" = "zSUSE" -o "z$VENDORNAME" = "zPLD"; then
			echo ".so man1/openoffice$BINSUFFIX.1" >| $MANDIR/man1/oo${app}$BINSUFFIX.1;
		fi
	done

	mkdir -p $PREFIX/sbin || exit 1;
	create_simple_wrapper $OOINSTBASE/program/spadmin $PREFIX/sbin/oopadmin${BINSUFFIX} || exit 1;
	create_simple_wrapper $OOINSTBASE/program/jvmsetup $PREFIX/sbin/oojvmsetup${BINSUFFIX} || exit 1;
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

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
		ln -sf /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
elif test "z$VENDORNAME" = "zNovell" -o "z$VENDORNAME" = "zSUSE" ; then
	mkdir -p $OODESTDIR/opt/gnome/share/applications
	cp $TOOLSDIR/desktop/*$BINSUFFIX.desktop $OODESTDIR/opt/gnome/share/applications
	for file in $OODESTDIR/opt/gnome/share/applications/*.desktop ; do
		echo "OnlyShowIn=GNOME;" >> $file
	done

	mkdir -p $OODESTDIR/opt/gnome/share/pixmaps
	cp $TOOLSDIR/desktop/ximian-openoffice-*.png $OODESTDIR/opt/gnome/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$UPD;
    ODK_INCLUDE=$OOINSTDIR/include
    echo " unzip"; 
    rm -Rf $ODK_SRC
    tar -C $OOBUILDDIR -xzf $OOBUILDDIR/solver/$UPD/$INPATH/bin/odk$UPD.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";

# strip binaries
if test "z$VENDORNAME" != "zRedHat" ; then
    echo "Stripping binaries";
    chmod 755 $OOINSTDIR/program/*.so
    strip $OOINSTDIR/program/*.so
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|$OODESTDIR||g" $file
done
rm -f list.fixdir.txt || exit 1;

echo "Done...";

echo "Installing extra dictionaries..."

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


# 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 "Building lang-packs ...";

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

# FIXME: this hack is not needed if the patch scp-no-ooo-res.diff is applied
test -f $OOINSTDIR/program/resource/ooo${UPD}01.res &&
  echo "$OOINSTDIR/program/resource/ooo${UPD}01.res" >>$BUILDDIR/lang_en_list.txt

echo "Installing localized help contents..."

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

# install more icons sets if required
if (echo "${OOO_ICONS_NAMES}" | grep -q " ") ; then

    echo "Installing extra icons sets..."

    cd $OOINSTDIR/program/resource

    # install resource files with all the selected icons set
    for ooo_icons_name in $OOO_ICONS_NAMES ; do
	mkdir -p $OOINSTDIR/program/resource.$ooo_icons_name
	for file in *.res ; do
	    # use the OOo specific source file instead of the SO specific one
	    # this hack won't be needed with OOo, version 2.0, #28285
	    source_file=`echo $file | sed "s/^iso\($UPD[0-9][0-9].res\)/ooo\1/"`
	    cp -vf $OOBUILDDIR/solver/$UPD/$INPATH/bin.$ooo_icons_name/$source_file \
        	../resource.$ooo_icons_name/$file
	done
	# update file lists
	for file in $BUILDDIR/*list.txt ; do
	    perl -pi -e "s|^($OOINSTDIR/program)/resource/(.*res)\$|\1/resource/\2\n\1/resource.$ooo_icons_name/\2|;" \
        	     -e "s|^(%dir $OOINSTDIR/program)/resource/$|\1/resource/\n\1/resource.$ooo_icons_name/|" $file
	done	
    done
    
    cd $BUILDDIR

    # remove original resource files
    rm -rf $OOINSTDIR/program/resource
    # link to the default resource
    ln -sf resource.${OOO_ICONS_NAMES%% *} $OOINSTDIR/program/resource.default
    # update file lists
    for file in $BUILDDIR/*list.txt ; do
	perl -pi -e "s|^$OOINSTDIR/program/resource/.*res$||;" \
        	 -e "s|^%dir $OOINSTDIR/program/resource/$||" $file
    done
    # the link to the default resource dir should be only in the main package
    echo "$OOINSTDIR/program/resource.default" >>common_list.txt
fi	

# move one file from one list of files to a second one
# Params: target_file_list source_file_list file_to_move
mv_file_between_flists()
{
    if grep "^$3\$" $2 >/dev/null 2>&1 ; then
	echo "$3" >>$1
	perl -pi -e "s|^$3$||" $2
    fi
}

# add the directories from the source list of files to the target list of
# file which are used in the target list of files but are missing there
# Params: target_file_list source_file_list
add_used_directories()
{
    sort -u -r $2 | sed -n "s|^%dir \(.*\)\$|s%^\\\\(\1\\\\).*%\\\\1%p|p" >$2.pattern
    sed -n -f $2.pattern $1 | sort -u | sed "s|^|%dir |" >>$1
    rm $2.pattern
}

echo "Creating specialized lists of files..."

cd $BUILDDIR

# kde subpackage
rm -f kde_list.txt
mv_file_between_flists kde_list.txt common_list.txt $OOINSTDIR/program/kdefilepicker
mv_file_between_flists kde_list.txt common_list.txt $OOINSTDIR/program/libfps_kde.so
mv_file_between_flists kde_list.txt common_list.txt $OOINSTDIR/program/libvclplug_kde645li.so
add_used_directories kde_list.txt common_list.txt

# gnome subpackage
rm -f gnome_list.txt
mv_file_between_flists gnome_list.txt common_list.txt $OOINSTDIR/program/libevoab2.so
mv_file_between_flists gnome_list.txt common_list.txt $OOINSTDIR/program/libfps_gnome.so
mv_file_between_flists gnome_list.txt common_list.txt $OOINSTDIR/program/libvclplug_gtk645li.so
add_used_directories gnome_list.txt common_list.txt

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

cd $BUILDDIR

# remove English specific files
# remove everything mentioned in lang_en_list.txt from common_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
add_used_directories common_list.txt lang_en_list.txt
# 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
echo "%dir $OOINSTDIR/" >help_en_list.txt
echo "%dir $OOINSTDIR/help/" >>help_en_list.txt
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

# mark the config files
if test "z$RPM_CONFIG_FILE_TAGS" != "z" ; then
    perl -pi -e "s|^($OOINSTBASE/help/.*\.xsl)\$|$RPM_CONFIG_FILE_TAGS \\1|;" \
	    -e "s|^($OOINSTBASE/help/.*\.css)\$|$RPM_CONFIG_FILE_TAGS \\1|;" \
	    -e "s|^($OOINSTBASE/program/[a-zA-Z0-9]*rc)\$|$RPM_CONFIG_FILE_TAGS \\1|;" \
	    -e "s|^($OOINSTBASE/program/.*\.xsl)\$|$RPM_CONFIG_FILE_TAGS \\1|;" \
	    -e "s|^($OOINSTBASE/share/config/[a-zA-Z0-9]*rc)\$|$RPM_CONFIG_FILE_TAGS \\1|;" \
	    -e "s|^($OOINSTBASE/share/dict/ooo/.*\.lst)\$|$RPM_CONFIG_FILE_TAGS \\1|;" \
	    -e "s|^($OOINSTBASE/share/psprint/.*\.conf)\$|$RPM_CONFIG_FILE_TAGS \\1|;" \
	    -e "s|^($OOINSTBASE/share/psprint/.*\.PS)\$|$RPM_CONFIG_FILE_TAGS \\1|;" \
	    -e "s|^($OOINSTBASE/share/registry/.*\.xcu)\$|$RPM_CONFIG_FILE_TAGS \\1|;" \
	    -e "s|^($OOINSTBASE/share/registry/.*\.properties)\$|$RPM_CONFIG_FILE_TAGS \\1|;" \
	    -e "s|^($OOINSTBASE/share/registry/.*\.xcs)\$|$RPM_CONFIG_FILE_TAGS \\1|;" \
	    -e "s|^($OOINSTBASE/user/config/.*\.so.)\$|$RPM_CONFIG_FILE_TAGS \\1|;" \
	*_list.txt
fi

# 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

	## 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;

