#!/usr/bin/env bash
set -euo pipefail

bash_repo="${bash_repo:-https://git.savannah.gnu.org/git/bash.git}"
bash_tag="${bash_tag:-bash-5.3}"
stdlib="${stdlib:-musl}"

if [ "$stdlib" == "gnu" ]; then
    COMPILER="aarch64-linux-gnu-gcc"
else
    COMPILER="zig cc -target aarch64-linux-musl"
fi

pushd "build" &>/dev/null || exit 1

if [ ! -d "bash" ]; then
    git clone --depth 1 --branch "$bash_tag" "$bash_repo" "bash"
else
    pushd "bash" &>/dev/null || exit 1
    git pull
    popd &>/dev/null || exit 1
fi

pushd "bash" &>/dev/null || exit 1
./configure --without-bash-malloc --enable-static-link --host="aarch64-linux-$stdlib" CC="$COMPILER"
make
mv bash ../bin/bash
popd &>/dev/null || exit 1

popd &>/dev/null || exit 1
