code:
#!/bin/bash

#  helper script: clean the dependencies that were built in coreboot
#
#	Copyright (C) 2014, 2015, 2016 Leah Rowe <info@minifree.org>
#       Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 current working directory is the root

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

# clean coreboot utilities (dependencies for 'build'):

printf "Cleaning the previous build of coreboot and its utilities\n"

# clean coreboot and crossgcc (source archives preserved)
for payload in coreboot/*; do
	for board in "${payload}/"*; do

        # Clean coreboot, of course
		make -C "${board}/" distclean

		# Clean its utilities as well
		for util in {cbfs,ifd,nvram}tool cbmem; do
			make -C "${board}/util/${util}/" clean
		done
		make -C "${board}/payloads/libpayload/" distclean

        rm -f "${board}/util/crossgcc"
	done
done

printf "\n\n"


code:
#!/bin/bash

#  helper script: clean the dependencies that were built in coreboot
#
#	Copyright (C) 2014, 2015, 2016 Leah Rowe <info@minifree.org>
#       Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 current working directory is the root

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

make -C "crossgcc/" crossgcc-clean

repo_name: libreboot/obsolete

license: mit


code:
#!/bin/bash

#  helper script: delete the .html docs that were generated from markdown
#
#	Copyright (C) 2017 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 current working directory is the root
# of libreboot_src or libreboot git

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

# clean bucts
# --------------------------------------------------------

printf "Cleaning HTML archives\n"

(
cd "www/"
cp template.html template.backup
find -L . -type f -name '*.html' -delete
mv template.backup template.html

cd "../docs/"
find -L . -type f -name '*.html' -delete
)

printf "\n\n"

# done. go back to main directory.
cd "../"


code:
#!/bin/bash

#  helper script: clean the dependencies that were built in flashrom
#
#	Copyright (C) 2014, 2015 Leah Rowe <info@minifree.org>
#       Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 current working directory is the root
# of git or release archive

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

printf "Cleaning the previous build of flashrom\n"

# clean flashrom
make -C flashrom clean
rm -f "flashrom/flashrom_lenovobios_sst"
rm -f "flashrom/flashrom_lenovobios_macronix"

printf "\n\n"


code:
#!/bin/bash

#  helper script: clean the dependencies that were built in GRUB
#
#	Copyright (C) 2014, 2015, 2016 Leah Rowe <info@minifree.org>
#       Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 current working directory is the root
# of git or release archive

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

printf "Cleaning the previous build of GRUB\n"
# clean GURB
[ -d grub/Makefile ] && make -C grub distclean

# Also clean SeaBIOS
./oldbuild clean seabios

printf "\n\n"


code:
#!/bin/bash

#  helper script: clean the dependencies that were built in i945pwm
#
#	Copyright (C) 2014, 2015 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 current working directory is the root
# of libreboot_src or libreboot git

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

# clean i945-pwm utility
# --------------------------------------------------------------------

printf "Cleaning the previous build of i945-pwm\n"
cd "resources/utilities/i945-pwm/"
make clean
cd "../../../"
printf "\n\n"


code:
#!/bin/bash

#  helper script: clean the dependencies that were built in ich9deblob
#
#	Copyright (C) 2014, 2015 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 current working directory is the root
# of libreboot_src or libreboot git

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

# clean ich9deblob utility
# --------------------------------------------------------------------

printf "Cleaning the previous build of ich9deblob\n"
cd "resources/utilities/ich9deblob/"
make clean
cd "../../../"
printf "\n\n"


code:
#!/bin/bash

#  helper script: clean the dependencies that were built in memtest86+
#
#	Copyright (C) 2014, 2015 Leah Rowe <info@minifree.org>
#       Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 current working directory is the root
# of git or release archive

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

printf "Cleaning the previous build of MemTest86+\n"

# clean MemTest86+
make -C memtest86plus clean

printf "\n\n"


code:
#!/bin/bash

#  helper script: delete the ROM images
#
#	Copyright (C) 2014, 2015 Leah Rowe <info@minifree.org>
#       Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 current working directory is the root
# of git or release archive

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

# Delete the ROM images
rm -Rf "bin/"

printf "Deleted the bin/ directory containing the ROM images.\n\n"


code:
#!/bin/bash

#  helper script: clean the dependencies that were built in seabios
#
#	Copyright (C) 2015 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 current working directory is the root
# of libreboot_src or libreboot git

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

# clean bucts
# --------------------------------------------------------

printf "Cleaning the previous build of seabios\n"

(
cd "seabios/"
rm -f "out/bios.bin.vga.elf"
make distclean
)


code:
#!/bin/bash

#
#  helper script: run "make menuconfig" on config(s) to modify them
#
#	Copyright (C) 2014, 2015, 2016 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 libreboot_src or git

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

payload="grub"
configpath="resources/libreboot/config/${payload}"

hackconfig () {
	board="${1}"
    cbrevision="$(cat ${configpath}/${board}/cbrevision)"
    vbootrevision="$(cat ${configpath}/${board}/vbootrevision)"
(
    cd "coreboot/${cbrevision}/${cbrevision}/"
    git checkout ${payload}_${board}
    (
        cd "3rdparty/vboot/"
        git checkout ${payload}_${board}
    )

	printf "Modifying config for %s (%s payload)\n" "${board}" "${payload}"
	sleep 2

	if [ ! -d "../../../${configpath}/${board}/" ]; then
		printf "build/config/grubmodify WARNING: config '%s' does not exist. Skipping.\n" "${board}"
        git checkout master
        (
            cd "3rdparty/vboot/"
            git checkout master
        )
		return
	fi

	mv "../../../${configpath}/${board}/config" ".config"
	make menuconfig
	mv ".config" "../../../${configpath}/${board}/config"

    git checkout master
    (
        cd "3rdparty/vboot/"
        git checkout master
    )
)
}

if [ $# -lt 1 ]; then
	for config in ${configpath}/*; do hackconfig "${config##*/}"; done
else
	for config in "${@}"; do hackconfig "${config}"; done
fi


code:
#!/bin/bash

#
#  helper script: run "make menuconfig" on config(s) to replace them
#
#	Copyright (C) 2014, 2015, 2016 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 libreboot_src or git

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

payload="grub"
configpath="resources/libreboot/config/${payload}"

hackconfig () {
	board="${1}"
    cbrevision="$(cat ${configpath}/${board}/cbrevision)"
    vbootrevision="$(cat ${configpath}/${board}/vbootrevision)"
(
    cd "coreboot/${cbrevision}/${cbrevision}/"
    git checkout ${payload}_${board}
    (
        cd "3rdparty/vboot/"
        git checkout ${payload}_${board}
    )

	printf "Replacing or creating new config for %s (%s payload)\n" "${board}" "${payload}"
	sleep 2

	make menuconfig

	[ ! -d "../../../${configpath}/${board}/" ] && mkdir -p "../../../${configpath}/${board}/"
	mv ".config" "../../../${configpath}/${board}/config"

    git checkout master
    (
        cd "3rdparty/vboot/"
        git checkout master
    )
)
}

if [ $# -lt 1 ]; then
	for config in ${configpath}/*; do hackconfig "${config##*/}"; done
else
	for config in "${@}"; do hackconfig "${config}"; done
fi


code:
#!/bin/bash

#
#  helper script: run "make oldconfig" on all coreboot config files
#
#	Copyright (C) 2014, 2015, 2016 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 libreboot_src or git

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

payload="grub"
configpath="resources/libreboot/config/${payload}"

hackconfig () {
	board="${1}"
    cbrevision="$(cat ${configpath}/${board}/cbrevision)"
    vbootrevision="$(cat ${configpath}/${board}/vbootrevision)"
(
    cd "coreboot/${cbrevision}/${cbrevision}/"
    git checkout ${payload}_${board}
    (
        cd "3rdparty/vboot/"
        git checkout ${payload}_${board}
    )

	printf "Updating config for %s (%s payload)\n" "${board}" "${payload}"
	sleep 2

	if [ ! -d "../../../${configpath}/${board}/" ]; then
		printf "build/config/grubupdate WARNING: config '%s' does not exist. Skipping.\n" "${board}"
        git checkout master
        (
            cd "3rdparty/vboot/"
            git checkout master
        )
		return
	fi

	mv "../../../${configpath}/${board}/config" ".config"
	make oldconfig
	mv ".config" "../../../${configpath}/${board}/config"

    git checkout master
    (
        cd "3rdparty/vboot/"
        git checkout master
    )
)
}

if [ $# -lt 1 ]; then
	for config in ${configpath}/*; do hackconfig "${config##*/}"; done
else
	for config in "${@}"; do hackconfig "${config}"; done
fi


code:
#!/bin/bash

#
#  helper script: update the seabios config
#
#	Copyright (C) 2014, 2015 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 libreboot_src or git

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

(
cd "seabios/"

for cfgtype in config config_vga; do

    printf "doing config %s\n" "${cfgtype}"
    sleep 2

    if [ -f "../resources/seabios/config/${cfgtype}" ]; then
    	cp "../resources/seabios/config/${cfgtype}" ".config"
    fi

    make menuconfig

    mv ".config" "../resources/seabios/config/${cfgtype}"

done
)


code:
#!/bin/bash

