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

wrs_echo "--- U-Boot"
zipname="u-boot-1.3.4.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
cd $WRS_OUTPUT_DIR/build
dirname="u-boot-1.3.4"
rm -rf $dirname
tar xjf ${WRS_DOWNLOAD_DIR}/$zipname || wrs_die "untar $zipname"

wrs_echo "Patching U-Boot"
patch -t -p2 -d $dirname < \
    "$WRS_BASE_DIR/../patches/u-boot/u-boot-1.3.4-wr-patch" \
    || wrs_die "patching u-boot"

wrs_echo "Building U-Boot"
cd $dirname
make whiterabbit_mch || wrs_die "configure u-boot"
make -s $WRS_MAKE_J || wrs_die "compile u-boot"
cp u-boot u-boot.bin $WRS_OUTPUT_DIR/images \
    || wrs_die "copying u-boot image"

exit 0