#!/bin/bash

cd $SOURCE_DIR
if [ ! -f bash-$BASH_VER.tar.gz ]; then
  wget http://ftp.gnu.org/gnu/bash/bash-$BASH_VER.tar.gz
fi

mkdir -p $SOURCE_DIR/patches/bash-$BASH_VER
cd $SOURCE_DIR/patches/bash-$BASH_VER
if [ $BASH_PATCHLEVEL -gt 000 ]; then
  for p in $(seq -w 001 $BASH_PATCHLEVEL); do
    if [ ! -f bash${BASH_VER/.}-$p ]; then
      wget http://ftp.gnu.org/gnu/bash/bash-$BASH_VER-patches/bash${BASH_VER/.}-$p
    fi
  done
fi

cd $BUILD_DIR
rm -rf bash-$BASH_VER
tar -xf $SOURCE_DIR/bash-$BASH_VER.tar.gz
cd bash-$BASH_VER
if [ $BASH_PATCHLEVEL -gt 000 ]; then
  for p in $(seq -w 001 $BASH_PATCHLEVEL); do
    patch -p0 < $SOURCE_DIR/patches/bash-$BASH_VER/bash${BASH_VER/.}-$p
  done
fi

cd $BUILD_DIR
rm -rf bash-build
mkdir -p bash-build
cd bash-build

../bash-$BASH_VER/configure \
  --host=$TARGET \
  --prefix=
make DESTDIR=$HURD_DIR install

cd $HURD_DIR/bin
ln -s bash sh

cd $ROOT