#
#  helper script: run "make menuconfig" on config(s) to modify them
#
#	Copyright (C) 2014, 2015, 2016 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 libreboot_src or git

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

payload="seabios"
configpath="resources/libreboot/config/${payload}"

hackconfig () {
	board="${1}"
    cbrevision="$(cat ${configpath}/${board}/cbrevision)"
    vbootrevision="$(cat ${configpath}/${board}/vbootrevision)"
(
    cd "coreboot/${cbrevision}/${cbrevision}/"
    git checkout ${payload}_${board}
    (
        cd "3rdparty/vboot/"
        git checkout ${payload}_${board}
    )

	printf "Modifying config for %s (%s payload)\n" "${board}" "${payload}"
	sleep 2

	if [ ! -d "../../../${configpath}/${board}/" ]; then
		printf "build/config/seabiosmodify WARNING: config '%s' does not exist. Skipping.\n" "${board}"
        git checkout master
        (
            cd "3rdparty/vboot/"
            git checkout master
        )
		return
	fi

	mv "../../../${configpath}/${board}/config" ".config"
	make menuconfig
	mv ".config" "../../../${configpath}/${board}/config"

    git checkout master
    (
        cd "3rdparty/vboot/"
        git checkout master
    )
)
}

if [ $# -lt 1 ]; then
	for config in ${configpath}/*; do hackconfig "${config##*/}"; done
else
	for config in "${@}"; do hackconfig "${config}"; done
fi


code:
#!/bin/bash

#
#  helper script: run "make menuconfig" on config(s) to replace them
#
#	Copyright (C) 2014, 2015, 2016 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 libreboot_src or git

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

payload="seabios"
configpath="resources/libreboot/config/${payload}"

hackconfig () {
	board="${1}"
    cbrevision="$(cat ${configpath}/${board}/cbrevision)"
    vbootrevision="$(cat ${configpath}/${board}/vbootrevision)"
(
    cd "coreboot/${cbrevision}/${cbrevision}/"
    git checkout ${payload}_${board}
    (
        cd "3rdparty/vboot/"
        git checkout ${payload}_${board}
    )

	printf "Replacing or creating new config for %s (%s payload)\n" "${board}" "${payload}"
	sleep 2

	make menuconfig

	[ ! -d "../../../${configpath}/${board}/" ] && mkdir -p "../../../${configpath}/${board}/"
	mv ".config" "../../../${configpath}/${board}/config"

    git checkout master
    (
        cd "3rdparty/vboot/"
        git checkout master
    )
)
}

if [ $# -lt 1 ]; then
	for config in ${configpath}/*; do hackconfig "${config##*/}"; done
else
	for config in "${@}"; do hackconfig "${config}"; done
fi


code:
#!/bin/bash

#
#  helper script: run "make oldconfig" on all coreboot config files
#
#	Copyright (C) 2014, 2015, 2016 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 libreboot_src or git

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

payload="seabios"
configpath="resources/libreboot/config/${payload}"

hackconfig () {
	board="${1}"
    cbrevision="$(cat ${configpath}/${board}/cbrevision)"
    vbootrevision="$(cat ${configpath}/${board}/vbootrevision)"
(
    cd "coreboot/${cbrevision}/${cbrevision}/"
    git checkout ${payload}_${board}
    (
        cd "3rdparty/vboot/"
        git checkout ${payload}_${board}
    )

	printf "Updating config for %s (%s payload)\n" "${board}" "${payload}"
	sleep 2

	if [ ! -d "../../../${configpath}/${board}/" ]; then
		printf "build/config/seabiosupdate WARNING: config '%s' does not exist. Skipping.\n" "${board}"
        git checkout master
        (
            cd "3rdparty/vboot/"
            git checkout master
        )
		return
	fi

	mv "../../../${configpath}/${board}/config" ".config"
	make oldconfig
	mv ".config" "../../../${configpath}/${board}/config"

    git checkout master
    (
        cd "3rdparty/vboot/"
        git checkout master
    )
)
}

if [ $# -lt 1 ]; then
	for config in ${configpath}/*; do hackconfig "${config##*/}"; done
else
	for config in "${@}"; do hackconfig "${config}"; done
fi


code:
#!/bin/bash

#
#  helper script: update the seabios config
#
#	Copyright (C) 2014, 2015 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 libreboot_src or git

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

(
cd seabios/

for cfgtype in config config_vga; do

    printf "doing config %s\n" "${cfgtype}"
    sleep 2

    if [ -f ../resources/seabios/config/${cfgtype} ]; then
	    cp ../resources/seabios/config/${cfgtype} .config
    	make oldconfig
    	mv .config ../resources/seabios/config/${cfgtype}
    fi

done
)


code:
#!/bin/bash

#  helper script: installs build dependencies for Parabola

# this script is based on the script for Trisquel 7
#
#	Copyright (C) 2014, 2015 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Klemens Nanni <klemens@posteo.de>
#
#    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/>.
#

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

if [ $EUID -ne 0 ]; then
   printf "This script must be run as root\n"
   exit 1
fi

# Duplications are intentional. Please do not re-factor.
#
# This is so that they can moved to separate scripts.
#

pacman -S --needed wget

# For downloading source code
# ------------------------------------------------------------

printf "Source Code: In order to download the it, git is needed\n"
pacman -S --needed git

# For building the documentation
# ------------------------------------------------------------

pacman -S --needed pandoc texi2html

# For building source code:
# ------------------------------------------------------------

printf "Building Source Code: development packages\n"
pacman -S --needed base-devel libstdc++5

pacman -S arm-none-eabi-gcc

pacman -S --needed libpciaccess

[ "$(uname -i)" = x86_64 ] || [ "$(uname -m)" = x86_64 ]
arch=${?}

# For cross-compiling i686 target on x86_64 host.
if [ "${arch}" -eq 0 ]; then
	printf "Cross-Compiling: You are running on 64-bit architecture, however 32-bit libraries are needed\n"
	pacman -Sl multilib &>/dev/null || (printf "You must enable the multilib repository in /etc/pacman.conf first\n"; exit 1)
	pacman -S --needed gcc-multilib gcc-libs-multilib lib32-libltdl lib32-libpciaccess lib32-libstdc++5 lib32-glibc lib32-fakeroot
fi

# Memtest86+ build dependencies
# ------------------------------------------------------------

# pacman -S --needed base-devel

# i945-pwm build dependencies
# ------------------------------------------------------------

# pacman -S base-devel

# Coreboot build dependencies (also requires build-essential and git)
# ------------------------------------------------------------

printf "Coreboot: Ncurses for menuconfig and a few other tools\n"
pacman -S ncurses doxygen iasl gdb flex bison
# pacman -S git base-devel

# For cross-compiling i686 target on x86_64 host.
if [ "${arch}" -eq 0 ]; then
	printf "Cross-Compiling: You are running on 64-bit architecture, however 32-bit libraries are needed\n"
	pacman -S --needed lib32-ncurses
fi

# GRUB2 build dependencies (also requires build-essential, bison and flex)
# pacman -S --needed base-devel bison flex

printf "GRUB: Fonts, (crypto) filesystem support, automake, etc.\n"
pacman -S --needed autogen m4 autoconf help2man freetype2 automake fuse fuse-exfat xz gawk device-mapper libtool python
pacman -S --needed bdf-unifont # trying this, otherwise there was an error. I'll try AUR if this won't work.

# BucTS build dependencies (external script)
# ------------------------------------------------------------

# pacman -S --needed base-devel

# Flashrom build dependencies (also requires build-essential)
# ------------------------------------------------------------

printf "Flashrom: Your external programmer will be most probably using PCI bus, FTDI chips need additional libraries\n"
pacman -S --needed pciutils zlib libftdi libftdi-compat
# pacman -S --needed base-devel


code:
#!/bin/bash

# for_trisquel7 script: installs build dependencies for Trisquel 7
#
#	Copyright (C) 2014, 2015 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/>.
#

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

if [ $EUID -ne 0 ]; then
   printf "This script must be run as root\n"
   exit 1
fi

# Duplications are intentional. Please do not re-factor.
#
# This is so that they can moved to separate scripts.
#

apt-get -y install wget

# For downloading source code
# ------------------------------------------------------------

apt-get -y install git

# For building the documentation
# ------------------------------------------------------------

apt-get -y install pandoc texi2html texinfo texlive

# For building source code:
# ------------------------------------------------------------

apt-get -y install build-essential

# for cross-compiling ARM binaries
apt-get -y install gcc-arm-linux-gnueabi || printf "debian stretch ftw\n"

[ "$(uname -i)" = x86_64 ] || [ "$(uname -m)" = x86_64 ]
arch=${?}

# For cross-compiling i686 target on x86_64 host.
if [ "${arch}" -eq 0 ];	then
	apt-get -y install gcc-multilib libc6-i386 libc6-dev-i386
	apt-get -y install lib32stdc++6 g++-multilib dh-autoreconf
	# recommended, but not sure what for:
	apt-get -y install lib32tinfo-dev texi2html
fi

# Memtest86+ build dependencies
# ------------------------------------------------------------

apt-get -y install build-essential

# i945-pwm build dependencies
# ------------------------------------------------------------

apt-get -y install build-essential

# Coreboot build dependencies (also requires build-essential and git)
# ------------------------------------------------------------

