#!/bin/bash

# check variables, like all scripts herein do
WRS_SCRIPT_NAME=$(basename $0)
if [ -z "$WRS_BASE_DIR" ]; then
    echo "$0: Plesae set WRS_BASE_DIR" >& 2
    exit 1
fi
. ${WRS_BASE_DIR}/scripts/wrs_functions

wrs_check_vars WRS_OUTPUT_DIR WRS_DOWNLOAD_DIR

wrs_echo "--- Buildroot compiler and filesystem"
zipname="buildroot-2011.11.tar.bz2"
wrs_download $zipname

mkdir -p $WRS_OUTPUT_DIR/build || wrs_die "mkdir build"
mkdir -p $WRS_OUTPUT_DIR/images || wrs_die "mkdir images"

# go to the build dir and compile it, using our configuration
cd $WRS_OUTPUT_DIR/build
dirname="buildroot-2011.11"
wrs_echo "Uncompressing buildroot"
rm -rf $dirname
tar xjf ${WRS_DOWNLOAD_DIR}/$zipname || wrs_die "untar $zipname"

# select configuration: old default, overridden by Kconfig and environment
CFG_DEFAULT="$WRS_BASE_DIR/../configs/buildroot/wrs_release_br2_config"
CFG="$CONFIG_BR2_CONFIGFILE"
if [ "x$WRS_BUILDROOT_CONFIG" != "x" ]; then
	CFG=$WRS_BUILDROOT_CONFIG
fi

# turn a relative CFG pathname into an absolute name
if echo $CFG | grep -q '^/'; then
    true
else
    CFG="$WRS_BASE_DIR/../configs/buildroot/$CFG"
fi

# check, and revert to default if needed
if [ ! -f "$CFG" ]; then
    wrs_warn "Ignoring \"$CFG\" (not a regular file)"
    CFG="$CFG_DEFAULT"
fi
wrs_echo "Configuring with \"$CFG\""

cd $WRS_OUTPUT_DIR/build/${dirname}
cp $CFG .config
# If busybox configuration is inside wr-switch-sw (currently it is),
# fix the config filename
sed -i "s,__WR_SWITCH_SW__,$WRS_BASE_DIR/../configs/buildroot," .config

# replace "-j" level. First remove it in case it's left in
sed -i /^BR2_JLEVEL/d .config
# make 3.82 splits the work differently, and compilation fails. So no -j
makefails=$(make -v | awk 'NR==1 {if ($NF > 3.81) print "fail"}')
if [ "x$makefails" = "xfail" ]; then
    echo "BR2_JLEVEL=1" >> .config
    # Actually, it seems it fails anyways. I give up and document the issue
else
    echo "BR2_JLEVEL=$WRS_MAKE_J_NUMBER" >> .config
fi

# apply any pending patches
wrs_echo "Patching buildroot"
cd $WRS_OUTPUT_DIR/build/${dirname}
for n in $WRS_BASE_DIR/../patches/buildroot/*patch; do
    # the shell passes the "*" over if no matches are there, so check
    if test -f "$n"; then
	patch -p1 < $n || wrs_die "Apply $n"
    fi
done

# now, if CROSS_COMPILE is already set and is not ours, change the config
if [ "x$CROSS_COMPILE" != "x" ]; then
	wrs_echo "Checking CROSS_COMPILE"
	${CROSS_COMPILE}gcc -v 2>&1 | grep arm &> /dev/null
	if [ $? -eq "0" ]; then
		wrs_echo "Changing config for external compiler ($CROSS_COMPILE)"
		wrs_unset_config BR2_TOOLCHAIN_BUILDROOT
		wrs_unset_config BR2_TOOLCHAIN_SOURCE
		wrs_set_config BR2_TOOLCHAIN_EXTERNAL
		
		echo BR2_TOOLCHAIN_EXTERNAL_PATH=\"$(dirname $(dirname $CROSS_COMPILE))\" \
		>> .config
		echo BR2_TOOLCHAIN_EXTERNAL_PREFIX="\"\$(ARCH)-linux\"" >> .config
		echo '# BR2_TOOLCHAIN_EXTERNAL_UCLIBC is not set' >> .config
		echo BR2_TOOLCHAIN_EXTERNAL_GLIBC=y >> .config
		echo BR2_TOOLCHAIN_EXTERNAL_STRIP=y >> .config
	fi
fi

# re-digest the config we built
wrs_echo "Reconfiguring buildroot"
make oldconfig || wrs_die "buildroot config"

buildrootdir=$(/bin/pwd)

cd $buildrootdir

# We don't want CC to be pre-set at this point (some of us do :)
unset CC
wrs_echo "Compiling buildroot"
# tell to buildroot to use our download directory
make BUILDROOT_DL_DIR=$WRS_DOWNLOAD_DIR || wrs_die "buildroot compilation"
