#!/bin/bash

piece=$1
ooo_build_tag=$2

source $OO_TOOLSDIR/piece/sys-setup

custom_env="$OO_TOOLSDIR/piece/env-$piece"
if test -f $custom_env; then
   source $custom_env
   echo "merged custom environment: $custom_env"
fi

# number of CPUs for the parallel build
if test -n "$PIECE_BUILD_NCPUS_AUTO" ; then
    PIECE_BUILD_NCPUS=`grep ^processor /proc/cpuinfo | wc -l`
fi
# fallback to 1
test -z "$PIECE_BUILD_NCPUS" && PIECE_BUILD_NCPUS=1
test $PIECE_BUILD_NCPUS -lt 0 && PIECE_BUILD_NCPUS=1

# reduce the number of jobs according to the available memory
# caused problem in the openSUSE Build Service
if test -n "$PIECE_USE_JOBS" && test "$PIECE_USE_JOBS" -gt 1 ; then
    # define the expected maximal memory usage per process in MB
    if test `uname -i` = "x86_64" ; then
	mem_per_process=250
    else
	mem_per_process=150
    fi
    max_mem=`LANG=C free -t -m | sed -n "s|^Total: *\([0-9]*\).*$|\1|p"`
    max_jobs="$(($max_mem / $mem_per_process / $PIECE_BUILD_NCPUS))"
    test $PIECE_USE_JOBS -gt $max_jobs && PIECE_USE_JOBS=$max_jobs && echo "Warning: Reducing number of parallel jobs to $max_jobs because of memory limits"
    test $PIECE_USE_JOBS -le 0 && PIECE_USE_JOBS= && echo "Warning: Do not use the parallel build at all because of memory limits"
fi

# finally, set the build flags for the selected parallelism
if test "$PIECE_BUILD_NCPUS" -gt 1 ; then
    echo "Parallel build: using $PIECE_BUILD_NCPUS CPUs"
    EXTRA_BUILD_FLAGS="-P$PIECE_BUILD_NCPUS $EXTRA_BUILD_FLAGS"
fi
if test -n "$PIECE_USE_JOBS" ; then
    echo "Parallel build: using $PIECE_USE_JOBS jobs"
    EXTRA_BUILD_FLAGS="$EXTRA_BUILD_FLAGS -- -P$PIECE_USE_JOBS"
fi

# unpack extra sources when needed
custom_unpack="$OO_TOOLSDIR/piece/unpack-$piece"
export GNUCP=cp
if test -f $custom_unpack; then
    echo "Invoke custom unpack: $custom_unpack"
    $custom_unpack $OO_TOOLSDIR/.. `pwd`
fi

$OO_TOOLSDIR/install-artwork $OO_TOOLSDIR/../src `pwd`

# new modules
test "$piece" = "libs-core" && mkdir -p vbahelper
test "$piece" = "filters" && mkdir -p lotuswordpro

# apply flags
builddir=`pwd`
FLAGS=`$OO_TOOLSDIR/applyflags $OO_TOOLSDIR $builddir`
# explicitely add --distro=System
# it can't be detected easily
FLAGS="$FLAGS --additional-sections=System"
# additional piece-specific sections added in the package sources (special build)
additional_sections_file="$builddir/ooo-build/apply-additional-sections"
if test -f "$additional_sections_file" ; then
    for section in `sed "s|,| |g" $additional_sections_file` ; do
	FLAGS="$FLAGS --additional-sections=$section"
    done
fi
# additional piece specifix hotfixes added in the package sources (special build)
FLAGS="$FLAGS --hotfixes=$builddir/ooo-build/hotfixes"

# finally call apply.pl
$OO_TOOLSDIR/../patches/apply.pl $OO_TOOLSDIR/../patches/dev300 \
    --tag=$ooo_build_tag $FLAGS $builddir || exit 1;

# available source dirs
# FIXME: omit optional directories that are not enabled (--with-system-XXX, ...)
omit_dirs="-e applied_patches -e ^solver -e ooo-build -e javainstaller2"
if test "$piece" != "libs-extern" -a "$piece" != "libs_extern" ; then
    omit_dirs="$omit_dirs -e ^external\$"
fi
dirs=`/bin/ls | grep -v $omit_dirs | tr '\n' ':'`
# clean build?
if test ! -d solver; then
    echo "A very clean straight-through build - deferring dep generation"
    export nodep=1
fi

# apply extra localization fixes in the libs-gui piece
if test "$piece" = "libs-gui" -o "$piece" = "libs_gui" ; then
    # do not build modules depending on transex3,l10n,vcl now
    for dir in * ; do 
	test -f $dir/prj/build.lst && \
	head -n 1 $dir/prj/build.lst | grep -q -e transex3 -e l10n -e vcl && \
	omit_dirs="$omit_dirs -e ^$dir\$"
	transex3_dirs=`/bin/ls | grep -v $omit_dirs | tr '\n' ':'`
	# actually build the transex3 module
	transex3_dirs="transex3:$transex3_dirs"
    done
    # build the localize tool
    perl $SOLARENV/bin/build.pl --subdirs:$transex3_dirs $EXTRA_BUILD_FLAGS $EXTRA_DMAKE_FLAGS || exit 1;
    unset nodep
    # apply sdf files
    $OO_TOOLSDIR/localize-ooo || exit 1;
fi

# final build
perl $SOLARENV/bin/build.pl --subdirs:$dirs $EXTRA_BUILD_FLAGS $EXTRA_DMAKE_FLAGS || exit 1;
