abinfo "Building Mesen..."
make USE_GCC=true MESENFLAGS="${CFLAGS} ${CXXFLAGS}" -j$(nproc)

MESEN_BIN="$(find bin/ -name 'Mesen.exe' -type f | head -n1)"
MESEN_DIR="$(dirname ${MESEN_BIN})"

pushd "${MESEN_DIR}/Dependencies/"

abinfo "Doing AOT compilation..."
# Mesen packed its DLLs into zip and embeds the zip into exe file
# so we need to copy the main exe to this folder for Mono to
# be able to load assemblies correctly
ln -sv '../Mesen.exe' .
mono --aot Mesen.exe

popd

abinfo "Installing..."

cat << EOF > mesen
#!/bin/sh
mono '/usr/libexec/mesen/Mesen.exe'
EOF

strip "${MESEN_BIN}"{,.so} "${MESEN_DIR}/Dependencies/"libMesenCore.*.dll
install -d "${PKGDIR}/usr/libexec/mesen/"
install -d "${PKGDIR}/usr/bin/"
install -Dm755 "${MESEN_BIN}"{,.so} "${PKGDIR}/usr/libexec/mesen/"
install -Dm755 "${MESEN_DIR}/Dependencies/"libMesenCore.*.dll "${PKGDIR}/usr/libexec/mesen/libMesenCore.dll"
install -Dm755 mesen "${PKGDIR}/usr/bin/"
