SUMMARY="A Full-Featured Open-Source Framework for Image Processing"
DESCRIPTION="G'MIC is a full-featured open-source framework for digital \
image processing, distributed under the CeCILL free software licenses \
(LGPL-like and/or GPL-compatible). It provides several user interfaces \
to convert / manipulate / filter / visualize generic image datasets, \
ranging from 1D scalar signals to 3D+t sequences of multi-spectral \
volumetric images, hence including 2D color images."
HOMEPAGE="https://gmic.eu"
COPYRIGHT="Sébastien Fourey"
LICENSE="GNU GPL v3"
REVISION="8"
SOURCE_URI="https://gmic.eu/files/source/gmic_$portVersion.tar.gz"
CHECKSUM_SHA256="9f053338752ec96a6b619718037767682c5fd58e2471c08f3740fdb070605bc0"
SOURCE_DIR="gmic-$portVersion"
PATCHES="gmic-$portVersion.patchset"
ADDITIONAL_FILES="
	gmic.rdef.in
	icons.zip
	"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"

PROVIDES="
	gmic$secondaryArchSuffix = $portVersion
	app:GMIC$secondaryArchSuffix = $portVersion
	lib:libgmic${secondaryArchSuffix} = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libavcodec$secondaryArchSuffix
	lib:libcurl$secondaryArchSuffix
	lib:libfftw3$secondaryArchSuffix
	lib:libGL$secondaryArchSuffix
	lib:libgomp$secondaryArchSuffix
	lib:libGraphicsMagick++$secondaryArchSuffix
	lib:libGraphicsMagickWand$secondaryArchSuffix
	lib:libiconv$secondaryArchSuffix
	lib:libintl$secondaryArchSuffix
	lib:libjpeg$secondaryArchSuffix
	lib:libOpenEXR_3_0$secondaryArchSuffix
	lib:libOpenEXRUtil_3_0$secondaryArchSuffix
	lib:libopencv_core$secondaryArchSuffix
	lib:libpng16$secondaryArchSuffix
	lib:libQt5Core$secondaryArchSuffix
	lib:libQt5Gui$secondaryArchSuffix
	lib:libQt5Network$secondaryArchSuffix
	lib:libQt5Widgets$secondaryArchSuffix
	lib:libtiff$secondaryArchSuffix
	lib:libxml2$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_devel="
	gmic${secondaryArchSuffix}_devel = $portVersion
	devel:libgmic${secondaryArchSuffix} = $portVersion
	"
REQUIRES_devel="
	gmic$secondaryArchSuffix == $portVersion base
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libavcodec$secondaryArchSuffix
	devel:libcurl$secondaryArchSuffix
	devel:libfftw3$secondaryArchSuffix
	devel:libGL$secondaryArchSuffix
	devel:libGraphicsMagick$secondaryArchSuffix
	devel:libGraphicsMagickWand$secondaryArchSuffix
	devel:libiconv$secondaryArchSuffix
	devel:libjpeg$secondaryArchSuffix
	devel:libOpenEXR_3_0$secondaryArchSuffix
	devel:libOpenEXRUtil_3_0$secondaryArchSuffix
	devel:libopencv_core$secondaryArchSuffix
	devel:libpng16$secondaryArchSuffix
	devel:libQt5Core$secondaryArchSuffix
	devel:libQt5Gui$secondaryArchSuffix
	devel:libQt5Network$secondaryArchSuffix
	devel:libQt5Widgets$secondaryArchSuffix
	devel:libtiff$secondaryArchSuffix
	devel:libxml2$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:cmake
	cmd:g++$secondaryArchSuffix
	cmd:lrelease$secondaryArchSuffix >= 5
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	cmd:unzip
	"

PATCH()
{
	unzip -o $sourceDir/../../additional-files/icons.zip -d $sourceDir/gmic-qt/icons
	unzip -o $sourceDir/../../additional-files/icons.zip -d $sourceDir/gmic-qt/icons/dark
}

