#! /bin/sh

set -ex

# Keep cd from picking a same-named directory out of CDPATH.
# Assigned first, as some shells fail to unset an unset variable.
CDPATH=
unset CDPATH

# Keep git from being pointed at another repository, as happens in hooks.
# They are assigned first for the same reason as CDPATH.
GIT_DIR= GIT_WORK_TREE= GIT_COMMON_DIR= GIT_INDEX_FILE= GIT_OBJECT_DIRECTORY=
unset GIT_DIR GIT_WORK_TREE GIT_COMMON_DIR GIT_INDEX_FILE GIT_OBJECT_DIRECTORY

srcdir="$(dirname "$0")"
test -z "$srcdir" && srcdir=.

: "${AUTORECONF:=autoreconf}"
: "${AUTOMAKE:=automake}"

cd "$srcdir"

AUXFILES="config.guess config.sub install-sh"

clone_repo_commit() {
    if test -d "$2/.git"; then
        ( cd "$2" && git reset --hard )
        ( cd "$2" && git clean -f -d )
        if ! ( cd "$2" && git checkout -q $3 ); then
            rm -rf "$2"
        fi
    else
        if test -d "$2"; then
            set +x
            echo "error: '$2' is not a Git repository" 1>&2
            exit 1
        fi
    fi
    if ! test -d "$2"; then
        git clone $1 "$2"
        if ! ( cd "$2" && git checkout -q $3 ); then
            rm -rf "$2"
            exit 1
        fi
    fi
}

if ! test -f version; then
    clone_repo_commit \
        https://github.com/osdev0/freestanding-c-hdrs.git \
        freestanding-c-hdrs \
        38fed4e1e3365733ddbfa03b0a28936243ad31e9

    clone_repo_commit \
        https://github.com/osdev0/cc-runtime.git \
        cc-runtime \
        9891629daf3703d0ddecd295772df7da82ab25b9
    cp cc-runtime/src/cc-runtime.c common/cc-runtime.s2.c

    clone_repo_commit \
        https://github.com/iczelia/pdgzip.git \
        pdgzip \
        010197664688f071b923615ea235ecb24963b61a
    cp pdgzip/pdgzip.c common/compress/pdgzip.c
    cp pdgzip/pdgzip.h common/compress/pdgzip.h

    clone_repo_commit \
        https://github.com/Limine-Bootloader/limine-protocol.git \
        limine-protocol \
        3a0526b700e356f0eac1b71a77697b3fd1c707a3

    clone_repo_commit \
        https://github.com/PicoEFI/PicoEFI.git \
        picoefi \
        470ec8325a1e05697c3af6248e2423a92bb820b0

    clone_repo_commit \
        https://github.com/Mintsuki/Flanterm.git \
        flanterm \
        3e311fe2ce4f663273c22d1a229745bc53afa930

    clone_repo_commit \
        https://github.com/Mintsuki/stbi-hardened.git \
        stbi-hardened \
        b85b7e87f6f87300f32f7de3d94e1ed083a71263
    cp stbi-hardened/include/stb_image.h common/lib/stb_image.h
    patch -p0 < common/stb_image.patch
    rm -f common/lib/stb_image.h.orig

    clone_repo_commit \
        https://github.com/osdev0/libfdt.git \
        libfdt \
        7bf94e6347129d17eca263112296ad170dec28a9
fi

# Create timestamps file
if test -d .git && git log -1 >/dev/null 2>&1; then
    cat >timestamps <<EOF
REGEN_DATE="$(LC_ALL=C git -c log.showSignature=false log -1 --pretty=%cd --date='format:%B %Y')"
SOURCE_DATE_EPOCH="$(git -c log.showSignature=false log -1 --pretty=%ct)"
SOURCE_DATE_EPOCH_TOUCH="$(TZ=UTC0 git -c log.showSignature=false log -1 --pretty=%cd --date='format-local:%Y%m%d%H%M')"
EOF
else
    if ! test -f timestamps; then
        cat >timestamps <<EOF
REGEN_DATE="UNVERSIONED"
SOURCE_DATE_EPOCH="1546300800"
SOURCE_DATE_EPOCH_TOUCH="201901010000"
EOF
    fi
fi

for auxfile in $AUXFILES; do
    rm -f build-aux/$auxfile
done

$AUTORECONF -fvi -Wall

# Take the auxiliary files from automake, whose copies tend to be more up to
# date than the ones autoreconf installs.
AUTOMAKE_LIBDIR="$($AUTOMAKE --print-libdir 2>/dev/null)" || true
if test -z "$AUTOMAKE_LIBDIR"; then
    set +x
    echo "error: Missing or broken automake." 1>&2
    echo "       Please make sure automake is installed and working." 1>&2
    exit 1
fi
mkdir -p build-aux
for auxfile in $AUXFILES; do
    cp "$AUTOMAKE_LIBDIR/$auxfile" build-aux/
    chmod +x build-aux/$auxfile
done

set +x

printf "\nSource tree bootstrapped successfully!\n"
