#!/bin/bash
# Reconstruct the port without depending on a developer's extracted source tree.
set -euo pipefail
cd -- "$(dirname -- "$0")"
if [[ -e bash-5.3 ]]; then
    echo 'bash-5.3 already exists; refusing to overwrite source edits' >&2
    exit 1
fi
mkdir -p upstream
while read -r digest filename; do
    if [[ ! -f $filename ]]; then
        case "$filename" in
            upstream/bash-5.3.tar.gz) url=https://ftp.gnu.org/gnu/bash/bash-5.3.tar.gz ;;
            upstream/bash53-*) url=https://ftp.gnu.org/gnu/bash/bash-5.3-patches/${filename##*/} ;;
            *) echo "Unexpected manifest path: $filename" >&2; exit 1 ;;
        esac
        curl --fail --location --proto '=https' --tlsv1.2 "$url" -o "$filename"
    fi
done < upstream.sha256
sha256sum --check upstream.sha256
tar -xzf upstream/bash-5.3.tar.gz
for number in {001..015}; do
    patch --batch --forward -d bash-5.3 -p0 < "upstream/bash53-$number"
done
patch --batch --forward -d bash-5.3 -p1 < patches/0001-carrier-cli-on-bash53.patch
