#!/bin/sh

#
#  helper script: generate the source release archive
#
#	Copyright (C) 2014, 2015 Francis Rowe <info@gluglug.org.uk>
#	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)"
else
	# git repo is being used
	version="$(git describe --tags HEAD)"
fi
versiondir="release/${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
	case "${resource}" in
		release | docs);;
		*) cp -R "${resource}" "${distdir}/";;
	esac
done

printf '%s\n' "${version}" >"${distdir}/version"

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

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

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

# 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. Trim scripts also not needed, because
# the sources in the _src release archive are already trimmed.
rm -f "${distdir}/download"
rm -Rf "${distdir}/resources/scripts/helpers/download/"
rm -Rf "${distdir}/resources/scripts/helpers/build/trim/"

# The release archive for _src does not include docs, so it
# is nonsense to include the script for it there
rm -f "${distdir}/resources/scripts/helpers/build/release/docs"

# Patches are not needed, because they are already applied to coreboot sources.
rm -Rf "${distdir}/resources/libreboot/patch/"

# coreboot's crossgcc archives are distributed separately.
rm -Rf "${distdir}/coreboot/util/crossgcc/tarballs/"

# ich9deblob: There are certain files in there that the user most likely does
# not want to share.
rm -f "${distdir}/resources/utilities/ich9deblob/deblobbed_descriptor.bin"
rm -f "${distdir}/resources/utilities/ich9deblob/factory.rom"
rm -f "${distdir}/resources/utilities/ich9deblob/libreboot.rom"
rm -f "${distdir}/resources/utilities/ich9deblob/mkdescriptor.c"
rm -f "${distdir}/resources/utilities/ich9deblob/mkdescriptor.h"
rm -f "${distdir}/resources/utilities/ich9deblob/mkgbe.c"
rm -f "${distdir}/resources/utilities/ich9deblob/mkgbe.h"
rm -f "${distdir}/resources/utilities/ich9deblob/ich9fdgbe_4m.bin"
rm -f "${distdir}/resources/utilities/ich9deblob/ich9fdgbe_8m.bin"
rm -f "${distdir}/resources/utilities/ich9deblob/demefactory_4kdescriptor.bin"
rm -f "${distdir}/mkgbe.c"
rm -f "${distdir}/mkgbe.h"
rm -f "${distdir}/ich9fdgbe_8m.bin"
rm -f "${distdir}/ich9fdgbe_4m.bin"

# 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}"
