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

sed -e "s|@OOINSTBASE@|$OOINSTBASE|g" $TOOLSDIR/bin/ootool.in \
		>| "$OOBUILDDIR/ootool$BINSUFFIX" || exit 1;

sed -e "s|@OOINSTBASE@|$OOINSTBASE|g" -e "s|@LIBDIRBASE@|$LIBDIRBASE|g"  $TOOLSDIR/bin/ootestapi.in \
		>| "$OOBUILDDIR/ootestapi$BINSUFFIX" || exit 1;

sed -e "s|@OOINSTBASE@|$OOINSTBASE|g" $TOOLSDIR/bin/ootesttool.in \
		>| "$OOBUILDDIR/ootesttool$BINSUFFIX" || exit 1;

sed -e "s|@OOINSTBASE@|$OOINSTBASE|g" $TOOLSDIR/bin/oosmoketest.in \
		>| "$OOBUILDDIR/oosmoketest$BINSUFFIX" || exit 1;

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

# Keep in ~sync with ooo-wrapper

SystemInstallDir="$OOINSTBASE"

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

create_qstart_wrapper()
{
    cat <<EOT >$1
#!/bin/sh
export OOO_EXTRA_ARG='$2'
$OOINSTBASE/program/ooqstart $3 "\$@"
EOT
    chmod 755 $1
}

install_script()
{
    cp -f $1 $2
    chmod +x $2
}

# Skip the versioning and linking dance of the wrapper script for Red Hat
if test "z$VENDORNAME" = "zRedHat"; then
	install_script $OOBUILDDIR/ooo-wrapper$BINSUFFIX $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
	if test "z$ENABLE_QUICKSTART" = "z"; then
	    install_script $OOBUILDDIR/ooo-wrapper$BINSUFFIX $PREFIX/bin/ooo-wrapper$BINSUFFIX
	    for app in base calc draw fromtemplate impress math web writer ffice; do
		ln -sf ooo-wrapper${BINSUFFIX} $PREFIX/bin/oo${app}${BINSUFFIX}
		if test "z$VENDORNAME" = "zNovell" -o "z$VENDORNAME" = "zPLD" \
		    -o "z$VENDORNAME" = "zDebian"; then
		    echo ".so man1/openoffice$BINSUFFIX.1" >| $MANDIR/man1/oo${app}$BINSUFFIX.1;
		fi
	    done
	else
	    for app in calc draw impress math web writer base; do
		create_qstart_wrapper "$PREFIX/bin/oo${app}${BINSUFFIX}" "-${app}" "" || exit 1;
	    done
	    create_qstart_wrapper "$PREFIX/bin/oofromtemplate${BINSUFFIX}" "" "slot:5500" || exit 1;
	    create_qstart_wrapper "$PREFIX/bin/ooffice${BINSUFFIX}" "" "" || exit 1;
	    if test "z$VENDORNAME" = "zNovell" -o "z$VENDORNAME" = "zPLD" \
		-o "z$VENDORNAME" = "zDebian"; then
		for app in calc draw impress math web writer base fromtemplate ffice ; do
		    echo ".so man1/openoffice$BINSUFFIX.1" >| $MANDIR/man1/oo${app}$BINSUFFIX.1;
		done
	    fi
	fi

	# /usr/bin/ooffice symlink is necessary by java UNO components to find
	# the UNO installation using $PATH, see
	# http://udk.openoffice.org/common/man/spec/transparentofficecomponents.html
	# Note: if you want to support parallel installation of more OOo versions
	#       you cannot include this link directly into the package
	#       For example, the Novell package mark this symlink as %ghost
	#	and update it in %post and %postun
	ln -sf $OOINSTBASE/program/soffice $PREFIX/bin/soffice

	# no man-page so ...
	if test "z$VENDORNAME" != "zDebian"; then
	    install_script $TOOLSDIR/bin/ooconfig $PREFIX/bin/ooconfig
	    install_script $OOBUILDDIR/ootool$BINSUFFIX $PREFIX/bin/ootool$BINSUFFIX
	fi

	# create bash completion
	mkdir -p $OODESTDIR/etc/bash_completion.d
	if test "z$ENABLE_QUICKSTART" = "z"; then
	    $TOOLSDIR/bin/generate-bash-completion --binsuffix="$BINSUFFIX" $TOOLSDIR/bin/bash-completion.in $OODESTDIR/etc/bash_completion.d/ooo-wrapper$BINSUFFIX.sh
	else
	    $TOOLSDIR/bin/generate-bash-completion --binsuffix="$BINSUFFIX" $TOOLSDIR/bin/bash-completion.in $OODESTDIR/etc/bash_completion.d/ooffice${BINSUFFIX}.sh
	fi
