# Template file for 'mozc'
pkgname=mozc
version=2.23.2815.102
revision=9
create_wrksrc=yes
build_wrksrc=mozc/src
hostmakedepends="gettext ninja pkg-config protobuf protobuf-devel python which"
makedepends="gtk+-devel ibus-devel libzinnia-devel protobuf-devel qt5-devel
 fcitx-devel"
depends="tegaki-zinnia-japanese>=0.3"
short_desc="Japanese IME (Open Source version of Google Japanese Input)"
maintainer="Matthias von Faber <mvf@gmx.eu>"
license="BSD-3-Clause"
homepage="https://github.com/google/mozc"
distfiles="
 https://gitlab.com/fcitx/mozc/repository/dad94584ea5012110ad1d204e433964243a1639d/archive.tar.bz2>${pkgname}-${version}.tar.bz2
 https://github.com/bnoordhuis/gyp/archive/c6f471687407bf28ddfc63f1a8f47aeb7bf54edc.tar.gz
 https://github.com/hiroyuki-komatsu/japanese-usage-dictionary/archive/e5b3425575734c323e1d947009dd74709437b684.tar.gz
"
checksum="
 e38457e63a7fa8d62d87b4f8b3e89fe5f6939d92e3c4fd287a93358375693892
 882f074cbb3a4f2bba550fb615e63949c0fbf39eeb1718a0330b4a545ab97616
 0b6efee0eebac2c1a8eeea333278aa40fcef7846bba9a379962c6e567e7e3dc1
"

CXXFLAGS="-D_GNU_SOURCE"

if [ "$CROSS_BUILD" ]; then
	hostmakedepends+=" qt5-devel"
	subpackages="ibus-mozc fcitx-mozc"
fi

if [ "$XBPS_TARGET_ENDIAN" = "be" ]; then
	broken="Does not build"
fi

post_extract() {
	ln -s mozc-* mozc
	rm -rf mozc/src/unix/fcitx5

	# symlink "submodules" into place
	for _src_dest in gyp japanese-usage-dictionary:japanese_usage_dictionary; do
		_src=${_src_dest%:*}
		_dst=mozc/src/third_party/${_src_dest#*:}
		rmdir $_dst
		ln -sr $_src-* $_dst
	done

	if [ "$CROSS_BUILD" ]; then
		# find fcitx4-config
		sed -i -e "s;fcitx4-config;${XBPS_CROSS_BASE}/usr/bin/fcitx4-config;" mozc/src/unix/fcitx/fcitx.gyp
	fi
}

do_configure() {
	case "$XBPS_TARGET_MACHINE" in
		*-musl) sed -i -e 's|PTHREAD_MUTEX_RECURSIVE_NP|PTHREAD_MUTEX_RECURSIVE|g' base/mutex.cc ;;
	esac
	GYP_DEFINES="
	 document_dir=/usr/share/licenses/${pkgname}
	 use_libprotobuf=1
	 use_libzinnia=1
	" python2 build_mozc.py gyp --target_platform=Linux
}

do_build() {
	local targets
	targets="
	 server/server.gyp:mozc_server
	 gui/gui.gyp:mozc_tool
	 unix/emacs/emacs.gyp:mozc_emacs_helper
	 unix/ibus/ibus.gyp:ibus_mozc
	 renderer/renderer.gyp:mozc_renderer
	 unix/fcitx/fcitx.gyp:fcitx-mozc
	"
	python2 build_mozc.py build -c Release $targets

	sed -i 's|/usr/libexec/|/usr/lib/ibus-mozc/|g' out_linux/Release/gen/unix/ibus/mozc.xml
}

do_install() {
	vinstall out_linux/Release/mozc_server 755 usr/lib/mozc
	vinstall out_linux/Release/mozc_tool 755 usr/lib/mozc

	head -n 29 server/mozc_server.cc > LICENSE
	for i in LICENSE data/installer/*.html; do
		vlicense "$i"
	done
}

ibus-mozc_package() {
	short_desc="Mozc engine for IBus - Client of the Mozc input method"
	depends="${sourcepkg}-${version}_${revision} ibus>=1.5.14"
	pkg_install() {
		vinstall out_linux/Release/ibus_mozc 755 usr/lib/ibus-mozc ibus-engine-mozc
		vinstall out_linux/Release/gen/unix/ibus/mozc.xml 644 usr/share/ibus/component
		vinstall data/images/unix/ime_product_icon_opensource-32.png 644 usr/share/ibus-mozc product_icon.png
		for i in data/images/unix/ui-*.png; do
			vinstall "${i}" 644 usr/share/ibus-mozc "${i##*/ui-}"
		done
		vinstall out_linux/Release/mozc_renderer 755 usr/lib/mozc
	}
}

emacs-mozc_package() {
	pkgdesc="Mozc for Emacs"
	depends="${sourcepkg}-${version}_${revision} emacs"
	pkg_install() {
		vbin out_linux/Release/mozc_emacs_helper
		vinstall unix/emacs/mozc.el 644 usr/share/emacs/site-lisp/emacs-mozc
	}
}

fcitx-mozc_package() {
	short_desc="Mozc engine for fcitx - Client of the Mozc input method"
	depends="${sourcepkg}-${version}_${revision} fcitx>=4.2.9"
	homepage="https://fcitx-im.org/"
	license="GPL-2.0-or-later"
	pkg_install() {
		vinstall out_linux/Release/fcitx-mozc.so 644 usr/lib/fcitx
		vinstall unix/fcitx/fcitx-mozc.conf 644 usr/share/fcitx/addon
		vinstall unix/fcitx/mozc.conf 644 usr/share/fcitx/inputmethod
	}
}
