USES+=			dos2unix
DOS2UNIX_GLOB=		*.c *.cpp *.h *.H *.ini *.conf

MAKE_ENV+=		PIC=1
PATCHDIR=		${.CURDIR}/files
BUILD_WRKSRC?=		${WRKSRC}/projects/unix

.if ${PKGNAMESUFFIX} == "-core"
BUILD_DEPENDS=		bash:shells/bash \
			nasm:devel/nasm \
			vulkan-headers>0:graphics/vulkan-headers
LIB_DEPENDS+=		libfreetype.so:print/freetype2 \
			libpng.so:graphics/png
USES+=			minizip
.else
PLIST=			${WRKDIR}/pkg-plist
.endif

.if ${PKGNAMESUFFIX} == "-video-rice"
LIB_DEPENDS+=		libpng.so:graphics/png
USE_CXXSTD=	c++14
.endif

.if ${PKGNAMESUFFIX} == "-video-glide64mk"
LIB_DEPENDS+=		libboost_thread.so:devel/boost-libs
.endif

.if ${PKGNAMESUFFIX} != "-core"
post-patch:
	@cd ${BUILD_WRKSRC} && ${REINPLACE_CMD} -e 's|gcc|${CC}|g' -e \
		's|g++|${CXX}|g' -e 's|sdl-config|${SDL2_CONFIG}|g' Makefile
.	if ${PKGNAMESUFFIX} == "-video-glide64mk2"
		@cd ${WRKSRC}/src && \
			${REINPLACE_CMD} -e 's|#include <thread>||g' \
				GlideHQ/TxQuantize.cpp GlideHQ/TxFilter.cpp && \
			${REINPLACE_CMD} -e 's|(FxU32)NULL|(uintptr_t)NULL|g' \
				Glide64/Main.cpp
.	endif
.	if ${PKGNAMESUFFIX} == "-video-glide64"
		@cd ${WRKSRC}/src && \
			${REINPLACE_CMD} -e 's|__builtin_ia32_loadups|_mm_loadu_ps|g' -e \
				's|__builtin_ia32_storeups|_mm_storeu_ps|g' \
					3dmath.cpp && \
			${REINPLACE_CMD} -e 's|\"G64_VERSION|\" G64_VERSION|g' -e \
				's|FxU32|uintptr_t|g' \
					Main.cpp
.	endif

pre-install:
	@${RM} ${PLIST}
.	if ${PKGNAMESUFFIX} == "-video-rice"
		@${ECHO_CMD} "%%DATADIR%%/RiceVideoLinux.ini" >> ${PLIST}
.	endif

.	if ${PKGNAMESUFFIX} == "-video-glide64"
		@${ECHO_CMD} "%%DATADIR%%/Glide64.ini" >> ${PLIST}
.	endif

.	if ${PKGNAMESUFFIX} == "-video-glide64mk2"
		@${ECHO_CMD} "%%DATADIR%%/Glide64mk2.ini" >> ${PLIST}
.	endif

.	if ${PKGNAMESUFFIX} == "-input-sdl"
		@${ECHO_CMD} "%%DATADIR%%/InputAutoCfg.ini" >> ${PLIST}
.	endif

.	if ${PKGNAMESUFFIX} == "-ui-console"
		@${ECHO_CMD} "share/man/man6/mupen64plus.6.gz" >> ${PLIST}
.	endif

.	if ${PKGNAMESUFFIX} != "-ui-console"
		@${ECHO_CMD} "lib/${PORTNAME}/${PORTNAME}${PKGNAMESUFFIX}.so" >> ${PLIST}
.	else
		@${ECHO_CMD} "bin/${PORTNAME}" >> ${PLIST}
.	endif
	@${ECHO_CMD} "@dir %%DATADIR%%" >> ${PLIST}
.else
post-patch:
	@cd ${BUILD_WRKSRC} && ${REINPLACE_CMD} -e 's|gcc|${CC}|g' -e \
		's|g++|${CXX}|g' Makefile
.endif

do-install:
	@${MKDIR} ${STAGEDIR}${DATADIR}
.if ${PKGNAMESUFFIX} == "-core"

	${INSTALL_PROGRAM} ${BUILD_WRKSRC}/libmupen64plus.so.2.0.0 \
		${STAGEDIR}${PREFIX}/lib/libmupen64plus.so.2.0.0

	${INSTALL_DATA} ${WRKSRC}/data/* \
		${STAGEDIR}${DATADIR}

	@${MKDIR} ${STAGEDIR}${PREFIX}/include/${PORTNAME}
	${INSTALL_DATA} ${WRKSRC}/src/api/m64p_*.h \
		${STAGEDIR}${PREFIX}/include/${PORTNAME}

	${MKDIR} ${STAGEDIR}${DOCSDIR}
	${INSTALL_DATA} ${WRKSRC}/RELEASE \
		${STAGEDIR}${DOCSDIR}/RELEASE
	${LN} -sf ${PREFIX}/lib/libmupen64plus.so.2.0.0 ${STAGEDIR}${PREFIX}/lib/libmupen64plus.so.2
.else
.	if ${PKGNAMESUFFIX} == "-ui-console"
		${INSTALL_MAN} ${WRKSRC}/doc/mupen64plus.6 ${STAGEDIR}${PREFIX}/share/man/man6/
.	endif

.	if ${PKGNAMESUFFIX} == "-video-rice"
		${INSTALL_DATA} ${WRKSRC}/data/RiceVideoLinux.ini ${STAGEDIR}${DATADIR}
.	endif

.	if ${PKGNAMESUFFIX} == "-video-glide64"
		${INSTALL_DATA} ${WRKSRC}/data/Glide64.ini ${STAGEDIR}${DATADIR}
.	endif

.	if ${PKGNAMESUFFIX} == "-video-glide64mk2"
		${INSTALL_DATA} ${WRKSRC}/data/Glide64mk2.ini ${STAGEDIR}${DATADIR}
.	endif

.	if ${PKGNAMESUFFIX} == "-input-sdl"
		${INSTALL_DATA} ${WRKSRC}/data/InputAutoCfg.ini ${STAGEDIR}${DATADIR}
.	endif

.	if ${PKGNAMESUFFIX} != "-ui-console"
		@${MKDIR} ${STAGEDIR}${PREFIX}/lib/${PORTNAME}
		${INSTALL_PROGRAM} ${BUILD_WRKSRC}/${PORTNAME}${PKGNAMESUFFIX}.so ${STAGEDIR}${PREFIX}/lib/${PORTNAME}
.	else
		${INSTALL_PROGRAM} ${BUILD_WRKSRC}/${PORTNAME} ${STAGEDIR}${PREFIX}/bin
.	endif
.endif
