#!/bin/bash

#  getcb script: downloads coreboot and patches/deblobs it
#
#	Copyright (C) 2014 Francis Rowe <info@gluglug.org.uk>
#
#    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 grabs current base used, and applies patches
# This is also used to run deblob scripts.

# (the idea is that this script will reproduce the coreboot directory included with this version of libreboot)

# You need the dependencies from ./builddeb or ./buildpac to use this script.

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

echo "DOWNLOADING AND PATCHING COREBOOT"

rm -rf coreboot

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

# download it using git
git clone http://review.coreboot.org/coreboot

# there are modifications required
cd coreboot

# reset to previously tested revision
git reset --hard 23aad4a83c3390dc39f7d1c1f5422f7ac54a80f3

# Get patches from review.coreboot.org
# ----------------------------------------------------------------------------------

# Text mode patch for X60 native graphics (main patch already merged in coreboot. See 6723 on coreboot gerrit)
git fetch http://review.coreboot.org/coreboot refs/changes/25/6725/1 && git cherry-pick FETCH_HEAD

# Backlight controls on i945 (X60/T60/macbook21)
git fetch http://review.coreboot.org/coreboot refs/changes/31/6731/6 && git cherry-pick FETCH_HEAD

# Enable T60 native graphics
git fetch http://review.coreboot.org/coreboot refs/changes/45/5345/7 && git cherry-pick FETCH_HEAD

# Apply necessary patches (from resources/libreboot/patch/)
# ---------------------------------------------------------------------------------

# look at resources/libreboot/patch/gitdiff to see what the whole patch looks like when all of these are applied.

# copy all the patches here for this release
cp ../resources/libreboot/patch/0000_t60_6723_and_6725_1_extension_textmode.git.diff .
cp ../resources/libreboot/patch/0001_t60_6731_6_extension_acpi_brightness.git.diff .
cp ../resources/libreboot/patch/0002_i945_6731_6_extension_fix_uneven_backlight.git.diff .

# Add text-mode for T60 (enables text-mode payloads like memtest, seabios, grub invaders, etc)
# Based on 6723 (merged) and 6725/1 from coreboot gerrit site (review.coreboot.org)
git apply 0000_t60_6723_and_6725_1_extension_textmode.git.diff

# Cleanup patch for T60 ACPI backlight controls, based on 6731/6 from coreboot gerrit site (review.coreboot.org)
# Won't commit to coreboot for this release, since 6731 is currently -2'd in gerrit (on 20140824) and will be improved.
git apply 0001_t60_6731_6_extension_acpi_brightness.git.diff

# Fix uneven backlight (also removes accoustic noise from (most likely) the inverter board underneath lcd, when reducing brightness)
# Won't commit to coreboot for this release, since 6731 is currently -2'd in gerrit (on 20140824) and will be improved.
git apply 0002_i945_6731_6_extension_fix_uneven_backlight.git.diff

# delete the copies of the patches (not needed anymore)
rm -rf 0000_t60_6723_and_6725_1_extension_textmode.git.diff
rm -rf 0001_t60_6731_6_extension_acpi_brightness.git.diff
rm -rf 0002_i945_6731_6_extension_fix_uneven_backlight.git.diff

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

# Apply coreboot-libre deblob script for coreboot git revision 63acd22dc5366c72a7165138f5030df9523824dc
# TODO: implement this.

# Deblobbing was done manually for this pre-release (will re-tool linux-libre deblob scripts later):
cd ../
echo "deblobbing coreboot"
./DEBLOB
# The git history (git diff command) shows what blobs were deleted (including the blobs themselves) which is a freedom issue. Just delete .git altogether:
cd coreboot
rm -rf .git
rm -rf .gitreview
rm -rf .gitmodules
rm -rf .gitignore
cd ../
echo "finished deblobbing coreboot"

# we're done
echo "FINISHED DOWNLOADING AND PATCHING COREBOOT"

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


