# Contributor: Sergey Lukin <sergej.lukin@gmail.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=icu
pkgver=76.1
pkgrel=1
pkgdesc="International Components for Unicode library"
url="https://icu.unicode.org/"
arch="all"
license="ICU"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="python3 py3-yaml"
checkdepends="diffutils"
install="$pkgname-data-en.post-install"
subpackages="
	$pkgname-static
	$pkgname-dev
	$pkgname-doc
	$pkgname-libs
	$pkgname-data-en:_data_en:noarch
	$pkgname-data-full:_data_full:noarch
	"
source="https://github.com/unicode-org/icu/releases/download/release-${pkgver//./-}/icu4c-${pkgver//./_}-src.tgz
	https://github.com/unicode-org/icu/releases/download/release-${pkgver//./-}/icu4c-${pkgver//./_}-data.zip
	https://github.com/unicode-org/icu/releases/download/release-${pkgver//./-}/icu4c-${pkgver//./_}-data-bin-b.zip
	https://github.com/unicode-org/icu/releases/download/release-${pkgver//./-}/icu4c-${pkgver//./_}-data-bin-l.zip
	standardize-vtzone-output.patch
	CVE-2025-5222.patch
	pc.patch
	data-filter-en.yml
	"
builddir="$srcdir/icu/source"

# seems to fail check now with gcc13, only on x86
case "$CARCH" in
x86) options="$options !check" ;;
esac

# secfixes:
#   76.1-r1:
#     - CVE-2025-5222
#   66.1-r0:
#     - CVE-2020-21913
#   65.1-r1:
#     - CVE-2020-10531
#   57.1-r1:
#     - CVE-2016-6293
#   58.1-r1:
#     - CVE-2016-7415
#   58.2-r2:
#     - CVE-2017-7867
#     - CVE-2017-7868

case "$CARCH" in
	s390x) _icudtfile=icudt${pkgver%%.*}b.dat;;
	*) _icudtfile=icudt${pkgver%%.*}l.dat;;
esac

prepare() {
	default_prepare
	update_config_sub

	rm -rf data
	mv "$srcdir"/data .
}

build() {
	_yaml2json "$srcdir"/data-filter-en.yml > data-filter-en.json
	export ICU_DATA_FILTER_FILE="./data-filter-en.json"

	case "$CARCH" in
	armv7)
		# bus error with -Os for some reason
		export CFLAGS="$CFLAGS -O2"
		export CXXFLAGS="$CXXFLAGS -O2"
		;;
	esac

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--with-data-packaging=archive \
		--disable-samples \
		--enable-static \
		--mandir=/usr/share/man
	make
}

check() {
	# Tests need full data.
	export ICU_DATA="$srcdir"/$_icudtfile

	CINTLTST_OPTS='-w' make check
}

package() {
	make DESTDIR="$pkgdir" install

	install -Dm644 "$srcdir"/icu/license.html -t "$pkgdir"/usr/share/licenses/icu/
}

dev() {
	default_dev

	amove usr/lib/icu
	amove usr/lib/libicutest.so.*
	amove usr/share/icu/*/LICENSE
	amove usr/share/icu/*/config
	amove usr/share/icu/*/install-sh
	amove usr/share/icu/*/mkinstalldirs
}

libs() {
	pkgdesc="$pkgdesc (libraries)"
	depends="$pkgname-data=$pkgver-r$pkgrel"
	replaces="icu"

	# Data stub required by the Common and I18n libraries.
	amove usr/lib/libicudata.so.[0-9]*
	# A library that contains many locale based internationalization (i18n) functions.
	amove usr/lib/libicui18n.so.[0-9]*
	# An optional library that provides a stdio like API with Unicode support.
	amove usr/lib/libicuio.so.[0-9]*
	# Base library required by all other ICU libraries.
	amove usr/lib/libicuuc.so.[0-9]*

	# An internal library that contains internal APIs that are only used by ICU’s tools.
	# Keep in the base package.
	#amove usr/lib/libicutu.so.[0-9]*
}

_data_en() {
	pkgdesc="Stripped down ICU data with only en_US/GB locale and no legacy charset converters"
	provides="$pkgname-data=$pkgver-r$pkgrel"
	provider_priority=100  # highest (other provider is icu-data-full)
	replaces="$pkgname-data<71.1-r1"

	amove usr/share/icu/$pkgver/$_icudtfile
}

_data_full() {
	pkgdesc="Full ICU data"
	provides="$pkgname-data=$pkgver-r$pkgrel"
	provider_priority=10  # lowest (other provider is icu-data-en)
	replaces="$pkgname-data<71.1-r1"

	install -D -m644 "$srcdir"/$_icudtfile -t "$subpkgdir"/usr/share/icu/$pkgver/
}

_yaml2json() {
	python3 -c 'import sys, yaml, json; json.dump(yaml.safe_load(sys.stdin), sys.stdout)' <"$1"
}

sha512sums="
b702ab62fb37a1574d5f4a768326d0f8fa30d9db5b015605b5f8215b5d8547f83d84880c586d3dcc7b6c76f8d47ef34e04b0f51baa55908f737024dd79a42a6c  icu4c-76_1-src.tgz
db029885e4bb274ebabb71de35adfd16f463a1d23b30ee909991b0e443d9dae374f2289924358e9042b1159167626b314a94a482e1e01fe097f7b6a9695ec119  icu4c-76_1-data.zip
098326fbb0f4a1b70a314985cbe6918f3fec94feb17236dcf8efbc516e139294ec96ae49210e11ee40f4de1ac6977e939cbfd6087009be057b8a60d3ad01daad  icu4c-76_1-data-bin-b.zip
1359ff28bad54f73fe29cc5c4fffb4c11c64399ddcc39bea2ee60b5d3672e7f79546a2255d604474cbd861791c11e62eb50bcecc0cf2cf9a7ece59180e8520fc  icu4c-76_1-data-bin-l.zip
c76fd529f92a7b27a45a307c8bb91c109910d012478414b08983af3320e29fbe76e03d8434c4efbde68ecaa56a86130d6514428c5a1085985634bc3650e8e96e  standardize-vtzone-output.patch
eaf41940ff39aa31c8f825ef3fa3f0168115ef8c153fb8dc4e1929fec99cdc7e5db0dabb3e643e9d1518688f41e9a57f03c4eec922f90048b2215fb82fec59f9  CVE-2025-5222.patch
c42817874c7ec659295d0b4f4058458deea1b78cd0e690bcab4f5affe5edb258e510fcf1cce0586c86ea2ec342feb4d57c9357fc148259ee979257ea26c7aa84  pc.patch
609541c1ac10c12b2b52f7800a2057d5c97e49dc9a1774a1b53e13d88599128baa7637f1a63b4de52dfe58b1038c7f3462ef29ad223dbe2ecb2862e3249a1cf4  data-filter-en.yml
"
