# Template file for 'Carla'
pkgname=Carla
version=2.5.9
revision=1
archs="x86_64* i686* aarch64* arm*"
build_style=gnu-makefile
pycompile_dirs="usr/share/carla"
hostmakedepends="pkg-config python3 python3-PyQt5-devel-tools which"
makedepends="python3-PyQt5 libmagic file-devel libsndfile-devel
 liblo-devel alsa-lib-devel pulseaudio-devel libX11-devel gtk+3-devel
 gtk+-devel qt5-devel fluidsynth-devel fftw-devel zlib-devel
 python3-rdflib"
depends="python3 python3-PyQt5 python3-PyQt5-svg pyliblo which"
short_desc="Audio plugin host"
maintainer="Rutpiv <roger_freitas@live.com>"
license="GPL-2.0-or-later"
homepage="https://kx.studio/Applications:Carla"
distfiles="https://github.com/falkTX/Carla/archive/v${version}.tar.gz"
checksum=226fb5d646b7541b82035080190e7440df1f92372fb798b4ad49289570e5ad81
python_version=3

build_options="win linux32"
desc_option_win="Enable building windows bridge"
desc_option_linux32="Enable building linux32 bridge"

subpackages="Carla-devel"

if [ -z "$CROSS_BUILD" ]; then
	case "$XBPS_TARGET_MACHINE" in
		x86_64)
			build_options_default=" win linux32"
			subpackages+=" Carla-bridge-win"
		;;
		i686)
			build_options_default=" win"
			subpackages+=" Carla-bridge-win"
		;;
	esac
fi

if [ "$CROSS_BUILD" ]; then
	hostmakedepends+=" Carla-devel qt5-host-tools"
fi

if [ "$build_option_win" ]; then
	case $XBPS_TARGET_MACHINE in
		x86_64)
			hostmakedepends+=" cross-x86_64-w64-mingw32 wine-tools"
			makedepends+=" wine-devel"
		;;
		i686)
			hostmakedepends+=" cross-i686-w64-mingw32 wine-tools"
			makedepends+=" wine-devel"
		;;
	esac
fi

if [ "$build_option_linux32" ]; then
	case $XBPS_TARGET_MACHINE in
		x86_64)
			makedepends+=" gcc-multilib freetype-devel-32bit libX11-devel-32bit"
		;;
	esac
fi

case $XBPS_TARGET_MACHINE in
	x86_64* | i686*);;
	*) make_build_args+=" NOOPT=true"
	   make_install_args+=" NOOPT=true";;
esac

if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
	makedepends+=" libexecinfo-devel musl-fts-devel"
	export LDFLAGS="-lfts"
fi

post_patch() {
	if [ "$CROSS_BUILD" ]; then
		vsed -i -e 's#\./carla-lv2-export#/usr/bin/carla-lv2-export#g' \
			source/plugin/Makefile
	fi
}

pre_build() {
	make ${make_build_args} features
}

post_build() {
	if [ "$build_option_win" ]; then
		case $XBPS_TARGET_MACHINE in
			x86_64)
				CXXFLAGS="" CFLAGS="" LDFLAGS="" \
					make ${makejobs} ${make_build_args} win64 CC="x86_64-w64-mingw32-gcc" CXX="x86_64-w64-mingw32-g++"
				make ${make_build_args} wine64
			;;
			i686)
				CXXFLAGS="" CFLAGS="" LDFLAGS="" \
					make ${makejobs} ${make_build_args} win32 CC="i686-w64-mingw32-gcc" CXX="i686-w64-mingw32-g++"
				make ${make_build_args} wine32
			;;
		esac
	fi

	if [ "$build_option_linux32" ]; then
		make posix32
	fi
}

post_install() {
	vbin bin/carla-lv2-export
}

Carla-devel_package() {
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/bin/carla-lv2-export
		vmove usr/lib/pkgconfig
		vmove usr/include
	}
}

Carla-bridge-win_package() {
	lib32mode=full
	lib32symlinks="carla/carla-bridge-win32.exe
		carla/carla-discovery-win32.exe
		carla/jackbridge-wine32.dll
		vst/carla.vst/carla-bridge-win32.exe
		vst/carla.vst/carla-carla-win32.exe
		vst/carla.vst/jackbridge-wine32.dll
		lv2/carla.lv2/carla-bridge-win32.exe
		lv2/carla.lv2/carla-carla-win32.exe
		lv2/carla.lv2/jackbridge-wine32.dll"

	depends="Carla>=${version} wine"
	short_desc+=" - windows bridge"
	pkg_install() {
		vmove usr/lib/carla/*.exe
		vmove usr/lib/carla/*.dll
		vmove usr/lib/vst/carla.vst/*.exe
		vmove usr/lib/vst/carla.vst/*.dll
		vmove usr/lib/lv2/carla.lv2/*.exe
		vmove usr/lib/lv2/carla.lv2/*.dll
	}
}
