#!/bin/bash

# created by vector sigma on July 15 2019
# github version

# Linux users should be able to use this wrapper, although the following are needed:
# (Ubuntu alike)
# sudo apt-get install build-essential uuid-dev iasl git gcc-10 nasm python3-distutils

# $1 argument override MYTOOLCHAIN variable, in case you want GCC53 for example

cd "$(dirname $([ -L $0 ] && readlink $0 || echo $0))"
declare -r THISROOT="$PWD"
declare -r THISAPP="XhciDxe"

cd ..
declare -r CLOVERROOT="$PWD"
declare -r SYSNAME="$(uname)"
MYTOOLCHAIN=${1:-GCC53}
MYBUILDTARGET=${2:RELEASE}

# Functions
pathmunge() {
if [[ ! $PATH =~ (^|:)$1(:|$) ]]; then
  if [[ "${2:-}" = "after" ]]; then
    export PATH=$PATH:$1
  else
    export PATH=$1:$PATH
  fi
fi
}

checkXCODE() {
echo "[CHECK XCODE]"
if [[ ! -x /usr/bin/xcodebuild ]]; then
  echo "ERROR: Install Xcode Tools from Apple before using this script."
  exit 1
fi

if [[ ! -d "$(xcode-select --print-path)" ]]; then
  echo "ERROR: Xcode Command Line Tools not selected:"
  echo "       open Xcode.app and go in Preferences->Locations,"
  echo "       and select the Command Line Tools"
  exit 1
fi
}

checkGETTEXT() {
exportPaths
local locations=($(which msgmerge))
if [ "${#locations[@]}" -eq 0 ]; then
  export GETTEXT_PREFIX="${TOOLCHAIN_DIR}"
  "${CLOVERROOT}"/buildgettext.sh
else
  # export gettex prefix to ensure buildpkg.sh will use it
  # without the need to install it again
  export GETTEXT_PREFIX="$(dirname "${locations[0]}")"
fi
}

exportPaths() {
if [[ "$SYSNAME" == Linux ]]; then
  TOOLCHAIN_DIR=${TOOLCHAIN_DIR:-/usr}
elif [[ "$SYSNAME" == Darwin ]]; then
  pathmunge "$(xcode-select --print-path)"/usr/bin
  if [[ -d ~/src/opt/local ]]; then
    TOOLCHAIN_DIR=~/src/opt/local
  else
    TOOLCHAIN_DIR=${TOOLCHAIN_DIR:-"$CLOVERROOT"/toolchain}
  fi
fi

pathmunge "$TOOLCHAIN_DIR"/bin
export TOOLCHAIN_DIR=$TOOLCHAIN_DIR
export DIR_MAIN=${DIR_MAIN:-"$CLOVERROOT"/toolchain}
export DIR_TOOLS=${DIR_TOOLS:-$DIR_MAIN/tools}
export DIR_DOWNLOADS=${DIR_DOWNLOADS:-$DIR_TOOLS/download}
export DIR_LOGS=${DIR_LOGS:-$DIR_TOOLS/logs}
export PREFIX=${TOOLCHAIN_DIR}
}

checkTools() {
# export any env vars before building anything
if [[ "$SYSNAME" == Darwin ]]; then
  checkXCODE
  exportPaths
  local GCC53_BIN="$PREFIX/cross/bin/x86_64-clover-linux-gnu-gcc"
  if [[ $MYTOOLCHAIN == GCC* ]] && [[ ! -x "${GCC53_BIN}" ]]; then
    if [[ $MYTOOLCHAIN == GCC53 ]]; then
      cd "${CLOVERROOT}"
      ./build_gcc10.sh
    else
      MYTOOLCHAIN=XCODE8
    fi
  fi
else
  exportPaths
fi
}

buildthisapp() {
checkTools

# to force recreation of the Conf folder. You can still use a custom CONF_PATH if you don't want recreation.
rm -rf "$CLOVERROOT"/Conf
mkdir "$CLOVERROOT"/Conf

cd "${CLOVERROOT}"
if [[ -z "$WORKSPACE" ]]; then
  export EDK_TOOLS_PATH="${PWD}"/BaseTools
  set +u
  source ./edksetup.sh BaseTools
  set -u
  cd "$CLOVERROOT"
  WORKSPACE="${PWD}"
fi

echo "[BUILD CLOVER]"
# Run a custom build script if exist (~/src/tools/Scripts/build.sh)
# This allow the user to run ebuild.sh with own parameters

echo "${THISROOT}/${THISAPP}_ebuild.sh"               -D NO_GRUB_DRIVERS_EMBEDDED -t $MYTOOLCHAIN -b $MYBUILDTARGET -m "${THISROOT}/${THISAPP}.inf"
     "${THISROOT}/${THISAPP}_ebuild.sh"               -D NO_GRUB_DRIVERS_EMBEDDED -t $MYTOOLCHAIN -b $MYBUILDTARGET -m "${THISROOT}/${THISAPP}.inf"

# Run a post build script if exist (~/src/tools/Scripts/postbuild.sh)
if [[ -x "${DIR_TOOLS}"/Scripts/postbuild.sh ]]; then
  echo "Running postbuild script"
  "${DIR_TOOLS}"/Scripts/postbuild.sh "${CLOVERROOT}" $MYTOOLCHAIN
fi
}

echo
echo '------------------------------------------------------------------------'
cd "${CLOVERROOT}"

echo "$THISAPP_buildme"
echo "TOOLCHAIN: $MYTOOLCHAIN (override example: './buildme XCODE8')"
echo

# Main
set -e

buildthisapp
