#!/bin/sh

# Based on docs/setup.txt

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

export LANG='';

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

echo "Building $OOINSTDIR/ooo-wrapper$BINSUFFIX";
sed -e "s|@OOINSTBASE@|$OOINSTBASE|g
	s|@SYSCONFDIR@|$SYSCONFDIR|g
	s|@BINSUFFIX@|$BINSUFFIX|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"

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 base calc draw fromtemplate html impress 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" \
		        -o "z$VENDORNAME" = "zDebian"; then
			echo ".so man1/openoffice$BINSUFFIX.1" >| $MANDIR/man1/oo${app}$BINSUFFIX.1;
		fi
	done

	# no man-page so ...
	if test "z$VENDORNAME" != "zDebian"; then
	    cp -f $TOOLSDIR/bin/ooconfig $PREFIX/bin
	    chmod +x $PREFIX/bin/ooconfig
	fi

	# create bash completion
	mkdir -p $OODESTDIR/etc/bash_completion.d
	$TOOLSDIR/bin/generate-bash-completion --binsuffix="$BINSUFFIX" $OODESTDIR/etc/bash_completion.d/ooo-wrapper.sh

	mkdir -p $PREFIX/sbin || exit 1;
	create_simple_wrapper $OOINSTBASE/program/spadmin $PREFIX/sbin/oopadmin${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 "Building $OOINSTDIR/program/java-set-classpath";
sed -e "s|@OOINSTBASE@|$OOINSTBASE|g" $TOOLSDIR/bin/java-set-classpath.in >| "$OOBUILDDIR/java-set-classpath" || exit 1;
mkdir -p $OOINSTDIR/program
cp -f $OOBUILDDIR/java-set-classpath $OOINSTDIR/program
chmod +x $OOINSTDIR/program/java-set-classpath

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

echo "Installing system files ...";
case $VENDORNAME in
  RedHat)
	# 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
	;;
  Debian)
  	# Menu icons are currently made in debian/rules
	;;
  *)
	mkdir -p $OODESTDIR/opt/gnome/share/applications
	cd $TOOLSDIR/desktop
	for source in *.desktop; do
	    dest=`echo $source | sed "s|.desktop\$||"`
	    dest="$OODESTDIR/opt/gnome/share/applications/$dest$BINSUFFIX.desktop"
	    add_version=
	    test "z$VENDORNAME" = "zNovell" -a "z$BINSUFFIX" != "z" && add_version=" ($VERSION)" || :
	    sed -e "s|@BINSUFFIX@|$BINSUFFIX|g
		    s|@VERSION@|$VERSION|g
		    s|\(^Name.*\)\$|\1$add_version|
		    s|\(^Comment.*\)\$|\1$add_version|" $source >| "$dest"  || exit 1;
	    if test "z$VENDORNAME" = "zNovell" ; then
		echo "OnlyShowIn=GNOME;" >> "$dest"
	    fi
	done

	mkdir -p $OODESTDIR/usr/share/pixmaps
	cd $TOOLSDIR/desktop
	for source in ooo-*.png ; do
	    dest=`echo $source | sed "s|.png\$||"`
	    dest="$OODESTDIR/usr/share/pixmaps/$dest$BINSUFFIX.png"
	    cp $source "$dest" || exit 1;
	done
	
	mkdir -p $OODESTDIR/usr/share/mime/packages
	cd $TOOLSDIR/desktop
	cp openoffice.xml $OODESTDIR/usr/share/mime/packages
	if test "z$RUN_POST_INSTALL_SCRIPTS" = "zyes" && 
	   which update-mime-database >/dev/null 2>&1 ; then
	    update-mime-database /usr/share/mime || :
	fi
	;;
esac

# 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

export DISPLAY=''; # clobber;
echo "Execute ooinstall ...";

cd $TOOLSDIR/bin

./ooinstall $OOINSTDIR || exit 1;
##cp -ra $OODESTDIR $OODESTDIR.1 || exit 1
##rm -r $OODESTDIR
##cp -ra $OODESTDIR.1 $OODESTDIR || exit 1
echo "Cleaning up ...";

