#!/bin/bash

#
#  helper script: generate the release archives
#
#	Copyright (C) 2014, 2015 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 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

printf "Generating the release archives\n"

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

# ### Version information for this release
# ----------------------------------------------------------------------------------------------------------------------------

# create file showing the commit ID from git for this archive.
cat .git/refs/heads/master > commitid

# ### Delete old archives before continuing
# ----------------------------------------------------------------------------------------------------------------------------

printf "Deleting the old release archives\n"
rm -f libreboot_*.tar.xz
rm -f tobuild.tar.xz
rm -Rf release/
rm -Rf tobuild/

# ### Create the release/ directory where the archives will go
# ----------------------------------------------------------------------------------------------------------------------------

# create directory for the release
mkdir release/

# ### Prepare libreboot_util archive ready for release
# ----------------------------------------------------------------------------------------------------------------------------

printf "Generating libreboot_util.tar.xz\n"

mkdir libreboot_util

# --------------
# 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
./build module bucts static
mkdir libreboot_util/bucts/
mkdir libreboot_util/bucts/"$arch"/
mv bucts/bucts libreboot_util/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
./build module flashrom static
mkdir libreboot_util/flashrom
cd flashrom/
mkdir ../libreboot_util/flashrom/"$arch"
mv flashrom ../libreboot_util/flashrom/"$arch"
mv flashrom_lenovobios_sst ../libreboot_util/flashrom/"$arch"
mv flashrom_lenovobios_macronix ../libreboot_util/flashrom/"$arch"
cd ../
rm -Rf flashrom
mv flashrom_ flashrom

# ----------------
# cbfstool related
# ----------------
# build cbfstool, compiled (statically linked) and include the binary
cd coreboot/util/
cp -R cbfstool cbfstool_
cd cbfstool/
make clean
make SHARED=0 CC='gcc -static'
mkdir ../../../libreboot_util/cbfstool

mkdir ../../../libreboot_util/cbfstool/"$arch"
mv cbfstool ../../../libreboot_util/cbfstool/"$arch"/

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

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

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

# ----------------
# ich9deblob related
# ----------------
# build ich9deblob, compiled (statically linked) and include the binary
cd resources/utilities/
cp -R ich9deblob ich9deblob_
cd ich9deblob/
make clean
make SHARED=0 CC='gcc -static'
mkdir ../../../libreboot_util/ich9deblob

mkdir ../../../libreboot_util/ich9deblob/"$arch"
mv ich9deblob ../../../libreboot_util/ich9deblob/"$arch"/
mv ich9gen ../../../libreboot_util/ich9deblob/"$arch"/
mv demefactory ../../../libreboot_util/ich9deblob/"$arch"/

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

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

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

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

mkdir ../../../libreboot_util/nvramtool/"$arch"
mv nvramtool ../../../libreboot_util/nvramtool/"$arch"/

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

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

# -----------------
# cbmem related
# -----------------
# build cbmem, compiled (statically linked) and include the binary
cd coreboot/util/
cp -R cbmem cbmem_
cd cbmem/
make clean
make SHARED=0 CC='gcc -static'
mkdir ../../../libreboot_util/cbmem

mkdir ../../../libreboot_util/cbmem/"$arch"
mv cbmem ../../../libreboot_util/cbmem/"$arch"/

if [ "$arch" = "x86_64" ]
	then
		# Now build 32-bit binaries
		make clean
		make SHARED=0 CC='gcc -static -m32'
		mkdir ../../../libreboot_util/cbmem/i686
		mv cbmem ../../../libreboot_util/cbmem/i686/
fi
cd ../
rm -Rf cbmem
mv cbmem_ cbmem
cd ../../

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

# Flashing script
cp flash libreboot_util/

# for changing the GRUB background
cp resources/scripts/misc/grub-background libreboot_util/

printf "\n\n"

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

# mention the commit ID (libreboot, git) in the release archives
cat commitid > libreboot_util/commitid

printf "Compressing libreboot_util/ into libreboot_util.tar.xz\n"
# create lzma compressed util archive
tar -c libreboot_util | xz -9e >libreboot_util.tar.xz

# Move the archives to the release directory
mv libreboot_util.tar.xz release/
# Put the change log in the release directory.
cp docs/release.html release/
cp docs/archive_old.html release/
cp docs/cc-by-sa-4.txt release/
cp -R docs/css/ release/

# Create a symlink to the commitid file, in release/
cat commitid > release/commitid

printf "\n\n"

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

# The uncompressed archives are no longer needed
rm -Rf libreboot_util

# For those utilities that have to be built on the target
./build external source

# Move the archive to the release directory
mv tobuild.tar.xz release/

printf "Tar archives are stored in release/.\n"
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 tobuild.tar.xz has been created with everything needed to build these utilities.\n\n"

# create sha512sum manifest
cd release/
printf "Creating manifest of SHA512 sums in sha512sum.txt...\n"
for file in $(find -type f); do sha512sum $file >> sha512sum.txt; done
printf "...done.\n"
cd ../

printf "\n\n"

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