fi

if test "z$VENDORNAME" != "zRedHat"; then
	mkdir -p $MANDIR/man1
	echo "Generating man page ...";
	man_page_in=$TOOLSDIR/man/openoffice.1.in
	# use the distro specific man page if available
	if test -f $TOOLSDIR/man/openoffice.1_${DISTRO%%-*}.in ; then
	    man_page_in=$TOOLSDIR/man/openoffice.1_${DISTRO%%-*}.in
	fi
	sed -e "s|@BINSUFFIX@|$BINSUFFIX|g" $man_page_in \
		>| "$OOBUILDDIR/openoffice$BINSUFFIX.1" || exit 1;
	cp -f $OOBUILDDIR/openoffice$BINSUFFIX.1 $MANDIR/man1
fi

mkdir -p $OOINSTDIR/program

echo "Building $OOINSTDIR/install-dict";
sed -e "s|@OOINSTBASE@|$OOINSTBASE|g" $TOOLSDIR/bin/install-dict.in >| "$OOBUILDDIR/install-dict" || exit 1;
install_script $OOBUILDDIR/install-dict $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;
install_script $OOBUILDDIR/java-set-classpath $OOINSTDIR/program/java-set-classpath

echo "Building $OOINSTDIR/program/pyunorc-update64";
sed -e "s|@OOINSTBASE@|$OOINSTBASE|g" $TOOLSDIR/bin/pyunorc-update64.in >| "$OOBUILDDIR/pyunorc-update64" || exit 1;
install_script $OOBUILDDIR/pyunorc-update64 $OOINSTDIR/program/pyunorc-update64

echo "Installing extra en-US templates ..."
mkdir -p $OOINSTDIR/share/template/en-US/forms
cp $TOOLSDIR/templates/resume.ott $OOINSTDIR/share/template/en-US/forms
mkdir -p $OOINSTDIR/share/template/en-US/officorr
cp $TOOLSDIR/templates/project-proposal.ott $OOINSTDIR/share/template/en-US/officorr

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/usr/share/applications
	cd $TOOLSDIR/desktop
	for source in *.desktop ; do
	    dest=`echo $source | sed "s|.desktop\$||"`
	    dest="$OODESTDIR/usr/share/applications/$dest$BINSUFFIX.desktop"
	    add_version=
	    test "z$VENDORNAME" = "zNovell" -a "z$BINSUFFIX" != "z" && add_version=" ($VERSION)" || :
	    sed -e "s|\(^Name.*\)\$|\1$add_version|
		    s|\(^Comment.*\)\$|\1$add_version|" $source >| "$dest"  || exit 1;
	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
	
	if test "z$VENDORNAME" = "zNovell" ; then
		# add GNOME MIME info and the application registry the old way
		# it is necessary for NLD9
		mkdir -p $OODESTDIR/opt/gnome/share/application-registry
		cp $TOOLSDIR/desktop/openoffice.applications \
		    $OODESTDIR/opt/gnome/share/application-registry/openoffice$BINSUFFIX.applications || exit 1;
		#
		mkdir -p $OODESTDIR/opt/gnome/share/mime-info
		cp $TOOLSDIR/desktop/openoffice.mime \
		    $OODESTDIR/opt/gnome/share/mime-info/openoffice$BINSUFFIX.mime || exit 1;
		cp $TOOLSDIR/desktop/openoffice.keys \
		    $OODESTDIR/opt/gnome/share/mime-info/openoffice$BINSUFFIX.keys || exit 1;
		if test "z$BINSUFFIX" != "z" ; then
		    cp $TOOLSDIR/desktop/openoffice-extra.keys \
			$OODESTDIR/opt/gnome/share/mime-info/openoffice$BINSUFFIX-extra.keys || exit 1;
		fi
	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 $LIBDIRBASE/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}" > $LIBDIRBASE/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";