apt-get -y install libncurses5-dev doxygen iasl gdb flex bison build-essential git libssl-dev

# For cross-compiling i686 target on x86_64 host.
[ "${arch}" -eq 0 ] && apt-get -y install lib32ncurses5-dev

# GRUB build dependencies (also requires build-essential, bison and flex)
# ------------------------------------------------------------

apt-get -y install ttf-unifont libopts25 libselinux1-dev autogen m4 autoconf help2man libopts25-dev libfont-freetype-perl automake autotools-dev build-essential bison flex libfuse-dev liblzma-dev gawk libdevmapper-dev libtool libfreetype6-dev

# BucTS build dependencies (external script)
# ------------------------------------------------------------

apt-get -y install build-essential

# Flashrom build dependencies (also requires build-essential)
# ------------------------------------------------------------

apt-get -y install libpci-dev pciutils zlib1g-dev libftdi-dev build-essential libusb-1.0-0-dev libusb-1.0

# For cross-compiling i686 target on x86_64 host.
[ "${arch}" -eq 0 ] && apt-get -y install lib32z1-dev


code:
#!/bin/bash

#
#  helper script: generate html pages from the markdown documentation
#
#	Copyright (C) 2017 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 libreboot_src or git

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

(
cd "www/"
./generate.sh
)


code:
#!/bin/bash

#  helper script: builds bucts source code
#
#	Copyright (C) 2014, 2015 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 libreboot_src or git

[ "x${DEBUG+set}" = 'xset' ] && set -v
set -u -e
if [ -z ${NPROC+x} ]; then
    cores="$(nproc)"
else
    case ${NPROC} in
        ''|*[!0-9]*)
            printf "value '%s' for NPROC is invalid. non-numeric. Exiting.\n" "${NPROC}"
            exit 1
            ;;
    esac
    cores="${NPROC}"
fi

# Build BUC.TS utility (needed for flashing ROM images on X60/T60 while Lenovo BIOS is running)
# --------------------------------------------------------------------

printf "Building bucts\n"

cd "bucts/"

buildtype="unknown"
if (( $# != 1 )); then
	buildtype="dynamic"
else
	buildtype="static"
fi

if [ "${buildtype}" = "static" ]; then
	patch "Makefile" < "../resources/bucts/patch/staticlink.diff"
fi

make clean
make -j${cores}

if [ "${buildtype}" = "static" ]; then
	patch "Makefile" -R < "../resources/bucts/patch/staticlink.diff"
fi

# done. go back to main directory
cd "../"

printf "\n\n"


code:
#!/bin/bash

#  helper script: builds the dependencies that coreboot needs before building a ROM image
#
#	Copyright (C) 2014, 2015, 2016 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 git or release archive

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

# Build utilities needed in coreboot directory
# --------------------------------------------------------------------

printf "Building the utilities in coreboot\n"

# build coreboot utilities (in each revision) and
# create symlinks to the crossgcc archive
for payload in $HOMEDIR/coreboot/*; do
	for board in "${payload}/"*; do

	# cbfstool, cbmem, nvramtool
	for util in {cbfs,nvram}tool cbmem; do
        if [ "${util}" = "cbfstool" ]; then
            sed -i '/.*fmd_scanner.o.*-Wno-unused-function$/ s/$/ -Wno-sign-compare/'  "${board}"/util/cbfstool/Makefile.inc
        fi
		make -BC "${board}/util/${util}"
	done
	# create symlink to crossgcc
	(

	ln -fs $HOMEDIR/crossgcc/util/crossgcc/ ${board}/util/crossgcc
	)

	done
done

for payloads in $HOMEDIR/resources/libreboot/config/*; do

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

	payload="${payloads##*/}"

	for boardconfig in $HOMEDIR/resources/libreboot/config/${payload}/*; do

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

		boardname="${boardconfig##*/}"
		cbrevision=$(cat "${boardconfig}/cbrevision")
		vbootrevision=$(cat "${boardconfig}/vbootrevision")

		reused_coreboot_patches="$HOMEDIR/resources/libreboot/patch/coreboot/${cbrevision}/${payload}/${boardname}/reused.list"
		reused_vboot_patches="$HOMEDIR/resources/libreboot/patch/vboot/${vbootrevision}/${payload}/${boardname}/reused.list"
		for reused_patches in "${reused_coreboot_patches}" "${reused_vboot_patches}"; do
			if [ -f "${reused_patches}" ]; then
				for patch in $(cat "${reused_patches}"); do
					if [ ! -f "./${patch}" ]; then
						printf "%s listed in %s does not exist\n" "${patch}" "${reused_patches}"
						exit 1
					fi
				done
			fi
		done

	done
done

# sanity check (check for invalid paths in the reused.list patch lists before proceeding)
# in ascending filename order, apply patches from a directory
apply_patches_from_directory() {
	patch_directory="${1}" # directory containing the patch files

	if [ -d "${patch_directory}" ]; then
		for patch in ${patch_directory}/*.patch;  do

			if [ "${patch##*/}" = "*.patch" ]; then # oh so ugly
				continue # ugly ugly ugly ugly ugly
			fi # most hideous thing you've ever seen

			git am "${patch}" || continue
		done
	fi
}
# files listed in the file (if found) are absolute paths, relative to the root of the libreboot src directory
# the file lists patches patches that should be applied
apply_patches_from_file() {
	patch_list="${1}" # file listing the paths to all the patches
	libreboot_src_root="${2}" # path to the root of the libreboot_src directory

	if [ -f "${patch_list}" ]; then
		for patchname in $(cat "${patch_list}"); do
			git am "${libreboot_src_root}/${patchname}" || continue
		done
	fi
}

create_branch() {
	branchname="${1}"
    git branch ${branchname}
    git checkout ${branchname}
    git checkout master
}

# use git-init on everything
# this is so that we can then apply patche
# for these revisions of vboot and coreboot
for i in $HOMEDIR/coreboot/*; do
    if [ ! -d "${i}/" ]; then
        continue
    fi
    for revision in ${i}/*; do
        if [ ! -d "${revision}/" ]; then
            continue
        fi
        (
        cd "${revision}/"
        rm -rf .git
        git init
        git add -A .
        git commit -m "coreboot revision ${revision##*/}"
        cd "3rdparty/vboot/"
        rm -rf .git
        git init
        git add -A .
        git commit -m "coreboot revision ${revision##*/}"
        )
    done
done

for payloads in $HOMEDIR/resources/libreboot/config/*; do

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

	payload="${payloads##*/}"

	for boardconfig in $HOMEDIR/resources/libreboot/config/${payload}/*; do

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

		boardname="${boardconfig##*/}"
		cbrevision=$(cat "${boardconfig}/cbrevision")
		vbootrevision=$(cat "${boardconfig}/vbootrevision")
        branchname="${payload}_${boardname}"

        # the same vboot revision is always used for coreboot revision,
        # so we don't need to wworry about checking for that here

		# patch that version
		(

			cd "$HOMEDIR/coreboot/${cbrevision}/${cbrevision}/"
			create_branch ${branchname}

            git checkout ${branchname}

			# apply patches (coreboot, common to all systems using this revision)
			apply_patches_from_directory "$HOMEDIR/resources/libreboot/patch/common/coreboot/${cbrevision}"
			# apply patches re-used from other boards, before applying main patches (common patches for similar boards)
			apply_patches_from_file "$HOMEDIR/resources/libreboot/patch/coreboot/${cbrevision}/${payload}/${boardname}/reused.list" ../../..
			# apply patches (coreboot, machine-specific for this revision)
			apply_patches_from_directory "$HOMEDIR/resources/libreboot/patch/coreboot/${cbrevision}/${payload}/${boardname}"

            git checkout master

			cd "3rdparty/vboot/"
			# reset to known revision (vboot)
			create_branch ${branchname}

            git checkout ${branchname}

			# apply patches (vboot, common to all systems using this revision)
			apply_patches_from_directory "$HOMEDIR/resources/libreboot/patch/common/vboot/${vbootrevision}"
			# apply patches re-used from other boards, before applying main patches (common patches for similar boards)
			apply_patches_from_file "$HOMEDIR/resources/libreboot/patch/vboot/${vbootrevision}/${payload}/${boardname}/reused.list" ../../../../..
			# apply patches (vboot, machine-specific for this revision)
			apply_patches_from_directory "$HOMEDIR/resources/libreboot/patch/vboot/${vbootrevision}/${payload}/${boardname}"

            git checkout master

		)
	done
done


code:
#!/bin/bash

#  helper script: builds crossgcc, needed by coreboot's build system
#
#	Copyright (C) 2016 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 git or release archive

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

if [ -z ${NPROC+x} ]; then
    cores="$(nproc)"
else
    case ${NPROC} in
        ''|*[!0-9]*)
            printf "value '%s' for NPROC is invalid. non-numeric. Exiting.\n" "${NPROC}"
            exit 1
            ;;
    esac
    cores="${NPROC}"
fi

(
cd "crossgcc/"
for architecture in i386 arm; do
    make crossgcc-${architecture} CPUS=${cores}
done
)


code:
#!/bin/bash

#  helper script: builds flashrom source code
#
#	Copyright (C) 2014, 2015 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 git or release archive

