#
# Copyright (c) 2026, Chloe M.
# Provided under the BSD-3 clause
#
# Description:  Top-level build driver
# Author:		Chloe M.
#

# Globals
MAKE=make

#
# Check if a list of programs are installed on the system
#
# <@>: List of programs to check
#
check_deps() {
    for dep in $@; do
        printf "\033[33;40mChecking if $dep is installed... "
        which $dep &>/dev/null
        if [ $? -ne 0 ]; then
            echo -e "\033[31;40mno"
            echo -e "fatal: Please install '$dep'!\033[0m"
            exit 1
        fi

        echo -e "\033[32;40myes\033[0m"
    done
}

#
# Verify that the build environment is sane before we actually
# start.
#
build_verify() {
    check_deps      \
        clang       \
        rsync       \
        xorriso

    # We need build envs !!
    if [ -z "${ST_BUILD_SOURCED}" ]
    then
        echo -e "\033[31;40mfatal\033[0m: Please run '. paw/devel/build.env' first!"
        exit 1
    fi
}

#
# <$1>: Build target
# <$2>: Build message
#
make_build() {
    echo -e "\033[37m[\033[35m*\033[37m] \033[32;40m$2\033[0m"
    pushd $1;   \
        $MAKE
    popd
}

#
# Kick off the actual build process
#
build() {
    mkdir -p artifacts/

    make_build      \
        paw/        \
        "Building world..."

    make_build      \
        paw/host    \
        "Building host utils..."

    make_build      \
        paw/stos    \
        "Building system kernel..."

    make_build      \
        paw/boot    \
        "Building bootloader..."
}

build_verify
build