BUILD()
{
	export DISABLE_ASLR=1

	local CUSTOM_CXX_FLAGS="-O3"
	if [ "$targetArchitecture" = x86_gcc2 ]; then
		CUSTOM_CXX_FLAGS="-O1"
	fi

	cmake $cmakeDirArgs \
		-B build -S . \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_CXX_FLAGS=$CUSTOM_CXX_FLAGS \
		-DCUSTOM_CFLAGS=ON \
		-DBUILD_BASH_COMPLETION=OFF \
		-DBUILD_CLI=OFF \
		-DBUILD_LIB=ON \
		-DBUILD_LIB_STATIC=OFF \
		-DBUILD_MAN=OFF \
		-DENABLE_CURL=ON \
		-DENABLE_DYNAMIC_LINKING=ON \
		-DENABLE_FFMPEG=ON \
		-DENABLE_FFTW=ON \
		-DENABLE_GRAPHICSMAGICK=ON \
		-DENABLE_OPENCV=ON \
		-DENABLE_OPENEXR=ON \
		-DENABLE_OPENMP=ON \
		-DENABLE_PNG=ON \
		-DENABLE_TIFF=ON \
		-DENABLE_ZLIB=ON \
		-DENABLE_LTO=OFF

	cd build
	make VERBOSE=1
	cd ..

	export LDFLAGS="$LDFLAGS -L../build"

	cmake -B build-qt -S ./gmic-qt \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_CXX_FLAGS=$CUSTOM_CXX_FLAGS \
		-DCUSTOM_CFLAGS=ON \
		-DGMIC_PATH=$sourceDir/src \
		-DGMIC_QT_HOST=none \
		-DENABLE_ASAN=OFF \
		-DENABLE_CURL=ON \
		-DENABLE_DYNAMIC_LINKING=ON \
		-DENABLE_FFTW3=ON \
		-DENABLE_LTO=OFF

	cd build-qt
	make VERBOSE=1
	cd ..

	cmake -B build-krita -S ./gmic-qt \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_CXX_FLAGS=$CUSTOM_CXX_FLAGS \
		-DCUSTOM_CFLAGS=ON \
		-DGMIC_PATH=$sourceDir/src \
		-DGMIC_QT_HOST=krita \
		-DENABLE_ASAN=OFF \
		-DENABLE_CURL=ON \
		-DENABLE_DYNAMIC_LINKING=ON \
		-DENABLE_FFTW3=ON \
		-DENABLE_LTO=OFF

	cd build-krita
	make VERBOSE=1
}

INSTALL()
{
	cd build
	make install
	cd ..

	mkdir -p "$appsDir/G'MIC"
	cp build-qt/gmic_qt "$appsDir/G'MIC/G'MIC"
	cp README "$appsDir/G'MIC"

	mkdir -p "$appsDir/Krita"
	cp build-krita/gmic_krita_qt "$appsDir/Krita"

	local APP_SIGNATURE="application/x-vnd.gmic"
	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | cut -d. -f2`"
	local MINOR="`echo "$portVersion" | cut -d. -f3`"
	local LONG_INFO="$SUMMARY"
	sed \
		-e "s|@APP_SIGNATURE@|$APP_SIGNATURE|" \
		-e "s|@MAJOR@|$MAJOR|" \
		-e "s|@MIDDLE@|$MIDDLE|" \
		-e "s|@MINOR@|$MINOR|" \
		-e "s|@LONG_INFO@|$LONG_INFO|" \
		$portDir/additional-files/gmic.rdef.in > gmic.rdef

	addResourcesToBinaries gmic.rdef "$appsDir/G'MIC/G'MIC"

	APP_SIGNATURE="application/x-vnd.gmic-krita-plugin"
	sed \
		-e "s|@APP_SIGNATURE@|$APP_SIGNATURE|" \
		-e "s|@MAJOR@|$MAJOR|" \
		-e "s|@MIDDLE@|$MIDDLE|" \
		-e "s|@MINOR@|$MINOR|" \
		-e "s|@LONG_INFO@|$LONG_INFO|" \
		$portDir/additional-files/gmic.rdef.in > gmic.rdef

	addResourcesToBinaries gmic.rdef "$appsDir/Krita/gmic_krita_qt"

	addAppDeskbarSymlink "$appsDir/G'MIC/G'MIC"

	prepareInstalledDevelLibs libgmic

	packageEntries devel \
		$developDir \
		$libDir/cmake
}
