# Maintainer (Hyperbola): André Silva <emulatorman@hyperbola.info>
# Maintainer: David P. <megver83@parabola.nu>

# based in Hyperbola's edk2-uefi-shell, but without i686/IA32
# support and with UEFI Shell v1 included

pkgname=uefi-shell
pkgver=201811
pkgrel=1
_srcname=edk2-stable$pkgver
pkgdesc='Tianocore UEFI Shell v1 and v2'
url='https://github.com/tianocore/edk2'
arch=(x86_64)
license=(BSD)
makedepends=(python2 nasm gnu-efi-libs subversion)
options=(!strip !makeflags)
source=(https://github.com/tianocore/edk2/archive/$_srcname.tar.gz
        svn+https://svn.code.sf.net/p/efi-shell/code/trunk/Shell)
sha512sums=('103cb503b73a57996b4ea7c941cb788ebf276e0f4b55480acf2080a632e956d27b0cf4b2a9cb41bac04e1c6aecf38d51c4b49432908e2fcbe6e10ced94d01573'
            'SKIP')

prepare() {
  cd $srcdir/edk2-$_srcname

  msg 'Cleanup UDK config files'
  rm -rf Build
  rm -rf Conf
  mkdir -p Conf
  mkdir -p Build

  msg 'Delete bogus dependency files'
  find . -name '*.d' -delete

  msg 'Disable build ID generation'
  sed -i 's|,--gc-sections|,--gc-sections,--build-id=none|g' BaseTools/Conf/tools_def.template

  msg 'Use python2 for UDK BaseTools'
  sed -i 's|python |python2 |g' BaseTools/BinWrappers/PosixLike/*
  sed -i 's|python |python2 |g' BaseTools/Tests/GNUmakefile

  msg 'Fix GCC Warning as error'
  sed -i 's|-Werror |-Wno-error -Wno-unused-but-set-variable |g' BaseTools/Source/C/Makefiles/header.makefile
  sed -i 's|-Werror |-Wno-error -Wno-unused-but-set-variable |g' BaseTools/Conf/tools_def.template

  msg 'Remove GCC -g debug option and add -O0 -mabi=ms -maccumulate-outgoing-args'
  sed -i 's|DEFINE GCC_ALL_CC_FLAGS            = -g |DEFINE GCC_ALL_CC_FLAGS            = -O0 -mabi=ms -maccumulate-outgoing-args |g' BaseTools/Conf/tools_def.template
  sed -i 's|DEFINE GCC44_ALL_CC_FLAGS            = -g |DEFINE GCC44_ALL_CC_FLAGS            = -O0 -mabi=ms -maccumulate-outgoing-args |g' BaseTools/Conf/tools_def.template

  msg 'Fix UDK Target Platform'
  sed -i 's|ACTIVE_PLATFORM       = Nt32Pkg/Nt32Pkg.dsc|ACTIVE_PLATFORM       = EdkShellPkg/EdkShellPkg.dsc|g' BaseTools/Conf/target.template
  sed -i 's|TARGET                = DEBUG|TARGET                = RELEASE|g' BaseTools/Conf/target.template
  sed -i 's|TOOL_CHAIN_TAG        = MYTOOLS|TOOL_CHAIN_TAG        = GCC5|g' BaseTools/Conf/target.template
  sed -i 's|IA32|X64|g' BaseTools/Conf/target.template

  chmod 0755 BaseTools/BuildEnv

  msg 'Move Shell directory to the main source'
  mv ../Shell .

  msg 'Apply EDK Shell patch to support GCC'
  cd Shell
  patch -p1 -i ../EdkShellPkg/ShellR64.patch

  msg 'Fix efilibplat.h path'
  sed -i 's|efilibplat.h|efi/x86_64/efilibplat.h|g' Library/EfiShellLib.h
  sed -i 's|#define RFC_3066_ENTRY_SIZE   12|#define RFC_3066_ENTRY_SIZE   12\n\n#define MIN_ALIGNMENT_SIZE  8|' Library/EfiShellLib.h
}

build() {
  cd $srcdir/edk2-$_srcname

  msg 'Unset all compiler FLAGS'
  unset CFLAGS
  unset CPPFLAGS
  unset CXXFLAGS
  unset LDFLAGS
  unset MAKEFLAGS

  msg 'Setup UDK Environment'
  source $srcdir/edk2-$_srcname/BaseTools/BuildEnv BaseTools
  echo

  msg 'Compile UDK BaseTools'
  make -C BaseTools
  echo

  msg 'Unset all compiler FLAGS'
  unset CFLAGS
  unset CPPFLAGS
  unset CXXFLAGS
  unset LDFLAGS
  unset MAKEFLAGS

  msg 'Compile UEFI Shell v1 X64 binary'
  BaseTools/BinWrappers/PosixLike/build -p EdkShellPkg/EdkShellPkg.dsc -a X64 -b RELEASE -t GCC5 -m Shell/Shell.inf
  echo

  msg 'Compile UEFI Shell v2 X64 binary'
  BaseTools/BinWrappers/PosixLike/build -p ShellPkg/ShellPkg.dsc -a X64 -b RELEASE -t GCC5
  echo
}

package() {
  msg 'Install the UEFI Shell v1 X64 binary'
  install -d $pkgdir/usr/share/uefi-shell
  install -D -m0644 $srcdir/edk2-$_srcname/Build/EdkShellPkg/RELEASE_GCC5/X64/Shell.efi $pkgdir/usr/share/uefi-shell/shellx64_v1.efi

  msg 'Install the UEFI Shell v2 X64 binary'
  install -d $pkgdir/usr/share/uefi-shell
  install -D -m0644 $srcdir/edk2-$_srcname/Build/Shell/RELEASE_GCC5/X64/Shell.efi $pkgdir/usr/share/uefi-shell/shellx64_v2.efi

  msg 'Install license'
  install -Dm644 $srcdir/edk2-$_srcname/License.txt $pkgdir/usr/share/licenses/$pkgname/License.txt
}
