#!/bin/sh

# This script creates a 'develop' directory that can be used from Haiku

set -x

#
# 1. Check command line arguments
#

if [ $# \< 2 ]; then
	echo Usage: $0 '<haiku trunkdir> <buildtools dir> [ <haiku output dir> ]' >&2
	exit 1
fi

#
# 2. Set up environment
#

haikuSourceDir=$1
buildToolsDir=$2/legacy

if [ $# \< 3 ]; then
	haikuOutputDir=$haikuSourceDir/generated
else
	haikuOutputDir=$3
fi

if [ ! -d $haikuSourceDir ]; then
	echo "No such directory: \"$haikuSourceDir\"" >&2
	exit 1
fi

if [ ! -d $buildToolsDir ]; then
	echo "No such directory: \"$buildToolsDir\"" >&2
	exit 1
fi

# create the output dir
mkdir -p $haikuOutputDir || exit 1

# get absolute paths
currentDir=$(pwd)

cd $haikuSourceDir
haikuSourceDir=$(pwd)
cd $currentDir

cd $buildToolsDir
buildToolsDir=$(pwd)
cd $currentDir

cd $haikuOutputDir
haikuOutputDir=$(pwd)

# create the object and installation directories for the cross compilation tools
installDir=$haikuOutputDir/developer-tools
objDir=$haikuOutputDir/developer-tools-build
binutilsObjDir=$objDir/binutils
gccObjDir=$objDir/gcc

rm -rf $installDir $objDir
mkdir -p $installDir $objDir $binutilsObjDir $gccObjDir $tmpIncludeDir \
	$tmpLibDir || exit 1

#
# 3. Copy necessities
#

cd $haikuOutputDir/objects/haiku/x86/release/system/glue/arch/x86
cp crti.o crtn.o ../../../../../../../../cross-tools/i586-pc-beos/lib
cd $currentDir

# Copy init_term_dyn.o and start_dyn.o
cd $haikuOutputDir/objects/haiku/x86/release/system/glue
cp init_term_dyn.o start_dyn.o ../../../../../../cross-tools/i586-pc-beos/lib
cd $currentDir

# Copy system libs
cd $haikuOutputDir/objects/haiku/x86/release
cp system/libroot/libroot.so ../../../../cross-tools/lib/gcc-lib/i586-pc-beos/2.95.3-beos-060710
cd $currentDir

# Copy Haiku POSIX headers and their dependencies
copy_headers()
{
	sourceDir=$1
	targetDir=$2

	headers="$(find $sourceDir -name \*\.h | grep -v /.svn)"
	headers="$(echo $headers | sed -e s@$sourceDir/@@g)"
	for f in $headers; do
		headerTargetDir=$targetDir/$(dirname $f)
		mkdir -p $headerTargetDir
		cp $sourceDir/$f $headerTargetDir
	done
}

copy_headers $haikuSourceDir/headers/os $haikuOutputDir/cross-tools/i586-pc-beos/sys-include/be
copy_headers $haikuSourceDir/headers/posix $haikuOutputDir/cross-tools/i586-pc-beos/sys-include/posix

#
# 4. Build binutils
#
cd $binutilsObjDir

PATH=$haikuOutputDir/cross-tools/bin:$PATH 

$buildToolsDir/binutils/configure --prefix=$installDir --host=i586-pc-beos --build=i486-linux-gnu --target=i586-pc-beos --disable-nls --enable-shared=yes --disable-werror || exit 1

make || exit 1

make install || exit 1

#
# 5. Build gcc
# 

# configure gcc
cd $gccObjDir
CFLAGS="-O2" CXXFLAGS="-O2" $buildToolsDir/gcc/configure --prefix=$installDir \
	--host=i586-pc-beos --build=i486-linux-gnu --target=i586-pc-beos --disable-nls \
	--enable-shared=yes --enable-languages=c,c++ || exit 1

# hack the Makefile to avoid trouble with stuff we don't need anyway
sedExpr=
for toRemove in libiberty libio libjava libobjc libstdc++; do
	sedExpr="$sedExpr -e 's@^\(TARGET_CONFIGDIRS =.*\)$toRemove\(.*\)@\1\2@'"
done
echo sedExpr: $sedExpr
mv Makefile Makefile.bak || exit 1
eval "sed $sedExpr Makefile.bak > Makefile" || exit 1
rm Makefile.bak

# make gcc
make || {
	echo "ERROR: Building gcc failed." >&2
	exit 1
}

# install gcc
make install-gcc-cross || {
	echo "ERROR: Installing the cross compiler failed." >&2
	exit 1
}

#
# 6. Copy headers into the developer-tools
#

copy_headers $haikuSourceDir/headers/os $installDir/i586-pc-beos/sys-include/be
copy_headers $haikuSourceDir/headers/posix $installDir/i586-pc-beos/sys-include/posix
