#! /bin/sh
#  bootstrap: Script for regenerating the whole circus.
#  Copyright (C) 2023-2024 streaksu
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -ex

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
        git -C "$2" reset --hard
        git -C "$2" clean -fd
        if ! git -C "$2" checkout $3; then
            rm -rf "$2"
        fi
    else
        if test -d "$2"; then
            set +x
            echo "error: '$2' is not a Git repository"
            exit 1
        fi
    fi
    if ! test -d "$2"; then
        git clone $1 "$2"
        if ! git -C "$2" checkout $3; then
            rm -rf "$2"
            exit 1
        fi
    fi
}

download_by_hash() {
    DOWNLOAD_COMMAND="curl -Lo"
    if ! command -v $DOWNLOAD_COMMAND >/dev/null 2>&1; then
        DOWNLOAD_COMMAND="wget -O"
        if ! command -v $DOWNLOAD_COMMAND >/dev/null 2>&1; then
            set +x
            echo "error: Neither curl nor wget found"
            exit 1
        fi
    fi
    SHA256_COMMAND="sha256sum"
    if ! command -v $SHA256_COMMAND >/dev/null 2>&1; then
        SHA256_COMMAND="sha256"
        if ! command -v $SHA256_COMMAND >/dev/null 2>&1; then
            set +x
            echo "error: Cannot find sha256(sum) command"
            exit 1
        fi
    fi
    if ! test -f "$2" || ! $SHA256_COMMAND "$2" | grep $3 >/dev/null 2>&1; then
        rm -f "$2"
        mkdir -p "$2" && rm -rf "$2"
        $DOWNLOAD_COMMAND "$2" $1
        if ! $SHA256_COMMAND "$2" | grep $3 >/dev/null 2>&1; then
            set +x
            echo "error: Cannot download file '$2' by hash"
            echo "incorrect hash:"
            $SHA256_COMMAND "$2"
            rm -f "$2"
            exit 1
        fi
    fi
}

if ! test -f version; then
    clone_repo_commit \
        https://github.com/osdev0/freestnd-c-hdrs-0bsd.git \
        freestnd-c-hdrs \
        a87c192f3eb66b0806740dc67325f9ad23fc2d0b

    # 1.0
    clone_repo_commit \
        https://github.com/uACPI/uACPI.git \
        uacpi-repository \
        1bd2930030ef819519de32dd1f7a8f999440308f
    rm -rf uacpi
    mkdir -p uacpi
    cp -rp uacpi-repository/include/uacpi uacpi/
    cp -rp uacpi-repository/source/* uacpi/
fi

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

$AUTORECONF -fvi -Wall

# Older versions of autoreconf have a bug where they do not
# install auxiliary files, sometimes... Check if that is the
# case and work around...
for auxfile in $AUXFILES; do
    if ! test -f build-aux/$auxfile; then
        if ! $AUTOMAKE --print-libdir >/dev/null 2>&1; then
            set +x
            echo "error: Broken autoreconf detected, but missing or broken automake."
            echo "       Please make sure automake is installed and working."
            exit 1
        fi
        AUTOMAKE_LIBDIR="$($AUTOMAKE --print-libdir)"
        if test -z "$AUTOMAKE_LIBDIR"; then
            # Assume `true` was passed as $AUTOMAKE
            continue
        fi
        mkdir -p build-aux
        cp -v "$AUTOMAKE_LIBDIR/$auxfile" build-aux/
        chmod +x build-aux/$auxfile
    fi
done