# No idea what these files are good for (?)
# they don't appear in the RPM file lists.
rm -Rf $OOINSTDIR/share/uno_packages/cache/*

echo "Done";

# Java-disabled builds do not create help
if test -d $OOINSTDIR/help; then
    echo "Removing duplicated English help..."
  
    for lang in $OOO_LANGS_LIST ; do
	test "$lang" = "en-US" && continue || :;
	test ! -f $OOINSTDIR/help/en/sbasic.cfg -o ! -f $OOINSTDIR/help/$lang/sbasic.cfg && continue;
	if diff $OOINSTDIR/help/en/sbasic.cfg $OOINSTDIR/help/$lang/sbasic.cfg >/dev/null 2>&1 ; then
	    echo "... remove \"$lang\""
	    rm -rf $OOINSTDIR/help/$lang
	    grep -v "$OOINSTBASE/help/$lang" $OODESTDIR/gid_Module_Root.$lang >$OODESTDIR/gid_Module_Root.$lang.new
	    mv -f $OODESTDIR/gid_Module_Root.$lang.new $OODESTDIR/gid_Module_Root.$lang
	    # FIXME: the following code could be used without the condition
	    #        and should replace the lines above after only the milestones
	    #	     providing gid_Module_Langpack_Help and fixed gid_Module_Root.$lang
	    #        are supported
	    # Note: The problem with gid_Module_Root.$lang is that it still includes
	    #       %dir */help/* entries.
	    if test -f $OODESTDIR/gid_Module_Langpack_Help.$lang ; then
		grep -v "$OOINSTBASE/help/$lang" $OODESTDIR/gid_Module_Langpack_Help.$lang >$OODESTDIR/gid_Module_Langpack_Help.$lang.new
		mv -f $OODESTDIR/gid_Module_Langpack_Help.$lang.new $OODESTDIR/gid_Module_Langpack_Help.$lang
	    fi
	    echo "... link \"$lang\" to \"en\""
	    ln -sf en $OOINSTDIR/help/$lang
	    echo "$OOINSTBASE/help/$lang" >>$OODESTDIR/gid_Module_Root.$lang
	    # FIXME: the following code could be used without the condition
	    #        and should replace the line above after only the milestones
	    #	     providing gid_Module_Langpack_Help are supported
	    if test -f $OODESTDIR/gid_Module_Langpack_Help.$lang ; then
		echo "$OOINSTBASE/help/$lang" >>$OODESTDIR/gid_Module_Langpack_Help.$lang
	    fi
	fi
    done
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
        # \$3 can be regular expression
	grep "^$3\$" $2 >>$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
    sort -u $1 >$1.unique
    mv $1.unique $1
}

# remove a duplicity between two filelist
# Params: filelist_with_original filelist_with_duplicity duplicit_path
remove_duplicity_from_flists()
{
    if grep "$3" "$1" >/dev/null 2>&1 && \
       grep "$3" "$2" >/dev/null 2>&1 ; then
	perl -pi -e "s|^$3$||" $2
    fi
}

if ! test -f $OODESTDIR/gid_Module_Root; then
    echo "Failed to generate package file lists";
    exit 1;
fi

cd $OODESTDIR

if test -f gid_Module_Root_Files_2; then
    GID_MODULE_ROOT_FILES_LISTS="gid_Module_Root_Files_*"
else
    GID_MODULE_ROOT_FILES_LISTS=""
fi

if test -f gid_Module_Optional_Javafilter; then
    GID_MODULE_OPTIONAL_JAVAFILTER="gid_Module_Optional_Javafilter"
else
    GID_MODULE_OPTIONAL_JAVAFILTER=""
fi

if test "z$VENDORNAME" != "zDebian" ; then
	echo "Moving package file lists..."

	# Nasty hack for now...
	echo "$OOINSTBASE/install-dict
	$OOINSTBASE/program/java-set-classpath
	$OOINSTBASE/program/pyunorc-update64
	$OOINSTBASE/program/liblnth680l?.so" > gid_Module_Hack

	cat gid_Module_Optional_Grfflt \
		gid_Module_Prg_Base \
		gid_Module_Prg_Calc \
		gid_Module_Prg_Math \
		$GID_MODULE_OPTIONAL_JAVAFILTER \
		gid_Module_Optional_Testtool \
		gid_Module_Prg_Draw \
		gid_Module_Prg_Wrt \
		gid_Module_Optional_Xsltfiltersamples \
		gid_Module_Prg_Impress \
		gid_Module_Root \
	        $GID_MODULE_ROOT_FILES_LISTS \
		gid_Module_Optional_Pyuno \
		gid_Module_Hack \
		$BUILDDIR/dictionaries \
		| sort | uniq \
		> $BUILDDIR/common_list.txt
	
	for lang in $OOO_LANGS_LIST ; do
		lang_lists=
		test -f gid_Module_Root.$lang           	&& lang_lists="gid_Module_Root.$lang" || :
		test -f gid_Module_Langpack_Help.$lang  	&& lang_lists="$lang_lists gid_Module_Langpack_Help.$lang" || :
		test -f gid_Module_Langpack_Resource.$lang	&& lang_lists="$lang_lists gid_Module_Langpack_Resource.$lang" || :
		test -n "$lang_lists" && cat $lang_lists | sort -u >$BUILDDIR/lang_${lang}_list.txt || :
	done
	
	# sort && uniq suck but eg. gid_Module_Optional_Draw & Impress have substantial overlap
	
	#
	# FIXME: lang-packs etc.
	#    gid_Module_Optional_Oo_English
	#

	cd $BUILDDIR

	# kde subpackage
	rm -f kde_list.txt
	mv_file_between_flists kde_list.txt common_list.txt $OOINSTBASE/program/kdefilepicker
	mv_file_between_flists kde_list.txt common_list.txt $OOINSTBASE/program/libfps_kde.so
	mv_file_between_flists kde_list.txt common_list.txt $OOINSTBASE/program/libvclplug_kde[0-9]*l..so
	mv_file_between_flists kde_list.txt common_list.txt $OOINSTBASE/program/libkab1.so
	add_used_directories kde_list.txt common_list.txt
	
	# gnome subpackage
	rm -f gnome_list.txt
	test -f $OODESTDIR/gid_Module_Optional_Gnome && cp $OODESTDIR/gid_Module_Optional_Gnome gnome_list.txt || :
	mv_file_between_flists gnome_list.txt common_list.txt $OOINSTBASE/program/libevoab2.so
	mv_file_between_flists gnome_list.txt common_list.txt $OOINSTBASE/program/libfps_gnome.so
	mv_file_between_flists gnome_list.txt common_list.txt $OOINSTBASE/program/libvclplug_gtk[0-9]*l..so
	mv_file_between_flists common_list.txt gnome_list.txt $OOINSTBASE/program/ucpgvfs1.uno.so
	add_used_directories gnome_list.txt common_list.txt

	# hunspell subpackage
	rm -f hunspell_list.txt
	mv_file_between_flists hunspell_list.txt common_list.txt $OOINSTBASE/program/libhunspell[0-9]*l..so
	add_used_directories hunspell_list.txt common_list.txt

	# mono subpackage
	rm -f mono_list.txt
	mv_file_between_flists mono_list.txt common_list.txt $OOINSTBASE/program/cli_.*.dll
	mv_file_between_flists mono_list.txt common_list.txt $OOINSTBASE/program/libcli_.*.so
	add_used_directories mono_list.txt common_list.txt

	if test "z$VENDORNAME" = "zNovell" ; then
		# officebean subpackage
		rm -f officebean_list.txt
		mv_file_between_flists officebean_list.txt common_list.txt $OOINSTBASE/program/classes/officebean.jar
		mv_file_between_flists officebean_list.txt common_list.txt $OOINSTBASE/program/libofficebean.so
		add_used_directories officebean_list.txt common_list.txt
	fi

	# remove known duplicities to do not have files packaged in two packages
	# the Bulgarian fixes can be removed after the issue #54110 is fixed
	remove_duplicity_from_flists common_list.txt lang_bg_list.txt $OOINSTBASE/presets/config/arrowhd.soe
	remove_duplicity_from_flists common_list.txt lang_bg_list.txt $OOINSTBASE/presets/config/classic.sog
	remove_duplicity_from_flists common_list.txt lang_bg_list.txt $OOINSTBASE/presets/config/hatching.soh
	remove_duplicity_from_flists common_list.txt lang_bg_list.txt $OOINSTBASE/presets/config/modern.sog
	remove_duplicity_from_flists common_list.txt lang_bg_list.txt $OOINSTBASE/presets/config/palette.soc
	remove_duplicity_from_flists common_list.txt lang_bg_list.txt $OOINSTBASE/presets/config/styles.sod
	# the British fixes can be removed after the issue #54113 is fixed
	remove_duplicity_from_flists common_list.txt lang_en-GB_list.txt $OOINSTBASE/presets/config/standard.sog
else
	echo "Creating package directories..."

	test -d pkg && rm -r pkg || :

	# Create package tree (needed by Debian's dpkg)
	# create_package_directory <list_file> <directory_name>
	create_package_directory()
	{
		listfile=$1
		directory="$2"
		perl -nl \
			-e " if(/^%dir (.*)/)
					{system('mkdir', '-p', '-m', '755', \"$directory\".\$1);}
				else
					{rename('./'.\$_, \"$directory\".\$_);}
				" \
			$listfile
	}

        create_package_directory gid_Module_Optional_Grfflt             pkg/openoffice.org-common
        create_package_directory gid_Module_Root                        pkg/openoffice.org-common
        create_package_directory gid_Module_Optional_Xsltfiltersamples  pkg/openoffice.org-common
	create_package_directory gid_Module_Optional_Javafilter		pkg/openoffice.org-common
        create_package_directory gid_Module_Prg_Calc                    pkg/openoffice.org-calc
        create_package_directory gid_Module_Prg_Math                    pkg/openoffice.org-math	
        create_package_directory gid_Module_Prg_Draw                    pkg/openoffice.org-draw
        create_package_directory gid_Module_Prg_Wrt                     pkg/openoffice.org-writer
        create_package_directory gid_Module_Prg_Impress                 pkg/openoffice.org-impress
        create_package_directory gid_Module_Prg_Base                    pkg/openoffice.org-base
        create_package_directory gid_Module_Optional_Pyuno		pkg/python-uno
        create_package_directory gid_Module_Optional_Gnome		pkg/openoffice.org-gnome
        
        if [ -f gid_Module_Optional_Mailcap ]; then
            # pre m76
            create_package_directory gid_Module_Optional_Mailcap            pkg/openoffice.org-common
        else
            create_package_directory gid_Module_Root_Files_2                pkg/openoffice.org-common
            create_package_directory gid_Module_Root_Files_3                pkg/openoffice.org-common
            create_package_directory gid_Module_Root_Files_4                pkg/openoffice.org-common
            create_package_directory gid_Module_Root_Files_5                pkg/openoffice.org-common
            create_package_directory gid_Module_Root_Files_6                pkg/openoffice.org-common
            create_package_directory gid_Module_Root_Files_7                pkg/openoffice.org-common
            create_package_directory gid_Module_Root_Files_8                pkg/openoffice.org-common
            create_package_directory gid_Module_Optional_Oo_English         pkg/openoffice.org-common
            create_package_directory gid_Module_Optional_Testtool           pkg/openoffice.org-common
        fi            

	for l in `echo $OOO_LANGS_LIST | sed -e s/en-US//`; do \
		create_package_directory gid_Module_Root.$l		pkg/openoffice.org-l10n-$l; \
		create_package_directory gid_Module_Langpack_Resource.$l	pkg/openoffice.org-l10n-$l; \
	done

	# move_wrappers <directory_name> <name> [...]
	move_wrappers()
	{
		directory=$1
		shift
		mkdir -m755 -p "$directory"/usr/bin
		mkdir -m755 -p "$directory"/usr/share/man/man1
		while test -n "$1"; do
			mv usr/*bin/"$1$BINSUFFIX" "$directory"/usr/bin
			mv usr/share/man/man1/"$1$BINSUFFIX.1" "$directory"/usr/share/man/man1
			shift
		done
	}
	move_wrappers pkg/openoffice.org-common ooffice oofromtemplate
	move_wrappers pkg/openoffice.org-base oobase
	move_wrappers pkg/openoffice.org-writer oowriter oohtml ooweb
	move_wrappers pkg/openoffice.org-calc oocalc
	move_wrappers pkg/openoffice.org-impress ooimpress
	move_wrappers pkg/openoffice.org-math oomath
	move_wrappers pkg/openoffice.org-draw oodraw
	for F in sbin/oopadmin bin/ooo-wrapper; do
		mv usr/$F$BINSUFFIX pkg/openoffice.org-common/usr/bin
	done

	# Core package contains arch dependent files
	#mkdir -m755 -p pkg/openoffice.org-core/$OOINSTBASE/program/filter
	#mv pkg/openoffice.org-common/$OOINSTBASE/program/filter/* pkg/openoffice.org-core/$OOINSTBASE/program/filter
	
	# Move pyuno into seperate package
	#mkdir -m755 -p pkg/python-uno/$OOINSTBASE/program
	#( cd pkg/openoffice.org-common/$OOINSTBASE/program
	#  find -type f -maxdepth 1 \
	#       -regex '\./\(.*\.py\|.*pyuno.*\|python.*\)' \
	#	   -exec mv {} $OODESTDIR/pkg/python-uno/$OOINSTBASE/program \;
	#)

	# Move all libraries, binaries, *.rdb from -common to -core
	if [ ! -d $OODESTDIR/pkg/openoffice.org-core/$OOINSTBASE/program ]; then \
	  mkdir -p $OODESTDIR/pkg/openoffice.org-core/$OOINSTBASE/program; \
	fi &&
	( cd pkg/openoffice.org-common/$OOINSTBASE/program
	  find -type f -maxdepth 1 \
	       -regex '\./\(.*\.so.*\|.*\.bin\|pagein\|nsplugin\|kdefilepicker\|msfontextract\|.*\.rdb\|javaldx\)' \
		   -exec mv {} $OODESTDIR/pkg/openoffice.org-core/$OOINSTBASE/program \;
	)

	# install additional ooo-build scripts & misc stuff
	mv usr/share/man/man1/openoffice$BINSUFFIX.1 \
		pkg/openoffice.org-common/usr/share/man/man1	
	mkdir -p pkg/openoffice.org-common/etc/bash_completion.d
	mv etc/bash_completion.d/ooo-wrapper.sh \
		pkg/openoffice.org-common/etc/bash_completion.d
	mv .$OOINSTBASE/program/java-set-classpath \
		pkg/openoffice.org-common/$OOINSTBASE/program

	# Warn for any remaining files
	find . -path './pkg' -prune -o -not -name 'gid_Module_*' -not -type d -exec echo "File not packaged: {}" \;
fi

echo "Cleaning up lists of files...";
mv -f $OODESTDIR/gid_Module_* $BUILDDIR

cd $BUILDDIR

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

if test "z$OODESTDIR" != "z" ; then
    echo "Checking for DESTDIR inside installed files..."
    found_destdir=
    for file in `find $OODESTDIR -type f` ; do
	grep -q "$OODESTDIR" $file && echo "$file: includes the string \"$OODESTDIR\"" && found_destdir=1
    done
    if test "z$found_destdir" != "z" ; then
	echo "!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!"
	echo "The path DESTDIR:$OODESTDIR was found inside some"
	echo "installed files. It is probably a bug."
	echo 
	echo "Especially, if the DESTDIR is set to \$RPM_BUILD_ROOT"
	echo "when creating RPM packages. Even it could be a security hole"
	echo "if the application searches /var/tmp for binaries or"
	echo "config files because the directory is world-writable."
	echo "!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!"
    fi
fi

echo "Packaging succeeded";
exit 0;

