#!/bin/bash

# stuff from configure
export DISTRO='SUSE-11.1'
export VENDORNAME='Novell'
export VERSION='3.2'
export OO_INSTDIR=/usr/lib64/ooo3
export OO_PREFIX=/usr
export OO_MANDIR=/usr/share/man
export OO_DOCDIR=/usr/share/doc/packages/OpenOffice_org-bootstrap
export OO_LANGS='en-US af ar be-BY bg br ca cs cy da de el en-GB en-ZA es et fi fr ga gl gu he hi-IN hu hr it ja ka km ko lt mk nb nl nn nr pa-IN pl pt pt-BR ru rw sh sk sl sr ss st sv tg th tr ts uk ve vi xh zh-TW zh-CN zu'
export OOO_BUILD_NOARCH='YES'
# do not lost ARCH_FLAGS from the environmnet; potential duplicates should not harm
export ARCH_FLAGS="$ARCH_FLAGS -fmessage-length=0  -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables -g -fno-strict-aliasing"

if test "z$OO_INSTDIR" == "z"; then
    echo "Error: missing oo instdir"
    exit 1
fi
# a saneish default directory for the solver
if test "z$OO_SOLVERDIR" == "z"; then
    export OO_SOLVERDIR=$OO_INSTDIR/solver
fi

# should be used only togehter with $OOO_BUILD_NOARCH = "YES"
# to install the stuff in /usr/share rather than into /usr/lib(64)
export OO_INSTDIR_SHARE=`echo $OO_INSTDIR | sed -e "s|lib6\?4\?|share|"`

# installed system libraries
URE_DIR=$OO_INSTDIR/ure
BASIS_DIR=$OO_INSTDIR/basis$VERSION
INSTALL_DIR=$BASIS_DIR/program
DEV_DIR=$OO_SOLVERDIR

# dmake can't cope with '_' in env. var names (interestingly)
export SPLITUREDIR=$URE_DIR
export SPLITBASISDIR=$BASIS_DIR
export SPLITINSTALLDIR=$INSTALL_DIR
export SPLITDEVDIR=$DEV_DIR

PRESERVE_PATH=$PATH
PATH=

# include all the main configured settings by default
. $DEV_DIR/*.[sS]et.sh

# override / extend ones we need to adapt
SRC_ROOT=`pwd`

#  new local solver
export SOLARPIECEVERSION="$SRC_ROOT/solver"
export SOLARPIECE="$SOLARPIECEVERSION"
# FIXME: we should this variable in the OOo sources
export SOLARSRC="$SRC_ROOT"

# system solver bits ...
export SOLARVER="$DEV_DIR"
export SOLARENV="$DEV_DIR/../solenv"
export MAKEDEPEND="$DEV_DIR/bin/makedepend"
export SOLARVERSION="$SOLARVER"
export SOLARENVINC="$SOLARENV/inc"
export LOCALINI="$SOLARENV/config"
export STAR_INIROOT="$SOLARENV/config"
export STAR_INIROOTOLD="$SOLARENV/config"
export STAR_STANDLST="$SOLARENV/config/stand.lst"
export STAR_SSCOMMON="$SOLARENV/config/ssolar.cmn"
export STAR_SSOLARINI="$SOLARENV/config/ssolar.ini"
export DMAKEROOT="$SOLARENVINC/startup"
export UNOINCLUDES="-I$DEV_DIR/inc/offuh -I$SOLARPIECE/$INPATH/inc/offuh"

# FIXME: the older LD_LIBRARY_PATH is necessary to build bean module; it adds the Java paths detected by config_office/configure
export LD_LIBRARY_PATH=".:$DEV_DIR/lib:$URE_DIR/lib:$INSTALL_DIR:$SOLARPIECE/$INPATH/lib:$SOLARENV/$INPATH/lib:$JAVA_HOME/jre/lib/i386:$JAVA_HOME/jre/lib/i386/xawt:$JAVA_HOME/jre/lib/i386/client:$JAVA_HOME/jre/lib/i386/native_threads:../lib:$SOLARVER/lib:/opt/OpenOffice/HEAD/build/lib:/opt/mozilla/lib:$LD_LIBRARY_PATH"
export PATH="$SOLARPIECE/$INPATH/bin:$URE_DIR/bin:$JAVA_HOME/bin:.:$SOLARVER/bin:$SOLARENV/$OUTPATH/bin:$SOLARENV/bin:$PRESERVE_PATH"
SOLARINC=" -I. -I$SOLARVER/inc/stl -I$SOLARVER/inc/external -I$SOLARVER/inc -I$SOLARENV/$OUTPATH/inc -I$DEV_DIR/inc -I$SOLARENV/inc -I$SRC_ROOT/res -I$SOLARENV/inc/Xp31"
SOLARINC="$SOLARINC -I$SOLARPIECE/$INPATH/inc/stl -I$SOLARPIECE/$INPATH/inc/external -I$SOLARPIECE/$INPATH/inc -I$SOLARPIECE/$OUTPATH/inc"
SOLARINC="$SOLARINC -I$JAVA_HOME/include -I$JAVA_HOME/include/linux -I$JAVA_HOME/include/native_threads/include -I/usr/include"

SOLARLIB_SYSTEM="$SOLARLIB" # FIXME: this is necessary to build bean module; it adds the Java paths detected by config_office/configure
SOLARLIB=" -L../lib -L$SOLARPIECE/$INPATH/lib -L$SOLARENV/$OUTPATH/lib -L$SOLARENV/$OUTPATH/lib"
SOLARLIB="$SOLARLIB -L$URE_DIR/lib -L$INSTALL_DIR "
# the system solver (from devel packages) must be searched after the normal installation directory because it includes static libraries that we do not want
# it must be before other system libraries to prefer the internal versions of the libraries if they are enabled and built
SOLARLIB="$SOLARLIB -L$SOLARVER/lib"
SOLARLIB="$SOLARLIB -L$JAVA_HOME/lib -L$JAVA_HOME/jre/lib/i386 -L$JAVA_HOME/jre/lib/i386/client -L$JAVA_HOME/jre/lib/i386/native_threads -L/usr/lib -L/usr/lib/xulrunner-1.9"
SOLARLIB="$SOLARLIB $SOLARLIB_SYSTEM"
export SOLARLIB SOLARINC
export SOLARIDLINC="-I$SOLARVERSION/$INPATH/idl$EXT_UPDMINOR -I$SOLARPIECE/$INPATH/idl$EXT_UPDMINOR"

# aliases
alias mkout="perl $SOLARENV/bin/mkout.pl"
alias deliver="perl $SOLARENV/bin/deliver.pl"
alias build="perl $SOLARENV/bin/build.pl"
alias build_client="perl $SOLARENV/bin/build_client.pl"
alias zipdep="perl $SOLARENV/bin/zipdep.pl"
alias nmake="dmake"

# force langs
if test -n "$FORCE_OOO_LANGS" ; then
    OO_LANGS="$FORCE_OOO_LANGS"
    WITH_LANG="$FORCE_OOO_LANGS"
fi

# some useful funtions

# remove installed file even from the file list
# Params: file_list file_to_remove
remove_file()
{
    rm -f "$DESTDIR/$2"
    perl -pi -e "s|^$2$||" "$1"
}

# remove installed directory even from the file list
# Params: file_list dir_to_remove
remove_dir()
{
    echo rm -rf "$DESTDIR/$2"
    rm -rf "$DESTDIR/$2"
    perl -pi -e "s|^(%dir\s*)?$2.*$||" "$1"
}

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