#!/bin/bash

#  helper script: downloads coreboot and patches/deblobs it
#
#	Copyright (C) 2014, 2015, 2016, 2020 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Paul Kocialkowski <contact@paulk.fr>
#
#    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

printf "Downloading coreboot and (if exist in build system) applying patches\n"

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

rm -Rf "coreboot/"

mkdir "coreboot/"

cd "coreboot/"

# Get latest coreboot:
# ------------------------------------------------------------------------------

# download it using git
git clone https://review.coreboot.org/coreboot || git clone https://github.com/coreboot/coreboot.git

if [ ! -d "coreboot" ]; then
    printf "coreboot not downloaded; check network connection?\n\n"
    exit 1
fi

# Create a separate version of coreboot, for each board supported.
# Each board defines which coreboot revision to use
# ------------------------------------------------------------------------------

for boards in ../resources/coreboot/*; do

	if [ ! -d "${boards}/" ]; then
		continue
	fi

	boardname="${boards##*/}"

    if [ -f "${boards}/symlink" ]; then
        ln -s $(cat ${boards}/symlink) ${boardname}
        continue
    fi

    cbrevision="$(cat ../resources/coreboot/${boardname}/cbrevision)"

    (
        cp -R coreboot ${boardname} # copy and make specific coreboot dir for that board
        cd ${boardname}/
        git reset --hard ${cbrevision}

        git submodule update --init --checkout
        for submodule in ../../resources/coreboot/${boardname}/revision/*; do
            if [ ! -f "${submodule}" ]; then
                continue
            fi
            module="${submodule##*/}"
            modulerevision="$(cat ${submodule})"
            (
                cd "3rdparty/${module}/"
                git reset --hard ${modulerevision}
            )
        done
        if [ -f "../../resources/coreboot/${boardname}/extra.sh" ]; then
            (
                "../../resources/coreboot/${boardname}/extra.sh"
            )
        fi

        # TODO: add support for custom patches to submodules

        for patch in ../../resources/coreboot/${boardname}/patches/*.patch; do
            if [ "${patch##*/}" = "*.patch" ]; then
                continue
            fi

            git am "${patch}" || continue
        done
    )

done

# reference coreboot archive no longer required
# because we have created separate coreboot archives for each target

rm -Rf "coreboot"

cd ../

printf "\n\n"
