# Template file for 'sunpinyin'
pkgname=sunpinyin
version=3.0.0.rc2
revision=2
build_style=scons
hostmakedepends="pkg-config python3 perl gettext"
makedepends="python3-devel sqlite-devel ibus-devel"
short_desc="Statistical language model based Chinese input method"
maintainer="Đoàn Trần Công Danh <congdanhqx@gmail.com>"
license="LGPL-2.1-or-later, CDDL-1.0"
homepage="https://github.com/sunpinyin/sunpinyin"
# fcitx has this archive, too
_lm_sc=lm_sc.3gm.arpa-20140820.tar.bz2
_dict=dict.utf8-20131214.tar.bz2
distfiles="https://github.com/sunpinyin/sunpinyin/archive/refs/tags/v${version/.r/-r}.tar.gz
 https://downloads.sourceforge.net/project/open-gram/$_lm_sc
 https://downloads.sourceforge.net/project/open-gram/$_dict"
checksum="6722bd41e58b0a42bf41b7c1cb7707e6adfce810ab35597f938c5df8aa5a0af0
 751bab7c55ea93a2cedfb0fbb7eb09f67d4da9c2c55496e5f31eb8580f1d1e2f
 af70bc2bcd7af7468495774fed9e3a2de434650119fbc3d3388c2bcf7e0acb01"
skip_extraction="$_lm_sc $_dict"
CXXFLAGS="-I../../src"
LDFLAGS="-L."

if [ "$CROSS_BUILD" ]; then
	hostmakedepends+=" sunpinyin"
fi

do_configure() {
	cat <<-EOF >configure.conf
	PREFIX         = '/usr'
	LIBDIR         = '/usr/lib${XBPS_TARGET_WORDSIZE}'
	DATADIR        = '/usr/share'
	LIBEXECDIR     = '/usr/libexec'
	ENABLE_PLUGINS = True
	PYTHON         = '/usr/bin/python3'
	EOF
	cp configure.conf wrapper/ibus/configure.conf
}

post_extract() {
	local _f
	mkdir -p dict
	for _f in $skip_extraction; do
		bsdtar -C dict -xf "$XBPS_SRCDISTDIR/sunpinyin-$version/$_f"
	done
	ln -s ../doc/SLM-inst.mk dict/Makefile
}

post_build() {
	PATH="$PATH:$wrksrc/src"
	make -C dict ${makejobs} ENDIANESS="${XBPS_TARGET_ENDIAN}"
	PKG_CONFIG_PATH="$wrksrc:/usr/lib/pkgconfig:/usr/share/pkgconfig"
	PKG_CONFIG_LIBDIR="$wrksrc:/usr/lib/pkgconfig"
	export PKG_CONFIG_PATH PKG_CONFIG_LIBDIR
	cd wrapper/ibus
	ln -s ../../libsunpinyin.so.*.* libsunpinyin.so
	do_build
}

post_install() {
	PKG_CONFIG_PATH="$wrksrc:/usr/lib/pkgconfig:/usr/share/pkgconfig"
	PKG_CONFIG_LIBDIR="$wrksrc:/usr/lib/pkgconfig"
	export PKG_CONFIG_PATH PKG_CONFIG_LIBDIR
	cd wrapper/ibus
	do_install
}

libsunpinyin_package() {
	short_desc+=" - libraries"
	pkg_install() {
		vmove "usr/lib/*.so.*"
		make -C dict install \
			DESTDIR=${PKGDESTDIR} \
			ENDIANESS="${XBPS_TARGET_ENDIAN}"
	}
}

libsunpinyin-devel_package() {
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove "usr/lib/*.so"
		vmove usr/lib/pkgconfig
	}
}

ibus-sunpinyin_package() {
	short_desc+=" - IBus plugin"
	pkg_install() {
		vmove usr/libexec
		vmove "usr/share/ibus*"
		vmove usr/share/locale
	}
}
