# Copyright 1999-2017 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

# https://aur.archlinux.org/packages/ue/uefi-shell-svn/PKGBUILD referenced for build

EAPI="6"

inherit eutils mount-boot

if [[ ${PV} != 9999 ]]; then
	MY_PV="vUDK${PV}"
	KEYWORDS="amd64 x86"
	SRC_URI="https://github.com/tianocore/edk2/archive/${MY_PV}.tar.gz"
	S="${WORKDIR}/edk2-${MY_PV}"
else
	inherit git-r3
	EGIT_REPO_URI="https://github.com/tianocore/edk2"
	EGIT_BRANCH="master"
	KEYWORDS=""
fi

DESCRIPTION="UEFI Shell"
HOMEPAGE="http://www.tianocore.org/"

LICENSE="BSD-2"
SLOT="0"
IUSE="shell32 +shell64"

DEPEND="dev-lang/python:2.7
	dev-lang/nasm
	sys-power/iasl"
RDEPEND=""

_setup_env_vars() {
	einfo "Setup UDK PATH ENV variables"
	export _UDK_DIR="${S}"
	export EDK_TOOLS_PATH="${_UDK_DIR}/BaseTools"

	export _TIANOCORE_PKG="Shell"
	export _UDK_TARGET="${_TIANOCORE_PKG}Pkg/${_TIANOCORE_PKG}Pkg.dsc"
	export _TIANOCORE_TARGET="RELEASE"
	export _COMPILER="GCC5"

	[[ "${ARCH}" == "amd64" ]] && export _TIANO_ARCH="X64"
	[[ "${ARCH}" == "x86" ]] && export _TIANO_ARCH="IA32"

	[[ "${ARCH}" == "amd64" ]] && export _TIANO_S_ARCH="x64"
	[[ "${ARCH}" == "x86" ]] && export _TIANO_S_ARCH="ia32"
}

src_prepare() {
	_setup_env_vars
	default

	sed 's|--gc-sections|--gc-sections -Wl,--build-id=none|g' -i "${EDK_TOOLS_PATH}/Conf/tools_def.template"
	einfo "Use python2 for UDK BaseTools"
	sed 's|python |python2 |g' -i "${EDK_TOOLS_PATH}/BinWrappers/PosixLike"/* || true
	sed 's|python |python2 |g' -i "${EDK_TOOLS_PATH}/Tests/GNUmakefile"

	einfo "Fix GCC Warning as error"
	sed 's|-Werror |-Wno-error -Wno-unused-but-set-variable |g' -i "${EDK_TOOLS_PATH}/Source/C/Makefiles/header.makefile" || true
	sed 's|-Werror |-Wno-error -Wno-unused-but-set-variable |g' -i "${EDK_TOOLS_PATH}/Conf/tools_def.template" || true

	einfo "Fix GCC >=4.7 error - gcc: error: unrecognized command line option '-melf_x86_64'"
	sed 's| -m64 --64 -melf_x86_64| -m64|g' -i "${EDK_TOOLS_PATH}/Conf/tools_def.template" || true
	sed 's|--64 | |g' -i "${EDK_TOOLS_PATH}/Conf/tools_def.template" || true
	sed 's| -m64 -melf_x86_64| -m64|g' -i "${EDK_TOOLS_PATH}/Conf/tools_def.template" || true

	einfo "Remove GCC -g debug option and add -O0 -mabi=ms -maccumulate-outgoing-args"
	sed 's|DEFINE GCC_ALL_CC_FLAGS            = -g |DEFINE GCC_ALL_CC_FLAGS            = -O0 -mabi=ms -maccumulate-outgoing-args |g' -i "${EDK_TOOLS_PATH}/Conf/tools_def.template" || true
	sed 's|DEFINE GCC44_ALL_CC_FLAGS            = -g |DEFINE GCC44_ALL_CC_FLAGS            = -O0 -mabi=ms -maccumulate-outgoing-args |g' -i "${EDK_TOOLS_PATH}/Conf/tools_def.template" || true

	einfo "Fix UDK Target Platform"
	sed "s|ACTIVE_PLATFORM       = Nt32Pkg/Nt32Pkg.dsc|ACTIVE_PLATFORM       = ${_UDK_TARGET}|g" -i "${EDK_TOOLS_PATH}/Conf/target.template" || true
	sed "s|TARGET                = DEBUG|TARGET                = ${_TIANOCORE_TARGET}|g" -i "${EDK_TOOLS_PATH}/Conf/target.template" || true
	sed "s|TOOL_CHAIN_TAG        = MYTOOLS|TOOL_CHAIN_TAG        = ${_COMPILER}|g" -i "${EDK_TOOLS_PATH}/Conf/target.template" || true
	sed "s|IA32|X64|g" -i "${EDK_TOOLS_PATH}/Conf/target.template" || true

	chmod 0755 "${_UDK_DIR}/BaseTools/BuildEnv"
}

src_compile() {
	_setup_env_vars
	einfo "Unset all compiler FLAGS"
	unset CFLAGS
	unset CPPFLAGS
	unset CXXFLAGS
	unset LDFLAGS
	unset MAKEFLAGS

	einfo "Setup UDK Environment"
	source "${_UDK_DIR}/BaseTools/BuildEnv" BaseTools

	einfo "Compile UDK BaseTools"
	emake -j1 -C "${EDK_TOOLS_PATH}" ARCH=${_TIANO_ARCH}

	einfo "Unset all compiler FLAGS"
	unset CFLAGS
	unset CPPFLAGS
	unset CXXFLAGS
	unset LDFLAGS
	unset MAKEFLAGS

	einfo "Compile UEFI Shell v2 ${_TIANO_ARCH} binary"
	"${EDK_TOOLS_PATH}/BinWrappers/PosixLike/build" -p "${_UDK_TARGET}" -a "${_TIANO_ARCH}" -b "${_TIANOCORE_TARGET}" -t "${_COMPILER}"

	if [[ "${ARCH}" == "amd64" ]] && use shell32; then
		einfo "Unset all compiler FLAGS"
		unset CFLAGS
		unset CPPFLAGS
		unset CXXFLAGS
		unset LDFLAGS
		unset MAKEFLAGS

		einfo "Compile UEFI Shell v2 IA32 binary"
		"${EDK_TOOLS_PATH}/BinWrappers/PosixLike/build" -p "${_UDK_TARGET}" -a "IA32" -b "${_TIANOCORE_TARGET}" -t "${_COMPILER}"
	fi
}

src_install() {
	_setup_env_vars

	einfo "Install the UEFI Shell v2 ${_TIANO_ARCH} binary"
	insinto /boot/efi
	newins "${_UDK_DIR}/Build/${_TIANOCORE_PKG}/${_TIANOCORE_TARGET}_${_COMPILER}/${_TIANO_ARCH}/Shell.efi" shellx64.efi

	if [[ "${ARCH}" == "amd64" ]] && use shell32; then
		einfo "Install the UEFI Shell v2 IA32 binary"
		newins "${_UDK_DIR}/Build/${_TIANOCORE_PKG}/${_TIANOCORE_TARGET}_${_COMPILER}/IA32/Shell.efi" shellia32.efi
	fi
}