#!/bin/bash
#
#   Copyright (C) 2020 Leah Rowe <info@minifree.org>
#
#    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/>.
#

# This script assumes that the working directory is the
# root of retroboot_src or retroboot git.

[ "x${DEBUG+set}" = 'xset' ] && set -v
set -u -e

# Remove the old version that may still exist
# ------------------------------------------------------------------------------

printf "Downloading Tianocore\n"

# Get latest SeaBIOS
# ------------------------------------------------------------------------------

# x230_4mb is the boardname for the coreboot environment
# used to build tianocore. a "dummy" coreboot ROM is
# build, using the coreboot build system to build tianocore
# and coreboot's build system is modified to not automatically download
# tianocore, and coreboot's Tianocore makefile has some hardcoded stuf:wf

# however, x230_4mb configs aren't used. a dummy config is in
# resources/tianocore/dummy.coreboot.config for building a coreboot ROM
# with tianocore

# This ROM is discarded, but then tianocore is in place, and fully built.
# Coreboot ROMs in Retroboot's build system specify "no payload" and
# Retroboot's build system inserts payloads manually, using cbfstool

# To build the Tianocore payload, run: ./build payload tianocore

if [ ! -d "coreboot/x230_4mb/" ]; then
    ./download coreboot
fi

(
    cd "coreboot/x230_4mb/payloads/external/tianocore/"
    make download && make update

    # revisions are hardcoded in coreboot's Makefile for Tianocore,
    # as a custom patch for that board, when running ./download coreboot

    # yes, this is cursed as fuck. i'm well aware of that.
    # but so is tianocore
)

