#!/bin/bash

PATCHES=$(pwd)/patches/
BASELINE="u-boot-v2013.01.01.tgz"
DEST="$(pwd)/u-boot-acds13.1"

echo "un-tarring ${BASELINE} into ${DEST}"
mkdir ${DEST}
if [ $? -ne 0 ] ; then
    echo "error: could not create directory ${DEST}"
    exit -1
fi

tar xfz ${BASELINE} -C ${DEST}
if [ $? -ne 0 ] ; then
    echo "error: could run tar with ${BASELINE}"
    exit -1
fi

echo "applying patches"
pushd ${DEST} >/dev/null 2>&1
ls -1 ${PATCHES} | while read p ; do
    echo "applying patch $(basename ${p})"
    patch -p1 -s < ${PATCHES}/$p
    if [ $? -ne 0 ] ; then
        echo "error: failed to apply patch ${p}..."
        exit -1
    fi
done
popd >/dev/null 2>&1

echo "done, sources in ${DEST}"

exit 0