remove_help_localization()
{
    lang=$1

    # nothing to be done if the localization is en-US if it does not exist
    # or if it is already removed
    test "$lang" = "en-US" -o \
          ! -e $OOINSTDIR/help/$lang -o \
	  -L $OOINSTDIR/help/$lang && return;

    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.
    # Note: It was still necessary on ppc with gcj (OOo-2.0.2). Strange. Have to
    # investigate it later.
    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

    # Note: We created a compat symlink in the past. It is no longer necessary.
    # We do not want it because RPM has problems with update when we remove
    # poor localizations in never packages
}

# Check if the English help is installed and is in the main package (is first on the list)
# Note that Java-disabled builds do not create help at all.
if test -f $OOINSTDIR/help/en/sbasic.cfg -a \
        "`for lang in $OOO_LANGS_LIST ; do echo $lang ; break ; done`" = "en-US" ; then

    echo "Removing duplicated English help..."
  
    for lang in $OOO_LANGS_LIST ; do
	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
	    remove_help_localization $lang
	fi
    done
    
    echo "Removing poor help localizations..."

    for lang in $OOO_POOR_HELP_LOCALIZATIONS ; do
	remove_help_localization $lang
    done
fi

if test -d $OOINSTDIR/sdk ; then
    # bin potential .orig files
    find $OOINSTDIR/sdk -name "*.orig" -exec rm -f {} \;

    # move some SDK directories to the right place according to FHS
    # note that examples must stay in $OOINSTDIR/sdk because there are used
    # relative paths to $OOINSTDIR/sdk/setting and it does not work via
    # a symlink
    mkdir -p $PREFIX/include
    mkdir -p $DATADIR/idl
    mkdir -p $DATADIR/xml
    mkdir -p $DATADIR/$OOOINSTALLDIRNAME/sdk
    mkdir -p $DOCDIR/sdk
    mv $OOINSTDIR/sdk/include      $PREFIX/include/$OOOINSTALLDIRNAME
    if [ -d $OOINSTDIR/sdk/classes ]; then
        mv $OOINSTDIR/sdk/classes      $DATADIR/$OOOINSTALLDIRNAME/sdk/classes
    fi
    mv $OOINSTDIR/sdk/idl          $DATADIR/idl/$OOOINSTALLDIRNAME
    mv $OOINSTDIR/sdk/xml          $DATADIR/xml/$OOOINSTALLDIRNAME
    mv $OOINSTDIR/sdk/docs         $DOCDIR/sdk
    mv $OOINSTDIR/sdk/share/readme $DOCDIR/sdk
    mv $OOINSTDIR/sdk/LICENSE*     $DOCDIR/sdk
    mv $OOINSTDIR/sdk/README*      $DOCDIR/sdk
    mv $OOINSTDIR/sdk/index.html   $DOCDIR/sdk

    # bin empty directories
    rmdir $OOINSTDIR/sdk/share/

    # compat symlinks
    ln -sf $PREFIXBASE/include/$OOOINSTALLDIRNAME      $OOINSTDIR/sdk/include
    ln -sf $DATADIRBASE/$OOOINSTALLDIRNAME/sdk/classes $OOINSTDIR/sdk/classes
    ln -sf $DATADIRBASE/idl/$OOOINSTALLDIRNAME         $OOINSTDIR/sdk/idl
    ln -sf $DATADIRBASE/xml/$OOOINSTALLDIRNAME         $OOINSTDIR/sdk/xml
    ln -sf $DOCDIRBASE/sdk/docs                        $OOINSTDIR/sdk/
    ln -sf $DOCDIRBASE/sdk/index.html                  $OOINSTDIR/sdk/index.html
    ln -sf $OOINSTBASE/sdk/examples                    $DOCDIR/sdk/examples

    # fix file list
    sed -e "s|^\(%dir \)\?$OOINSTBASE/sdk/include|\1$PREFIXBASE/include/$OOOINSTALLDIRNAME|" \
	-e "s|^\(%dir \)\?$OOINSTBASE/sdk/classes|\1$DATADIRBASE/$OOOINSTALLDIRNAME/sdk/classes|" \
	-e "s|^\(%dir \)\?$OOINSTBASE/sdk/idl|\1$DATADIRBASE/idl/$OOOINSTALLDIRNAME|" \
	-e "s|^\(%dir \)\?$OOINSTBASE/sdk/xml|\1$DATADIRBASE/xml/$OOOINSTALLDIRNAME|" \
	-e "s|^\(%dir \)\?$OOINSTBASE/sdk/docs|\1$DOCDIRBASE/sdk/docs|" \
	-e "s|^\(%dir \)\?$OOINSTBASE/sdk/share/readme|\1$DOCDIRBASE/sdk/readme|" \
	-e "s|^$OOINSTBASE/sdk/LICENSE\(.*\)$|$DOCDIRBASE/sdk/LICENSE\1|" \
	-e "s|^$OOINSTBASE/sdk/README\(.*\)$|$DOCDIRBASE/sdk/README\1|" \
	-e "s|^$OOINSTBASE/sdk/index.html$|$DOCDIRBASE/sdk/index.html|" \
	-e "s|^\(%dir \)\?$OOINSTBASE/sdk/share.*$||" \
	-e "/\.orig$/D" \
	-e "/^$/D" \
	$OODESTDIR/gid_Module_Root_SDK \
	>$OODESTDIR/gid_Module_Root_SDK.new
    mv $OODESTDIR/gid_Module_Root_SDK.new $OODESTDIR/gid_Module_Root_SDK
    #
    echo "%dir $DATADIRBASE/$OOOINSTALLDIRNAME/sdk"    >>$OODESTDIR/gid_Module_Root_SDK
    echo "%dir $DATADIRBASE/$OOOINSTALLDIRNAME"        >>$OODESTDIR/gid_Module_Root_SDK
    echo "%dir $DATADIRBASE/idl"                       >>$OODESTDIR/gid_Module_Root_SDK
    echo "%dir $DATADIRBASE/xml"                       >>$OODESTDIR/gid_Module_Root_SDK
    echo "%dir $DOCDIRBASE/sdk/docs"                   >>$OODESTDIR/gid_Module_Root_SDK
    echo "%dir $DOCDIRBASE/sdk"                        >>$OODESTDIR/gid_Module_Root_SDK
    echo "%dir $DOCDIRBASE"                            >>$OODESTDIR/gid_Module_Root_SDK
    echo "$OOINSTBASE/sdk/include"     >>$OODESTDIR/gid_Module_Root_SDK
    echo "$OOINSTBASE/sdk/classes"     >>$OODESTDIR/gid_Module_Root_SDK
    echo "$OOINSTBASE/sdk/idl"         >>$OODESTDIR/gid_Module_Root_SDK
    echo "$OOINSTBASE/sdk/xml"         >>$OODESTDIR/gid_Module_Root_SDK
    echo "$OOINSTBASE/sdk/docs"        >>$OODESTDIR/gid_Module_Root_SDK
    echo "$OOINSTBASE/sdk/index.html"  >>$OODESTDIR/gid_Module_Root_SDK
    echo "$DOCDIRBASE/sdk/examples"    >>$OODESTDIR/gid_Module_Root_SDK

    # generate default profiles
    for file in setsdkenv_unix.csh setsdkenv_unix.sh ; do
        sed -e "s,@OO_SDK_NAME@,OpenOffice.org2.0_SDK," \
	    -e "s,@OO_SDK_HOME@,$OOINSTBASE/sdk," \
	    -e "s,@OFFICE_HOME@,$OOINSTBASE," \
	    -e "s,@OO_SDK_URE_HOME@,," \
	    -e "s,@OO_SDK_MAKE_HOME@,/usr/bin," \
	    -e "s,@OO_SDK_ZIP_HOME@,/usr/bin," \
	    -e "s,@OO_SDK_CPP_HOME@,/usr/bin," \
	    -e "s,@OO_SDK_CC_55_OR_HIGHER@,," \
	    -e "s,@OO_SDK_JAVA_HOME@,$JAVA_HOME," \
	    -e "s,@OO_SDK_OUTPUT_DIR@,\$HOME," \
	    -e "s,@SDK_AUTO_DEPLOYMENT@,NO," \
            $OOINSTDIR/sdk/$file.in \
	    > $OOINSTDIR/sdk/$file
	chmod 755 $OOINSTDIR/sdk/$file
	echo $OOINSTBASE/sdk/$file >>$OODESTDIR/gid_Module_Root_SDK
    done
    
    # FIXME: I rather set this file to be non-world-writttable for now, i#64812
    chmod go-w $OOINSTDIR/sdk/settings/component.uno.map
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_[0-9]"
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
	%dir $OOINSTBASE/share/template/en-US/forms
	$OOINSTBASE/share/template/en-US/forms/resume.ott
	%dir $OOINSTBASE/share/template/en-US/officorr
	$OOINSTBASE/share/template/en-US/officorr/project-proposal.ott" > 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_Optional_Pymailmerge \
		gid_Module_Hack \
		$BUILDDIR/dictionaries \
		| sort | uniq \
		> $BUILDDIR/common_list.txt

	if test "$VENDORNAME" = "Novell" ; then
		cat $BUILDDIR/novell-gallery-addon >> $BUILDDIR/common_list.txt
	fi
	
	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

	if test -f gid_Module_Root_SDK ; then
	    cp gid_Module_Root_SDK $BUILDDIR/sdk_list.txt
	fi
	
	# 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
	test -f $OODESTDIR/gid_Module_Optional_Kde && cp $OODESTDIR/gid_Module_Optional_Kde 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/libkabdrv1.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

	# NLD subpackage
	rm -f nld_list.txt
	mv_file_between_flists nld_list.txt common_list.txt $OOINSTBASE/program/openintro_nld.bmp
	mv_file_between_flists nld_list.txt common_list.txt $OOINSTBASE/program/openabout_nld.bmp
	add_used_directories nld_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

	if test -f sdk_list.txt ; then
		rm -f sdk_doc_list.txt
		# in this case we move all entries including directories
		mv_file_between_flists sdk_doc_list.txt sdk_list.txt "%dir $DOCDIRBASE/sdk/docs.*"
		mv_file_between_flists sdk_doc_list.txt sdk_list.txt "$DOCDIRBASE/sdk/docs.*"
		mv_file_between_flists sdk_doc_list.txt sdk_list.txt "$DOCDIRBASE/sdk/examples"
		mv_file_between_flists sdk_doc_list.txt sdk_list.txt "$DOCDIRBASE/sdk/index.html"
		mv_file_between_flists sdk_doc_list.txt sdk_list.txt "%dir $OOINSTBASE/sdk/examples.*"
		mv_file_between_flists sdk_doc_list.txt sdk_list.txt "$OOINSTBASE/sdk/docs"
		mv_file_between_flists sdk_doc_list.txt sdk_list.txt "$OOINSTBASE/sdk/examples.*"
		mv_file_between_flists sdk_doc_list.txt sdk_list.txt "$OOINSTBASE/sdk/index.html"
		add_used_directories sdk_doc_list.txt sdk_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_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_Optional_Grfflt             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_Pymailmerge	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            

	create_package_directory gid_Module_Root_SDK                    pkg/openoffice.org-dev

	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; \
		create_package_directory gid_Module_Langpack_Help.$l	pkg/openoffice.org-help-$l; \
	done

	# move_wrappers <directory_name> <name> [...]
	move_wrappers()
	{
		directory=$1
		shift
		mkdir -m755 -p "$directory"/usr/bin
		if test "$ENABLE_QUICKSTART" = ""; then
			mkdir -m755 -p "$directory"/usr/share/man/man1
		fi
		while test -n "$1"; do
			mv usr/*bin/"$1$BINSUFFIX" "$directory"/usr/bin
			if test "$ENABLE_QUICKSTART" = ""; then
				mv usr/share/man/man1/"$1$BINSUFFIX.1" "$directory"/usr/share/man/man1
			fi
			shift
		done
	}
	move_wrappers pkg/openoffice.org-common ooffice oofromtemplate soffice
	move_wrappers pkg/openoffice.org-base oobase
	move_wrappers pkg/openoffice.org-writer oowriter 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
	if test "$ENABLE_QUICKSTART" = ""; then
		for F in bin/ooo-wrapper; do
			mv usr/$F$BINSUFFIX pkg/openoffice.org-common/usr/bin
		done
	fi

	# 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 -maxdepth 1 -type f \
	       -regex '\./\(.*\.so.*\|.*\.bin\|pagein\|nsplugin\|kdefilepicker\|msfontextract\|.*\.rdb\|javaldx\|uri-encode\)' \
		   -exec mv {} $OODESTDIR/pkg/openoffice.org-core/$OOINSTBASE/program \;
	)

	# install additional ooo-build scripts & misc stuff
	mkdir -p pkg/openoffice.org-common/usr/share/man/man1
	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
	if test "$ENABLE_QUICKSTART" = ""; then
		mv etc/bash_completion.d/ooo-wrapper$BINSUFFIX.sh \
			pkg/openoffice.org-common/etc/bash_completion.d
	else
		mv etc/bash_completion.d/ooffice$BINSUFFIX.sh \
			pkg/openoffice.org-common/etc/bash_completion.d
	fi
	mv .$OOINSTBASE/program/java-set-classpath \
		pkg/openoffice.org-common/$OOINSTBASE/program
	if [ -e .$OOINSTBASE/program/kdebe1.uno.so ]; then \
		mv .$OOINSTBASE/program/kdebe*.uno.so \
			pkg/openoffice.org-core/$OOINSTBASE/program; \
	fi
	if echo $OOO_LANGS_LIST | grep -q en-US; then
		for i in forms/resume.ott officorr/project-proposal.ott; do \
			mkdir -p pkg/openoffice.org-common/$OOINSTBASE/share/template/en-US/`dirname $i`; \
			mv .$OOINSTBASE/share/template/en-US/$i \
				pkg/openoffice.org-common/$OOINSTBASE/share/template/en-US/$i; \
		done; \
	fi
	# 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" $OOINSTBASE/program/instdb.ins
	perl -pi -e "/^Installation gid_Installation/ .. /^End/ and s|$PREFIX||g" $OOINSTBASE/program/instdb.ins
	perl -pi -e "/^Directory gid_Dir_Home_Gnome_Apps_Product/ .. /^End/ and s|OpenOffice\.org\ 1\.1\.0|OpenOffice\.org|g" $OOINSTBASE/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" $OOINSTBASE/program/instdb.ins
	perl -pi -e "/^Directory gid_Dir_Kde_Share_Applnk_Product/ .. /^End/ and s|OpenOffice\.org\ 1\.1\.0|OpenOffice\.org|g" $OOINSTBASE/program/instdb.ins
	perl -pi -e "/^Procedure gid_Procedure_Kde_Inst/ .. /^End/ and s|OpenOffice\.org\ 1\.1\.0|OpenOffice\.org|g" $OOINSTBASE/program/instdb.ins
	perl -pi -e "/^Procedure gid_Procedure_Gnome_Install/ .. /^End/ and s|OpenOffice\.org\ 1\.1\.0|OpenOffice\.org|g" $OOINSTBASE/program/instdb.ins
	perl -pi -e "/^Procedure gid_Procedure_Gnome_Deinstall/ .. /^End/ and s|1\.1\.0||g" $OOINSTBASE/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)" \
			$OOINSTBASE/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;