[ "x${DEBUG+set}" = 'xset' ] && set -v
set -u -e
if [ -z ${NPROC+x} ]; then
    cores="$(nproc)"
else
    case ${NPROC} in
        ''|*[!0-9]*)
            printf "value '%s' for NPROC is invalid. non-numeric. Exiting.\n" "${NPROC}"
            exit 1
            ;;
    esac
    cores="${NPROC}"
fi

# Build "flashrom" (utility for flashing/dumping ROMs)
# --------------------------------------------------------------------

printf "Building flashrom\n"

cd "flashrom/"

make clean
if (( $# != 1 )); then
	make -j${cores}
else
	if [ "${1}" = "static" ]; then
		make SHARED=0 CC='gcc -static' -j${cores} CONFIG_ENABLE_LIBUSB1_PROGRAMMERS=no
	else
		make -j${cores} CONFIG_ENABLE_LIBUSB1_PROGRAMMERS=no
	fi
fi

mv "flashrom" "flashrom_normal"

# build patched binaries for MX25L1605D and SST25VF016B flash chips
# - these patches are needed for initial installation on an X60 or T60
# - these patches are for people who have lenovo bios running
for patchname in "lenovobios_macronix" "lenovobios_sst"
do
	# first remove the existing build
	rm -f "flashrom_${patchname}"

	# backup the unpatched flashchips.c (it will be patched)
	cp "flashchips.c" "flashchips.c_"
	# patch flashchips.c
	patch "flashchips.c" < "../resources/flashrom/patch/${patchname}.diff"

	make clean
	if (( $# != 1 )); then
		make -j${cores} CONFIG_ENABLE_LIBUSB1_PROGRAMMERS=no
	else
		if [ "${1}" = "static" ]; then
			make SHARED=0 CC='gcc -static' -j${cores} CONFIG_ENABLE_LIBUSB1_PROGRAMMERS=no
		else
			make -j${cores} CONFIG_ENABLE_LIBUSB1_PROGRAMMERS=no
		fi
	fi

	# Rename the binary based on the patch name
	mv "flashrom" "flashrom_${patchname}"

	# restore unpatched flashchips.c
	rm -f "flashchips.c"
	mv "flashchips.c_" "flashchips.c"
done

mv "flashrom_normal" "flashrom"

cd "../"


code:
#!/bin/bash

#  helper script: builds GRUB2 source code
#
#	Copyright (C) 2014, 2015 Leah Rowe <info@minifree.org>
#   Copyright (C) 2015, 2016 Klemens Nanni <contact@autoboot.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 git or release archive

[ "x${DEBUG+set}" = 'xset' ] && set -v
set -u -e
if [ -z ${NPROC+x} ]; then
    cores="$(nproc)"
else
    case ${NPROC} in
        ''|*[!0-9]*)
            printf "value '%s' for NPROC is invalid. non-numeric. Exiting.\n" "${NPROC}"
            exit 1
            ;;
    esac
    cores="${NPROC}"
fi

# Build GRUB2 as coreboot payload

printf "Building GRUB\n"

# use a subshell to not end up in grub/ in case of build issues
(
cd grub/

# clean up first
[ -d Makefile ] && make distclean

# build
./autogen.sh
./configure --with-platform=coreboot
make -j${cores}
)

# Also build SeaBIOS, which we use with GRUB to make SeaGRUB
./oldbuild module seabios


code:
#!/bin/bash

#  helper script: builds i945-pwm source code
#
#	Copyright (C) 2014, 2015 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 libreboot_src or git

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

# Build i945-pwm utility
# --------------------------------------------------------------------

printf "Building i945-pwm\n"

cd "resources/utilities/i945-pwm/"

make clean
# build it
make

# done. go back to main directory
cd "../../../"

printf "\n\n"


code:
#!/bin/bash

#  helper script: builds ich9deblob source code
#
#	Copyright (C) 2014, 2015 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 libreboot_src or git

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

# Build ich9deblob utility
# --------------------------------------------------------------------

printf "Building ich9deblob\n"

cd "resources/utilities/ich9deblob/"

# clean it first
make clean

# build ich9deblob
make

# done. go back to main directory
cd "../../../"

printf "\n\n"


code:
#!/bin/bash

#  helper script: builds memtest86+ source code
#
#	Copyright (C) 2014, 2015 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 git or release archive

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

# Build MemTest86+ payload
# --------------------------------------------------------------------

printf "Building MemTest86+\n"

make -BC memtest86plus


code:
#!/bin/bash

#  helper script: builds SeaBIOS source code
#
#	Copyright (C) 2015 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 libreboot_src or git

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

# Build SeaBIOS
# ---------------------------------------------------------------------

printf "Building SeaBIOS\n"

cd seabios/

cp ../resources/seabios/config/config .config

# clean it first
[[ -f Makefile ]] && make clean

# build SeaBIOS
make

mv "out/bios.bin.elf" ../

printf "\n\n"

rm -f .config

make distclean

cp ../resources/seabios/config/config_vga .config

make
rm -f .config

mv out/bios.bin.elf out/bios.bin.vga.elf
mv ../bios.bin.elf out/

# done. go back to main directory
cd ../

# ------------------- DONE ----------------------


code:
#!/bin/bash

#
#  helper script: generate the ROM image release archives
#
#	Copyright (C) 2015 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Patrick "P. J." McDermott <pj@pehjota.net>
#
#    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/>.
#

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

if [ ! -d bin/ ]; then
	printf "build/release/roms: no bin/ directory exists. (you haven't built any images)\n"
	exit 1
fi

if [ -f "version" ]; then
	# _src release archive is being used
	version="$(cat version)"
	versiondate="$(cat versiondate)"
else
	# git repo is being used
	version="$(git describe --tags HEAD)"
	versiondate="$(git show -s --format=%ct)"
fi
versiondir="release/oldbuildsystem/${version}"
romdir="${versiondir}/rom"
distname="libreboot_${version}"

printf 'Building ROM image release archives for...\n'

cd "bin/"

for payload in *; do

	[ ! -d "../${romdir}/${payload}/" ] && mkdir -p "../${romdir}/${payload}/"

	cd "${payload}/"

	for board in *; do

		archivename="${distname}_${payload}_${board}"

		printf '...%s' "${payload}/${board}"

		# Delete the old archive
		rm -f "../../${romdir}/${payload}/${distname}_${board}.tar.xz"

		cp -R "${board}/" "${archivename}/"

		# this has to be done before generating
		# the "version" file
		if [ ! -f "version" ]; then
			# generate ChangeLog and NEWS files
			rm -f "ChangeLog" "NEWS"
			git log > "${archivename}/ChangeLog"
			cp "${archivename}/ChangeLog" "${archivename}/NEWS"
		else
			# building from release archive
			cp "ChangeLog" "${archivename}/"
			cp "NEWS" "${archivename}/"
		fi

        if [ -f "../../RELEASE" ]; then
            rm -f "${archivename}/ChangeLog"
            rm -f "${archivename}/NEWS"
            cp "../../RELEASE" "${archivename}/ChangeLog"
            cp "../../RELEASE" "${archivename}/NEWS"
        fi

		# Put the version string in the archive.
		printf '%s\n' "${version}" >"${archivename}/version"
		# Put the version date string in the archive
		printf '%s\n' "${versiondate}" >"${archivename}/versiondate"

		# Create the compressed archive.
		tar -c "${archivename}/" | xz -9e >"../../${romdir}/${payload}/${archivename}.tar.xz"

		rm -Rf "${archivename:?}/"

		printf ' OK\n'

	done

	cd "../"

done

cd "../"

printf 'ROM image release archives are stored in %s/\n' "${romdir}"


code:
#!/bin/bash

#
#  helper script: create sha512sum file for the current snapshot
#
#	Copyright (C) 2015 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/>.
#

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

if [ -f "version" ]; then
	# _src release archive is being used
	version="$(cat version)"
	versiondate="$(cat versiondate)"
else
	# git repo is being used
	version="$(git describe --tags HEAD)"
	versiondate="$(git show -s --format=%ct)"
fi
versiondir="release/oldbuildsystem/${version}"
sha512filename="SHA512SUMS"
sha512filepath="${versiondir}/${sha512filename}"

if [ ! -d "${versiondir}/" ]; then

	printf "build/release/sha512sums: directory %s/ does not exist. You haven't generated any archives yet.\n" "${versiondir}"
	exit 1

fi

# delete the old file
rm -f "${sha512filepath}"

# create sha512sum file
printf "Creating list of SHA512 sums in %s...\n" "${sha512filepath}"
(cd "${versiondir}/" && for file in $(find -type f); do sha512sum "${file}" >> "${sha512filename}"; done)
printf "...done.\n"

printf "\n"


code:
#!/bin/bash

#
#  helper script: generate the source release archive
#
#	Copyright (C) 2014, 2015 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Patrick "P. J." McDermott <pj@pehjota.net>
#
#    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/>.
#

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

printf 'Building the source release archive\n'

if [ -f "version" ]; then
	# _src release archive is being used
	version="$(cat version)"
	versiondate="$(cat versiondate)"
else
	# git repo is being used
	version="$(git describe --tags HEAD)"
	versiondate="$(git show -s --format=%ct)"
fi
versiondir="release/oldbuildsystem/${version}"
distname="libreboot_${version}_src"
distdir="${versiondir}/${distname}"

printf 'Deleting old source release archives\n'
rm -f "${distdir}.tar.xz"
rm -Rf "${distdir:?}/"

mkdir -p "${distdir}/"

printf 'Copying sources to %s/\n' "${distdir}"
for resource in *; do
	if [ "${resource}" = "release" ]; then
		continue
	fi
	cp -R "${resource}" "${distdir}/"
done

# this has to be done before generating
# the "version" file
if [ ! -f "version" ]; then
	# generate ChangeLog and NEWS files
	rm -f "ChangeLog" "NEWS"
	git log > "${distdir}/ChangeLog"
	cp "${distdir}/ChangeLog" "${distdir}/NEWS"
else
	# building from release archive
	cp "ChangeLog" "${distdir}/"
	cp "NEWS" "${distdir}/"
fi

if [ -f "RELEASE" ]; then
    rm -f "${distdir}/ChangeLog"
    rm -f "${distdir}/NEWS"
    cp "RELEASE" "${distdir}/ChangeLog"
    cp "RELEASE" "${distdir}/NEWS"
fi

# include version information
printf '%s\n' "${version}" >"${distdir}/version"
# include version date information
printf '%s\n' "${versiondate}" >"${distdir}/versiondate"

printf 'Cleaning files in %s/\n' "${distdir}"

# Clean old builds.
(cd "${distdir}/" && ./oldbuild clean all)
printf '\n'

# Delete Git repositories and properties and Subversion administrative
# directories and properties.
rm -Rf "${distdir}/bucts/".git*
rm -Rf "${distdir}/flashrom/".git*
rm -Rf "${distdir}/grub/".git*
rm -Rf "${distdir}/seabios/".git*

rm -f "${distdir}"/*.vim

# Delete useless files.
rm -Rf "${distdir}/TODO/"
rm -f "${distdir}/push"

# Delete the deblob scripts.
# Since the source archive doesn't distribute the download scripts and already
# comes with a deblobbed coreboot, the deblobbing scripts aren't needed at all.
rm -Rf "${distdir}/resources/utilities/coreboot-libre/"

# Download scripts not needed, because the modules already exist
# in the src release archive
rm -f "${distdir}/download"
rm -Rf "${distdir}/resources/scripts/helpers/download/"

rm -Rf "${distdir}/resources/grub/patch/"
rm -Rf "${distdir}/resources/memtest86plus/patch/"

# ich9deblob: There are certain files in there that the user most likely does
# not want to share.
rm -f "${distdir}/resources/utilities/ich9deblob/"*.bin
rm -f "${distdir}/resources/utilities/ich9deblob/"*.rom
rm -f "${distdir}/resources/utilities/ich9deblob/mk"*.[ch]
rm -f "${distdir}/mk"*.[ch]
rm -f "${distdir}/"*.bin
rm -f "${distdir}/"*.rom

# Remove any GRUB payload ELF executables that may exist
rm -f "${distdir}/resources/utilities/grub-assemble/grub_txtmode.elf"
rm -f "${distdir}/resources/utilities/grub-assemble/grub_vesafb.elf"

printf 'Creating %s.tar.xz\n' "${distdir}"
(cd "${versiondir}/" && tar -c "${distname}/" | xz -9e >"${distname}.tar.xz")

rm -Rf "${distdir:?}/"

printf 'Source release archive is stored in %s/\n' "${versiondir}"


code:
#!/bin/bash

#  helper: prepare a small source archive for those utils
#  that don't easily cross-compile. Then the tarball can be extracted
#  on those systems with the target architecture, and compiled.
#
#	Copyright (C) 2014, 2015 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/>.
#

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

if [ -f "version" ]; then
	# _src release archive is being used
	version="$(cat version)"
	versiondate="$(cat versiondate)"
else
	# git repo is being used
	version="$(git describe --tags HEAD)"
	versiondate="$(git show -s --format=%ct)"
fi
versiondir="release/oldbuildsystem/${version}"
distname="libreboot_${version}_tobuild"
distdir="${versiondir}/${distname}"

printf "Creating source archive for flashrom and bucts\n"

# delete the old data
rm -Rf "${distdir:?}/"
rm -f "${distdir}.tar.xz"

# this is where they will go
mkdir -p "${distdir}/"

# flashrom
cp -R "flashrom/" "${distdir}/"
rm -f "${distdir}/flashrom/flashrom_lenovobios_sst"
rm -f "${distdir}/flashrom/flashrom_lenovobios_macronix"
(cd "${distdir}/flashrom/" && make clean)
mkdir -p "${distdir}/resources/flashrom/"
cp -R "resources/flashrom/patch/" "${distdir}/resources/flashrom/"

# bucts
cp -R "bucts/" "${distdir}/"
(cd "${distdir}/bucts/" && make clean)
mkdir -p "${distdir}/resources/bucts/"
cp -R "resources/bucts/patch/" "${distdir}/resources/bucts/"

# the build script will be needed
cp build "${distdir}/"
# needed build scripts (helpers)
mkdir -p "${distdir}/resources/scripts/helpers/build/"
mkdir -p "${distdir}/resources/scripts/helpers/build/module/"
mkdir -p "${distdir}/resources/scripts/helpers/build/clean/"
cp "resources/scripts/helpers/build/clean/bucts" "${distdir}/resources/scripts/helpers/build/clean/"
cp "resources/scripts/helpers/build/clean/flashrom" "${distdir}/resources/scripts/helpers/build/clean/"
cp "resources/scripts/helpers/build/module/bucts" "${distdir}/resources/scripts/helpers/build/module/"
cp "resources/scripts/helpers/build/module/flashrom" "${distdir}/resources/scripts/helpers/build/module/"
cp "resources/scripts/misc/powertop.trisquel7" "${distdir}/"

# this has to be done before generating
# the "version" file
if [ ! -f "version" ]; then
	# generate ChangeLog and NEWS files
	rm -f "ChangeLog" "NEWS"
	git log > "${distdir}/ChangeLog"
	cp "${distdir}/ChangeLog" "${distdir}/NEWS"
else
	# building from release archive
	cp "ChangeLog" "${distdir}/"
	cp "NEWS" "${distdir}/"
fi

if [ -f "RELEASE" ]; then
    rm -f "${distdir}/ChangeLog"
    rm -f "${distdir}/NEWS"
    cp "RELEASE" "${distdir}/ChangeLog"
    cp "RELEASE" "${distdir}/NEWS"
fi

# include version information
printf '%s\n' "${version}" >"${distdir}/version"
# include version date information
printf '%s\n' "${versiondate}" >"${distdir}/versiondate"

# that is all. now tar it up
(cd "${versiondir}/" && tar -c "${distname}/" | xz -9e >"${distname}.tar.xz")

# and delete the directory
rm -Rf "${distdir:?}/"

printf "\n"
printf "Tar archive created: %s\n" "${distdir}.tar.xz"
printf "NOTE: don't forget to add ARM binaries for flashrom.\n"
printf "NOTE: don't forget to add i386 binaries for flashrom/bucts.\n"
printf "The archive %s.tar.xz has been created with everything needed to build these utilities.\n\n" "${distname}"


code:
#!/bin/bash

#
#  helper script: generate the release archives
#
#	Copyright (C) 2014, 2015, 2016 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 current working directory when running
# it is the root directory of the libreboot git repository clone.

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

arch="unknown"
if [ "$(uname -i)" = "i686" ] || [ "$(uname -m)" = "i686" ]
	then
		arch="i686"
		echo "Running on i686. ok."
		sleep 2
elif [ "$(uname -i)" = "x86_64" ] || [ "$(uname -m)" = "x86_64" ]
	then
		arch="x86_64"
		echo "Running on x86_64. ok."
		sleep 2
else
	echo "This script must be run on an i686 or x86_64 host. x86_64 is recommended."
	exit 1
fi

if [ -f "version" ]; then
	# _src release archive is being used
	version="$(cat version)"
	versiondate="$(cat versiondate)"
else
	# git repo is being used
	version="$(git describe --tags HEAD)"
	versiondate="$(git show -s --format=%ct)"
fi
versiondir="release/oldbuildsystem/${version}"
distname="libreboot_${version}_util"
distdir="${versiondir}/${distname}"

printf "Creating utility archive (static executables)\n"

# delete the old data
rm -Rf "${distdir:?}/"
rm -f "${distdir}.tar.xz"

# this is where they will go
mkdir -p "${distdir}/"

# this has to be done before generating
# the "version" file
if [ ! -f "version" ]; then
	# generate ChangeLog and NEWS files
	rm -f "ChangeLog" "NEWS"
	git log > "${distdir}/ChangeLog"
	cp "${distdir}/ChangeLog" "${distdir}/NEWS"
else
	# building from release archive
	cp "ChangeLog" "${distdir}/"
	cp "NEWS" "${distdir}/"
fi

if [ -f "RELEASE" ]; then
    rm -f "${distdir}/ChangeLog"
    rm -f "${distdir}/NEWS"
    cp "RELEASE" "${distdir}/ChangeLog"
    cp "RELEASE" "${distdir}/NEWS"
fi

# include version information
printf '%s\n' "${version}" >"${distdir}/version"
# include version date information
printf '%s\n' "${versiondate}" >"${distdir}/versiondate"

# --------------
# BUC.TS related
# --------------
# X60/T60: BUC.TS utility is needed to flash libreboot while Lenovo BIOS is running
# Include it statically compiled
cp -R "bucts" "bucts_/"
# make it statically compile
./oldbuild module bucts static
mkdir -p "${distdir}/bucts/${arch}/"
mv "bucts/bucts" "${distdir}/bucts/${arch}/"
rm -Rf "bucts/"
mv "bucts_/" "bucts/"

# ----------------
# Flashrom related
# ----------------
# Flashrom is used to install libreboot on supported targets
# Include it statically compiled
cp -R "flashrom/" "flashrom_/"
# make it statically compile
./oldbuild module flashrom static
mkdir -p "${distdir}/flashrom/${arch}/"
mv "flashrom/flashrom" "${distdir}/flashrom/${arch}/"
mv "flashrom/flashrom_lenovobios_sst" "${distdir}/flashrom/${arch}/"
mv "flashrom/flashrom_lenovobios_macronix" "${distdir}/flashrom/${arch}/"
rm -Rf "flashrom/"
mv "flashrom_/" "flashrom/"

 # ----------------
# cbfstool related
# ----------------
# build cbfstool, compiled (statically linked) and include the binary

mkdir -p "${distdir}/cbfstool/${arch}/"

cd "crossgcc/util/"
cp -R "cbfstool" "cbfstool_/"
cd "cbfstool/"
sed -i '/.*fmd_scanner.o.*-Wno-unused-function$/ s/$/ -Wno-sign-compare/'  Makefile.inc # build fix for latest flex version
make clean
make SHARED=0 CC='gcc -static'

mv "cbfstool" "../../../${distdir}/cbfstool/${arch}/"

if [ "${arch}" = "x86_64" ]
       then
               # Now build 32-bit binaries
               make clean
               make SHARED=0 CC='gcc -static -m32'
               mkdir "../../../${distdir}/cbfstool/i686"
               mv "cbfstool" "../../../${distdir}/cbfstool/i686/"
fi

# cross-compile for ARM
make clean
make SHARED=0 CC='arm-linux-gnueabi-gcc -static'
mkdir "../../../${distdir}/cbfstool/armv7l/"
mv "cbfstool" "../../../${distdir}/cbfstool/armv7l/"

cd ../
rm -Rf "cbfstool/"
mv "cbfstool_/" "cbfstool/"
cd ../../

# ----------------
# ich9deblob related
# ----------------
# build ich9deblob, compiled (statically linked) and include the binary

mkdir -p "${distdir}/ich9deblob/${arch}"

cd "resources/utilities/"
cp -R "ich9deblob" "ich9deblob_/"
cd "ich9deblob/"
make clean
make SHARED=0 CC='gcc -static'

mv "ich9deblob" "../../../${distdir}/ich9deblob/${arch}/"
mv "ich9gen" "../../../${distdir}/ich9deblob/${arch}/"
mv "demefactory" "../../../${distdir}/ich9deblob/${arch}/"

if [ "${arch}" = "x86_64" ]
	then
		# Now build 32-bit binaries
		make clean
		make SHARED=0 CC='gcc -static -m32'
		mkdir "../../../${distdir}/ich9deblob/i686/"
		mv "ich9deblob" "../../../${distdir}/ich9deblob/i686/"
		mv "ich9gen" "../../../${distdir}/ich9deblob/i686/"
		mv "demefactory" "../../../${distdir}/ich9deblob/i686/"
fi

# cross-compile for ARM
make clean
make SHARED=0 CC='arm-linux-gnueabi-gcc -static'
mkdir "../../../${distdir}/ich9deblob/armv7l"
mv "ich9deblob" "../../../${distdir}/ich9deblob/armv7l/"
mv "ich9gen" "../../../${distdir}/ich9deblob/armv7l/"
mv "demefactory" "../../../${distdir}/ich9deblob/armv7l/"

cd "../"
rm -Rf "ich9deblob/"
mv "ich9deblob_/" "ich9deblob/"
cd "../../"

# -----------------
# nvramtool related
# -----------------
# build nvramtool, compiled (statically linked) and include the binary
cd "crossgcc/util/"
cp -R "nvramtool/" "nvramtool_/"
cd "nvramtool/"
make clean
make SHARED=0 CC='gcc -static'

mkdir -p "../../../${distdir}/nvramtool/${arch}/"

mv "nvramtool" "../../../${distdir}/nvramtool/${arch}/"

if [ "${arch}" = "x86_64" ]
       then
               # Now build 32-bit binaries
               make clean
               make SHARED=0 CC='gcc -static -m32'
               mkdir "../../../${distdir}/nvramtool/i686/"
               mv "nvramtool" "../../../${distdir}/nvramtool/i686/"
fi

cd "../"
rm -Rf "nvramtool/"
mv "nvramtool_/" "nvramtool/"
cd "../../"

# -------------
# Miscellaneous
# -------------

# Flashing script
cp "flash" "${distdir}/"

# powertop script
cp "resources/scripts/misc/powertop.trisquel7" "${distdir}/"

printf "\n\n"

# ### Create the release tarballs
# ----------------------------------------------------------------------------------------------------------------------------

printf "Compressing %s/ into %s.tar.xz\n" "${distdir}" "${distdir}.tar.xz"
# create lzma compressed util archive
(cd "${versiondir}/" && tar -c "${distname}/" | xz -9e >"${distname}.tar.xz")

printf "done\n\n"

# ### Delete the uncompressed release directories
# ----------------------------------------------------------------------------------------------------------------------------

# The uncompressed archives are no longer needed
rm -Rf "${distdir:?}/"


code:
#!/bin/bash

#
#  helper script: build ROM images with SeaGRUB and put them in ./bin/
#
#	Copyright (C) 2014, 2015, 2016 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 git or release archive

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

printf "Building ROM images with the GRUB payload\n"

# The GRUB files should be deleted first
rm -f "coreboot/grub"*.{elf,cfg}
rm -f "coreboot/"*/*/grub*.{elf,cfg}

[ -d bin ] || mkdir "bin/"

# Put GRUB payloads and config files
# in the coreboot directory, ready for next step
(
cd "coreboot/"
for romtype in txtmode vesafb; do
	cd ../resources/utilities/grub-assemble
	./gen.sh ${romtype}
	rm -f "../../../coreboot/grub_${romtype}.elf"
	mv "grub_${romtype}.elf" "../../../coreboot/"
	cd "../../../coreboot"

	# GRUB configuration files
	for keylayout in ../resources/utilities/grub-assemble/keymap/original/*; do
		keymap="${keylayout##*/}"
		cat ../resources/grub/config/extra/{common,"${romtype}"}.cfg > "grub_${keymap}_${romtype}.cfg"
		printf "keymap %s\n" "${keymap}" >> "grub_${keymap}_${romtype}.cfg"
		cat ../resources/grub/config/menuentries/{common,"${romtype}"}.cfg >> "grub_${keymap}_${romtype}.cfg"
		# grubtest.cfg should be able to switch back to grub.cfg
		sed "s/grubtest.cfg/grub.cfg/" < "grub_${keymap}_${romtype}.cfg" > "grub_${keymap}_${romtype}_test.cfg"
	done
done
)

# Build ROM images for supported boards
buildrom() {
	board="$1"
	if [ -f "resources/libreboot/config/grub/${board}/config" ]; then
		./oldbuild roms withgrub_helper "${board}"
	fi
}

if [ $# -gt 0 ]; then
	for board in "${@}"; do
		buildrom "${board}"
	done
else
	for board in resources/libreboot/config/grub/*; do
		buildrom "${board##*/}"
	done
fi

# Needed on i945 systems for the bucts/dd trick (documented)
# This enables the ROM to be flashed over the lenovo bios firmware
for i945board in "x60" "t60"
do
	if [ -d "bin/grub/${i945board}/" ]; then
		cd "bin/grub/${i945board}/"
		for i945rom in *
		do
			dd if="${i945rom}" of=top64k.bin bs=1 skip=$[$(stat -c %s "${i945rom}") - 0x10000] count=64k
			dd if=top64k.bin of="${i945rom}" bs=1 seek=$[$(stat -c %s "${i945rom}") - 0x20000] count=64k conv=notrunc
			rm -f top64k.bin
		done
		cd "../../../"
	fi
done

# Build the deblobbed descriptor+gbe regions for GM45/ICH9M targets.
# Then put them in the ROM images.
if [ -d "bin/grub/" ]; then
	cd "bin/grub/"
	for board in "x200" "r400" "t400" "t500"
	do
		for romsize in "4m" "8m" "16m"
		do
			if [ -d "${board}_${romsize}b/" ]; then
				cd "${board}_${romsize}b/"
				if [ -f "../../../gm45macaddress" ]; then
					../../../resources/utilities/ich9deblob/ich9gen --macaddress $(cat ../../../gm45macaddress)
				else
					../../../resources/utilities/ich9deblob/ich9gen
				fi
				for rom in *.rom
				do
					dd if="ich9fdgbe_${romsize}.bin" of="${rom}" bs=1 count=12k conv=notrunc
				done
				rm -f "ich9fd"*.bin "mk"*.[ch]
				cd "../"
			fi
		done
	done
	cd ../../
fi

# The GRUB files are no longer needed
rm -f "coreboot/"*.{elf,cfg}
rm -f "coreboot/"*/*/*.{elf,cfg}

printf "\n\n"


code:
#!/bin/bash

#  helper script: create ROM images for a given system, with SeaGRUB
#
#	Copyright (C) 2014, 2015, 2016 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 git or release archive

[ "x${DEBUG+set}" = 'xset' ] && set -v
set -u -e
if [ -z ${NPROC+x} ]; then
    cores="$(nproc)"
else
    case ${NPROC} in
        ''|*[!0-9]*)
            printf "value '%s' for NPROC is invalid. non-numeric. Exiting.\n" "${NPROC}"
            exit 1
            ;;
    esac
    cores="${NPROC}"
fi

if (( $# != 1 )); then
	printf "Usage: ./oldbuild rom withgrub boardname\n"
	printf "Example: ./oldbuild rom withgrub x60\n"
	printf "You need to specify exactly 1 argument\n"
	exit 1
fi

boardtarget="${1}"

if [ -f "version" ]; then
	# release archive is being used
	version="$(cat version)"
else
	# git repo is being used
	version="$(git describe --tags HEAD)"
fi

printf "GRUB Helper script: build ROM images for '%s'\n" "${boardtarget}"

(
cbrevision="$(cat resources/libreboot/config/grub/${boardtarget}/cbrevision)"
vbootrevision="$(cat resources/libreboot/config/grub/${boardtarget}/vbootrevision)"
branchname="grub_${boardtarget}"

cd "coreboot/${cbrevision}/${cbrevision}/"
git checkout ${branchname}
(
    cd "3rdparty/vboot/"
    git checkout ${branchname}
)

# Make sure to remove these first
rm -f *.{elf,cfg}

printf 'libreboot-%s\n' "${version}" > ".coreboot-version" # needed for reproducible builds in coreboot

# Build ROM images with text-mode and corebootfb modes.

if [ "${boardtarget}" = "qemu_i440fx_piix4" ] || [ "${boardtarget}" = "qemu_q35_ich9" ]
then
	# assume that the default config enable framebuffer mode, duplicate and patch for text-mode
	# necessary, otherwise it will ask you to enter the Y/X resolution of the framebuffer at build time
	cp "../../../resources/libreboot/config/grub/${boardtarget}/config" "config_vesafb"
	sed 's/CONFIG_FRAMEBUFFER_KEEP_VESA_MODE=y/# CONFIG_FRAMEBUFFER_KEEP_VESA_MODE is not set/' < "config_vesafb" > "config_txtmode"
else
	# assume that the default config enables text-mode, duplicate and patch for framebuffer mode
	cp "../../../resources/libreboot/config/grub/${boardtarget}/config" "config_txtmode"
	sed 's/# CONFIG_FRAMEBUFFER_KEEP_VESA_MODE is not set/CONFIG_FRAMEBUFFER_KEEP_VESA_MODE=y/' < "config_txtmode" > "config_vesafb"
fi

for romtype in txtmode vesafb
do
	if [ "${boardtarget}" = "kgpe-d16" ] || [ "${boardtarget}" = "kcma-d8" ] || [ "${boardtarget}" = "d510mo" ]; then
		if [ "${romtype}" = "vesafb" ]; then
			printf "Only text-mode is reported to work on KGPE-D16, KCMA-D8, D510MO and ga-g41m-es2l\n"
			printf "TODO: get tpearson to fix it\n"
			continue
		fi
	fi

	# Build coreboot ROM image
	make clean
	mv "config_${romtype}" ".config"
	cp "../../grub_${romtype}.elf" "grub.elf"
    if [ "${boardtarget}" = "d510mo" ] || [ "${boardtarget}" = "ga-g41m-es2l" ]; then
        # Do not use SeaGRUB
        cp grub.elf payload.elf
	    make -j${cores}
        mv "build/coreboot.rom" "${boardtarget}_${romtype}.rom"

        # We dont need seabios.elf anymore
        rm -f "payload.elf"
    else
        # Use SeaGRUB
        cp "../../../seabios/out/bios.bin.elf" "payload.elf"
	    make -j${cores}

	    mv "build/coreboot.rom" "${boardtarget}_${romtype}.rom"

        # We dont need seabios.elf anymore
         rm -f "payload.elf"
        # Add the grub.elf to CBFS
        ./util/cbfstool/cbfstool "${boardtarget}_${romtype}.rom" add-payload -c lzma -f grub.elf -n img/grub2

        # Set bootorder so that seabios loads grub by default
        printf "/rom@img/grub2\n" > bootorder
        ./util/cbfstool/cbfstool "${boardtarget}_${romtype}.rom" add -f bootorder -n bootorder -t raw
        rm -f bootorder

        # Add 0s delay to seabios, so that the user is not burdened by seeing
        # that horrible interface that seabios has
        ./util/cbfstool/cbfstool "${boardtarget}_${romtype}.rom" add-int -i 0 -n etc/show-boot-menu || "already exists"

        # Add a file to CBFS that tells SeaBIOS not to load option ROMs
        ./util/cbfstool/cbfstool "${boardtarget}_${romtype}.rom" add-int -i 0 -n etc/pci-optionrom-exec || "already exists"

        # keyboard spinup timeout
       ./util/cbfstool/cbfstool "${boardtarget}_${romtype}.rom" remove -n etc/ps2-keyboard-spinup || printf "does not exist"
       ./util/cbfstool/cbfstool "${boardtarget}_${romtype}.rom" add-int -i 3000 -n etc/ps2-keyboard-spinup || printf "already exists"
    fi

    printf "%s\n" "${version}" > lbversion
    ./util/cbfstool/cbfstool "${boardtarget}_${romtype}.rom" add -f lbversion -n lbversion -t raw
    rm -f "lbversion"

    # we dont need that grub.elf now
    rm -f "grub.elf"

	# .config no longer needed
	rm -f ".config"

	# Add the background image
	if [ "$romtype" = "vesafb" ]
	then
		if [ "$1" = "macbook21" ] || [ "$1" = "x200_4mb" ] || [ "$1" = "x200_8mb" ] || [ "$1" = "x200_16mb" ] || [ "$1" = "r400_4mb" ] || [ "$1" = "r400_8mb" ] || [ "$1" = "r400_16mb" ] || [ "$1" = "t400_4mb" ] || [ "$1" = "t400_8mb" ] || [ "$1" = "t400_16mb" ] || [ "$1" = "t500_4mb" ] || [ "$1" = "t500_8mb" ] || [ "$1" = "t500_16mb" ]
		then
			./util/cbfstool/cbfstool "${boardtarget}_${romtype}.rom" add -f ../../../resources/grub/background/background1280x800.png -n background.png -t raw
		else
			./util/cbfstool/cbfstool "${boardtarget}_${romtype}.rom" add -f ../../../resources/grub/background/background1024x768.png -n background.png -t raw
		fi
	fi

	# Add the correct GRUB configuration file for this image.
	for keylayout in ../../../resources/utilities/grub-assemble/keymap/original/*
	do
		keymap="${keylayout##*/}"

		# copy the images based on the keymap
		cp "${boardtarget}_${romtype}.rom" "${boardtarget}_${keymap}_${romtype}.rom"

		# Insert grub config into the image
		./util/cbfstool/cbfstool "${boardtarget}_${keymap}_${romtype}.rom" add -f ../../"grub_${keymap}_${romtype}.cfg" -n grub.cfg -t raw
		# Insert grub test config into the image (for the user to test modifications to before modifying the main one)
		./util/cbfstool/cbfstool "${boardtarget}_${keymap}_${romtype}.rom" add -f ../../"grub_${keymap}_${romtype}_test.cfg" -n grubtest.cfg -t raw
	done

	# This config-less ROM is no longer needed
	rm -f "${boardtarget}_${romtype}.rom"
done

# Clean up and prepare bin/ containing all ROM images

# move ROM images into the newly created directory
rm -Rf "${boardtarget:?}/"
mkdir "${boardtarget}/"
mv "${boardtarget}"*.rom "${boardtarget}/"

# delete old ROM images
rm -Rf "../../../bin/grub/${boardtarget}/"
# put new ROM images in ../bin/grub/
[ -d "../../../bin/grub/" ] || mkdir -p "../../../bin/grub/"
mv "${boardtarget}/" "../../../bin/grub/"

# version info file no longer needed
rm -f ".coreboot-version"

git checkout master
(
    cd "3rdparty/vboot/"
    git checkout master
)
)

