SUMMARY="Chinese input method library"
DESCRIPTION="SunPinyin is an SLM (Statistical Language Model) based input \
method engine. To model the Chinese language, it uses a backoff bigram and \
trigram language model."
HOMEPAGE="https://github.com/sunpinyin/sunpinyin"
COPYRIGHT="2010-2021 Sun Beijing Globalization team"
LICENSE="CDDL v1
	GNU LGPL v2.1"
REVISION="1"
SOURCE_URI="https://github.com/sunpinyin/sunpinyin/archive/refs/tags/v${portVersion/\~/-}.tar.gz"
CHECKSUM_SHA256="6722bd41e58b0a42bf41b7c1cb7707e6adfce810ab35597f938c5df8aa5a0af0"
SOURCE_DIR="sunpinyin-${portVersion/\~/-}"
PATCHES="libsunpinyin-$portVersion.patchset"

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

libVersion="3.0"
libVersionCompat="$libVersion compat >= ${libVersion%%.*}"

PROVIDES="
	libsunpinyin$secondaryArchSuffix = $portVersion
	cmd:genpyt
	cmd:getwordfreq
	cmd:idngram_merge
	cmd:ids2ngram
	cmd:mmseg
	cmd:mmseg
	cmd:slmbuild
	cmd:slminfo
	cmd:slmpack
	cmd:slmprune
	cmd:slmseg
	cmd:slmthread
	cmd:sunpinyin_dictgen
	cmd:tslmendian
	cmd:tslminfo
	lib:libsunpinyin$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libiconv$secondaryArchSuffix
	lib:libsqlite3$secondaryArchSuffix
	"

PROVIDES_devel="
	libsunpinyin${secondaryArchSuffix}_devel = $portVersion
	devel:libsunpinyin$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES_devel="
	libsunpinyin$secondaryArchSuffix == $portVersion base
	devel:libsqlite3$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libiconv$secondaryArchSuffix
	devel:libsqlite3$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:make
	cmd:gcc$secondaryArchSuffix
	cmd:pkg_config$secondaryArchSuffix
	cmd:pod2man
	cmd:python3
	cmd:scons
	"

BUILD()
{
	export LDFLAGS=-liconv
	scons --prefix=$prefix --libdir=$libDir --datadir=$dataDir
}

INSTALL()
{
	export LDFLAGS=-liconv
	scons install --prefix=$prefix --libdir=$libDir --datadir=$dataDir

	#move some things around, not handled by scons paths
	mkdir -p $docDir $includeDir $manDir
	mv $prefix/share/doc/sunpinyin/* $docDir
	mv $prefix/share/man/man1 $manDir
	mv $prefix/include/sunpinyin-2.0 $includeDir

	# cleanup
	rm -rf $prefix/{include,share}

	prepareInstalledDevelLib libsunpinyin
	fixPkgconfig

	# fixPkgconfig
	if [ -n "$secondaryArchSuffix" ]; then
		sed -i 's,\/headers/x86,\/headers/x86/sunpinyin-2.0,g' \
			$prefix/$relativeDevelopLibDir/pkgconfig/sunpinyin-2.0.pc
	else
		sed -i 's,\/headers,\/headers/sunpinyin-2.0,g' \
			$prefix/$relativeDevelopLibDir/pkgconfig/sunpinyin-2.0.pc
	fi

	packageEntries devel \
		$developDir
}

TEST()
{
	make check
}
