#!/bin/zsh

# Parse the CLI arguments
# Especially the paths to the pure- and MPI-CXX compilers
# Courtesy of http://manpages.ubuntu.com/manpages/bionic/man1/zsh-lovers.1.html
o_cc=(-c /bin/clang)
o_cxx=(-x /bin/clang++)
o_mpicc=(-cmpi /bin/mpicc)
o_mpicxx=(-xmpi /bin/mpicxx)
o_libomproot=(-lomp /opt/homebrew/opt/libomp)
o_llvmroot=(-llvm /opt/homebrew/opt/llvm)
#-
zparseopts -D -E -- x:=o_cxx \
xmpi:=o_mpicxx \
c:=o_cc \
cmpi:=o_mpicc \
lomp:=o_libomproot \
llvm:=o_llvmroot
#-
CC=$o_cc[2]
CXX=$o_cxx[2]
MPICC=$o_mpicc[2]
MPICXX=$o_mpicxx[2]
LIBOMP_ROOT=$o_libomproot[2]
LLVM_ROOT=$o_llvmroot[2]

# Save current location
CWD=`pwd`

# Directory containing the present script
# Courtesy of https://stackoverflow.com/questions/18810483
SCRIPT_DIR="${0:A:h}"
CODE_DIR=$( dirname ${SCRIPT_DIR} )

# Directory welcoming all submodules inside ${CODE_DIR}
SUB_DIR="thirdparty"

# Check for Homebrew
if ! command -v brew &> /dev/null
then
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
#- Install base packages
brew install bison meson ninja libxcb libx11 libxdamage libxext xorgproto
LIBXCB_ROOT=$( brew --prefix libxcb )
LIBX11_ROOT=$( brew --prefix libx11 )
LIBXDAMAGE_ROOT=$( brew --prefix libxdamage )
LIBXEXT_ROOT=$( brew --prefix libxext )
XORGPROTO_ROOT=$( brew --prefix xorgproto )
#- Install mako in python
pip install mako

# Make the directory containing OSMesa
mkdir -p ${CODE_DIR}/${SUB_DIR}/mesa

# Download OSMesa
MESA_VERSION=22.3.6
curl https://archive.mesa3d.org/mesa-${MESA_VERSION}.tar.xz --output ${CODE_DIR}/${SUB_DIR}/mesa/mesa-${MESA_VERSION}.tar.xz
cd ${CODE_DIR}/${SUB_DIR}/mesa
tar -xzf mesa-${MESA_VERSION}.tar.xz --strip-components=1 && rm -f mesa-${MESA_VERSION}.tar.xz

# Build OSMesa
if [ -d build ]; then
    rm -rf build;
fi
#-
mkdir -p build/install
#-
# Reference: https://www.paraview.org/Wiki/ParaView/ParaView_And_Mesa_3D
CC=${CC} CXX=${CXX} \
CFLAGS="-I${LIBXCB_ROOT}/include -I${LIBX11_ROOT}/include -I${LIBXDAMAGE_ROOT}/include -I${LIBXEXT_ROOT}/include -I${XORGPROTO_ROOT}/include" \
LDFLAGS="-L${LIBXCB_ROOT}/lib -L${LIBX11_ROOT}/lib -L${LIBXDAMAGE_ROOT}/lib -L${LIBXEXT_ROOT}/lib" \
meson setup \
--prefix=${CODE_DIR}/${SUB_DIR}/mesa/build/install \
--buildtype=release \
build \
-Dvulkan-drivers=            \
-Ddri-drivers=               \
-Dgallium-vdpau=disabled     \
-Dgallium-omx=disabled       \
-Dgallium-va=disabled        \
-Dgallium-xa=disabled        \
-Dgallium-nine=false         \
-Dgallium-opencl=disabled    \
-Dbuild-tests=false          \
-Degl=disabled               \
-Dgbm=disabled               \
-Dglx=disabled               \
-Dplatforms=                 \
-Dglvnd=false                \
-Dosmesa=true                \
-Dopengl=true                \
-Dgles1=disabled             \
-Dgles2=disabled             \
-Dshared-glapi=enabled       \
-Dllvm=disabled               \
-Dshared-llvm=disabled        \
-Dgallium-drivers=swrast
#-
CC=${CC} CXX=${CXX} \
CFLAGS="-I${LIBXCB_ROOT}/include -I${LIBX11_ROOT}/include -I${LIBXDAMAGE_ROOT}/include -I${LIBXEXT_ROOT}/include -I${XORGPROTO_ROOT}/include" \
LDFLAGS="-L${LIBXCB_ROOT}/lib -L${LIBX11_ROOT}/lib -L${LIBXDAMAGE_ROOT}/lib -L${LIBXEXT_ROOT}/lib" \
meson compile -C build \
--verbose
#-
CC=${CC} CXX=${CXX} \
meson install -C build