printf "\n\n"


code:
#!/bin/bash

#
#  helper script: build ROM images with SeaBIOS and put them in ./bin/seabios/
#
#	Copyright (C) 2014, 2015, 2016 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 git or release archive

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

printf "Building ROM images with the SeaBIOS payload\n"

[ -d bin ] || mkdir "bin/"

# Build ROM images for supported boards
buildrom() {
	board="$1"
	if [ -f "resources/libreboot/config/seabios/${board}/config" ]; then
		./oldbuild roms withseabios_helper "${board}"
	fi
}

if [ $# -gt 0 ]; then
	for board in "${@}"; do
		buildrom "${board}"
	done
else
	for board in resources/libreboot/config/seabios/*; do
		buildrom "${board##*/}"
	done
fi

# The SeaBIOS files are no longer needed
rm -f coreboot/*.{elf,cfg}
rm -f coreboot/*/*/*.{elf,cfg}

printf "\n\n"


code:
#!/bin/bash

#  helper script: create ROM images for a given system, with SeaBIOS
#
#	Copyright (C) 2014, 2015, 2016 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Klemens Nanni <contact@autoboot.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 git or release archive

[ "x${DEBUG+set}" = 'xset' ] && set -v
set -u -e
if [ -z ${NPROC+x} ]; then
    cores="$(nproc)"
else
    case ${NPROC} in
        ''|*[!0-9]*)
            printf "value '%s' for NPROC is invalid. non-numeric. Exiting.\n" "${NPROC}"
            exit 1
            ;;
    esac
    cores="${NPROC}"
fi

if (( $# != 1 )); then
	printf "Usage: ./oldbuild rom withseabios boardname\n"
	printf "Example: ./oldbuild rom withseabios x60\n"
	printf "You need to specify exactly 1 argument\n"
	exit 1
fi

boardtarget="${1}"

if [ -f "version" ]; then
	# release archive is being used
	version="$(cat version)"
else
	# git repo is being used
	version="$(git describe --tags HEAD)"
fi

printf "SeaBIOS Helper script: build ROM images for '%s'\n" "${boardtarget}"

(
cbrevision="$(cat resources/libreboot/config/seabios/${boardtarget}/cbrevision)"
vbootrevision="$(cat resources/libreboot/config/seabios/${boardtarget}/vbootrevision)"
branchname="seabios_${boardtarget}"

cd "coreboot/${cbrevision}/${cbrevision}/"
git checkout ${branchname}
(
    cd "3rdparty/vboot/"
    git checkout ${branchname}
)

# Make sure to remove these first
rm -f *{elf,cfg}

printf 'libreboot-%s\n' "${version}" > ".coreboot-version" # needed for reproducible builds in coreboot

# Build ROM images with text-mode and corebootfb modes.

for romtype in txtmode vesafb
do
	if [ "${boardtarget}" = "kgpe-d16" ] || [ "${boardtarget}" = "kcma-d8" ] || [ "${boardtarget}" = "d945gclf" ]; then
		if [ "${romtype}" = "vesafb" ]; then
			printf "Only text-mode is reported to work on KGPE-D16, KCMA-D8, d945gclf\n"
			continue
		fi
	fi

    if [ "${boardtarget}" = "qemu_i440fx_piix4" ] || [ "${boardtarget}" = "qemu_q35_ich9" ]
    then
    	# assume that the default config enable framebuffer mode, duplicate and patch for text-mode
    	# necessary, otherwise it will ask you to enter the Y/X resolution of the framebuffer at build time
    	cp "../../../resources/libreboot/config/seabios/${boardtarget}/config" "config_vesafb"
    	sed 's/CONFIG_FRAMEBUFFER_KEEP_VESA_MODE=y/# CONFIG_FRAMEBUFFER_KEEP_VESA_MODE is not set/' < "config_vesafb" > "config_txtmode"
    else
    	# assume that the default config enables text-mode, duplicate and patch for framebuffer mode
    	cp "../../../resources/libreboot/config/seabios/${boardtarget}/config" "config_txtmode"
    	sed 's/# CONFIG_FRAMEBUFFER_KEEP_VESA_MODE is not set/CONFIG_FRAMEBUFFER_KEEP_VESA_MODE=y/' < "config_txtmode" > "config_vesafb"
    fi

	# Build coreboot ROM image
	make clean
	cp "../../../seabios/out/bios.bin.vga.elf" "payload.elf"
    mv "config_${romtype}" .config
    make -j${cores}
	rm -f "payload.elf"
	mv "build/coreboot.rom" "${boardtarget}_${romtype}.rom"

    # Now add SeaVGABIOS (SeaBIOS wrapper for coreboot native video init)
    ./util/cbfstool/cbfstool "${boardtarget}_${romtype}.rom" add -f ../../../seabios/out/vgabios.bin -n vgaroms/vgabios.bin -t raw

    # Add a file to CBFS that tells SeaBIOS not to load option ROMs
    ./util/cbfstool/cbfstool "${boardtarget}_${romtype}.rom" add-int -i 0 -n etc/pci-optionrom-exec || "already exists"

	# .config no longer needed
	rm -f ".config"
done

# Clean up and prepare bin/ containing all ROM images

# move ROM images into the newly created directory
rm -Rf "${boardtarget:?}/"
mkdir "${boardtarget}/"
mv "${boardtarget}"*.rom "${boardtarget}/"

# delete old ROM images
rm -Rf "../../../bin/seabios/${boardtarget}/"
# put new ROM images in ../bin/grub/
[ -d "../../../bin/seabios/" ] || mkdir -p "../../../bin/seabios/"
mv "${boardtarget}/" "../../../bin/seabios/"

# version info file no longer needed
rm -f ".coreboot-version"

git checkout master
(
    cd "3rdparty/vboot/"
    git checkout master
)
)

printf "\n\n"


code:
#!/bin/bash

#  helper script: downloads bucts and patches it
#
#	Copyright (C) 2014, 2015 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 libreboot_src or libreboot git.

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

# Get bucts and checkout last revision used

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

printf "Downloading bucts\n"

rm -Rf "bucts/"

# Get bucts
# ------------------------------------------------------------------------------

# download it using git
git clone git://git.stuge.se/bucts.git

# modifications are required
cd "bucts/"

# Reset to the last commit that was tested
# ------------------------------------------------------------------------------

printf "Patching bucts to not require git for building\n"
git reset --hard dc27919d7a66a6e8685ce07c71aefa4f03ef7c07

# Apply patches
# ------------------------------------------------------------------------------

git am "../resources/bucts/patch/0001-Makefile-don-t-use-git.patch"

# we're done
cd "../"

printf "\n\n"


code:
#!/bin/bash

#  helper script: downloads coreboot and patches/deblobs it
#
#	Copyright (C) 2014, 2015, 2016 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 libreboot_src or libreboot git.

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

if [ -f "version" ]; then
	# _src release archive is being used
	version="libreboot-$(cat version)"
else
	# git repo is being used
	version="libreboot-$(git describe --tags HEAD)"
fi

make_coreboot_src_directory() {
    payload="${1}"
    cbrevision="${2}"
    vbootrevision="${3}"
    firmwarepath="${4}" # libreboot_src/coreboot/
(
    cd "${firmwarepath}/"
    # copy coreboot directory there
    rm -Rf "${payload:?}/${cbrevision:?}/"
    if [ ! -d "${payload}/" ]; then
        mkdir -p "${payload}/"
    fi
    cp -R "coreboot/" "${payload}/${cbrevision}/"

    # We need to reset that revision of coreboot to the specified revision
    cd "${payload}/${cbrevision}/"
    git reset --hard ${cbrevision}

    if [ "${vbootrevision}" != "no_vboot_revision" ]; then
        # reset vboot revision. in practise, we always use the same
        # vboot revision on all boards that use a given coreboot revision
        cd "3rdparty/vboot/"
        git reset --hard ${vbootrevision}
    fi
)
}

printf "Downloading coreboot, patching coreboot and deblobbing coreboot\n"

# This grabs current base used, and applies patches
# This is also used to run the deblob scripts.

# 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

# there are modifications required
cd "coreboot/"

# Define a common version (based on the libreboot version)
# Most likely redundant, because the build system needs to update
# this every time when building a ROM image anyway
printf '%s\n' "${version}" >".coreboot-version"

# vboot submodule is needed
# ------------------------------------------------------------------------------

git submodule update --init --checkout -- 3rdparty/vboot/

# Create branches, with patches in each branch
# Create separate coreboot source directories *for each board*
# ------------------------------------------------------------------------------

for payloads in ../../resources/libreboot/config/*; do

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

	payload="${payloads##*/}"

	for boardconfig in ../../resources/libreboot/config/${payload}/*; do

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

		cbrevision=$(cat "${boardconfig}/cbrevision")
		vbootrevision=$(cat "${boardconfig}/vbootrevision")

        # the same vboot revision is always used for coreboot revision,
        # so we don't need to wworry about checking for that here

        if [ -d "../${cbrevision}/${cbrevision}" ]; then
            continue
            # the directory already exists, no need to recreate it
        fi

		make_coreboot_src_directory ${cbrevision} ${cbrevision} ${vbootrevision} ..
	done
done

# go back to _src/coreboot/ (containing all coreboot directories)
cd "../"
# delete the gitted one (not needed anymore)
rm -Rf "coreboot/"

# Run coreboot-libre deblob scripts
# ------------------------------------------------------------------------------

printf "Deleting .git* in coreboot/ (history inside .git contains the blobs that were deleted)\n"
rm -Rf */*/.git*
rm -Rf */*/3rdparty/*/.git*

# Delete crossgcc from non-crossgcc coreboot archives
# (the build system will create symlinks later when building the ROM images)
for payload in *; do
	if [ "${payload##*/}" != "crossgcc" ]; then
		rm -Rf ${payload:?}/*/util/crossgcc/
	fi
done
cd "../"

printf "Deblobbing coreboot\n"
./resources/utilities/coreboot-libre/deblob

printf "\n\n"