# Contributor: lauren n. liberda <lauren@selfisekai.rocks>
# Maintainer: lauren n. liberda <lauren@selfisekai.rocks>
pkgname=flutter
# upgrade checklist: https://md.sdomi.pl/Q-ECZTJ2Qqyp3ahfJuYSBw
pkgver=3.24.3
pkgrel=0
pkgdesc="Cross-platform apps, built in Omelas"
url="https://flutter.dev/"
arch="aarch64 x86_64"	# officially supported for linux desktop
license="BSD-3-Clause"
_dartver=3.5.4
_depends_common="
	bash
	clang
	cmake
	dart-sdk~$_dartver
	git
	gtk+3.0-dev
	pkgconf
	samurai
	"
makedepends="
	$_depends_common
	freetype-dev
	gn
	harfbuzz-dev
	icu-dev
	jpeg-dev
	libpng-dev
	llvm
	python3
	spirv-tools-dev
	sqlite-dev
	zlib-dev
	zstd
	"
subpackages="
	$pkgname-common
	$pkgname-desktop
	$pkgname-developer
	$pkgname-glfw
	$pkgname-gtk
	$pkgname-tool
	$pkgname-tool-developer:tool_developer
	"

case "$pkgver" in
	*.*.*_alpha*)
		_canonver="${pkgver/_alpha/-}"
		_canonver="${_canonver/-r/.}.dev"
		_channel=master
		;;
	*.*.*_beta*)
		_canonver="${pkgver/_beta/-}"
		_canonver="${_canonver/-r/.}.beta"
		_channel=beta
		;;
	*.*.*_pre*)
		_canonver="${pkgver/_pre/-}"
		_canonver="${_canonver/-r/.}.pre"
		_channel=master
		;;
	*.*.*)
		_canonver="$pkgver"
		_channel=stable
		;;
esac

case "$_dartver" in
	*.*.*_alpha*)
		_dartcanonver="${_dartver/_alpha/-}"
		_dartcanonver="${_dartcanonver/-r/.}.dev"
		;;
	*.*.*_beta*)
		_dartcanonver="${_dartver/_beta/-}"
		_dartcanonver="${_dartcanonver/-r/.}.beta"
		;;
	*.*.*)
		_dartcanonver="$_dartver"
		;;
esac

_enginever=36335019a8eab588c3c2ea783c618d90505be233
_materialfontsver=3012db47f3130e62f7cc0beabff968a33cbec8d8
_gradlewver=fd5c1f2c013565a3bea56ada6df9d2b8e96d56aa
options="!check"	# idk no attempt to build tests even with --enable-unittests
source="
	https://github.com/flutter/flutter/archive/refs/tags/$_canonver/flutter-$_canonver.tar.gz
	https://ab-sn.lnl.gay/flutter-engine-$_enginever.tar.zst
	https://ab-sn.lnl.gay/dart-sdk-$_dartcanonver.tar.zst
	fonts-$_materialfontsver.zip.noauto::https://storage.googleapis.com/flutter_infra_release/flutter/fonts/$_materialfontsver/fonts.zip
	gradle-wrapper-$_gradlewver.tar.gz.noauto::https://storage.googleapis.com/flutter_infra_release/gradle-wrapper/$_gradlewver/gradle-wrapper.tgz

	alpine-target.patch
	doctor.patch
	no-lock.patch
	no-runtime-download.patch
	opt-in-analytics.patch
	system-dart.patch
	unbundle.patch
	version.patch

	pmos-if-touch-is-a-mouse-then-mouse-is-touch.patch

	git-revision.patch.engine
	target-musl.patch.engine
	musl-no-execinfo.patch.engine
	musl-no-mallinfo.patch.engine
	no-vpython.patch.engine
	shared-libcxx.patch.engine
	libstdc++13.patch.engine
	system-icu.patch.engine
	unbundle.patch.engine

	flutter
	"
builddir="$srcdir/flutter-$_canonver"

export PUB_CACHE="$srcdir/flutter-$_canonver/pub_cache"

export CFLAGS="${CFLAGS/-g/} -O2 -Wno-error -Wno-absolute-value -Wno-implicit-float-conversion"
export CXXFLAGS="${CXXFLAGS/-g/} -O2 -Wno-error -Wno-absolute-value -Wno-implicit-float-conversion"

case "$CARCH" in
	x86_64) _flutter_arch="x64" ;;
	aarch64) _flutter_arch="arm64" ;;
esac
_out="$srcdir"/flutter-engine-$_enginever/out
_modes='release profile debug'
_release_out="$_out"/linux_release_$_flutter_arch
_debug_out="$_out"/linux_debug_$_flutter_arch

# gclient comes from teapot-tools
_distbucket="sakamoto/lnl-aports-snapshots/"
snapshot() {
	mkdir -p "$srcdir"
	cd "$srcdir"

	rm -rf src
	rm -rf flutter-engine-$_enginever
	rm -f flutter-engine-$_enginever.tar
	rm -f flutter-engine-$_enginever.tar.zst

	echo "
solutions = [{
	'name': 'src/flutter',
	'url': 'https://github.com/flutter/engine.git@$_enginever',
	'custom_vars': {
		'download_android_deps': False,
		'download_dart_sdk': False,
	},
}]
target_cpu = ['x64', 'arm', 'arm64', 'riscv64']
target_cpu_only = True
" > .gclient

	gclient sync --no-history --nohooks --tpot-cipd-ignore-platformed

	for elf in $(scanelf -RA -F "%F" src); do
		rm -f "$elf"
	done

	mv src flutter-engine-$_enginever

	msg "generating tarball.."
	tar -cf flutter-engine-$_enginever.tar \
		--exclude="ChangeLog*" \
		--exclude=".build-id" \
		--exclude="third_party/dart" \
		--exclude-backups \
		--exclude-caches-all \
		--exclude-vcs \
		flutter-engine-$_enginever

	zstd --auto-threads=logical --ultra --long -22 -T"${ZSTD_LIMIT:-0}" -vv flutter-engine-$_enginever.tar -o "$SRCDEST"/flutter-engine-$_enginever.tar.zst
	mcli cp "$SRCDEST"/flutter-engine-$_enginever.tar.zst "$_distbucket"
}

prepare() {
	dos2unix "$srcdir"/flutter-engine-$_enginever/flutter/third_party/vulkan_memory_allocator/include/vk_mem_alloc.h

	default_prepare

	msg "Applying more patches"
	for x in $source; do
		case "$x" in
		*.patch.engine)
			msg "$x"
			patch -p1 -i "$srcdir"/$x -d "$srcdir"/flutter-engine-$_enginever
			;;
		esac
	done

	_dart_installed="$(cat /usr/lib/dart/version)"
	if [ "$_dart_installed" != "$_dartcanonver" ]; then
		echo "mismatch between installed dart version ($_dart_installed) and downloaded source code ($_dartcanonver)"
		exit 1
	fi

	echo -n "$_canonver" > version
	mkdir bin/cache

	(
		cd "$srcdir"/flutter-engine-$_enginever

		# bind dart source code
		rm -rf flutter/third_party/dart
		ln -s "$srcdir"/dart-sdk-$_dartcanonver flutter/third_party/dart
		# bind dart build
		mkdir -p flutter/third_party/dart/tools/sdks/dart-sdk/
		ln -s /usr/lib/dart/bin flutter/third_party/dart/tools/sdks/dart-sdk/bin
		mkdir -p flutter/prebuilts/linux-$_flutter_arch/dart-sdk/
		ln -s /usr/lib/dart/bin flutter/prebuilts/linux-$_flutter_arch/dart-sdk/bin

		mkdir -p flutter/third_party/gn/
		ln -s /usr/bin/gn flutter/third_party/gn/gn

		# https://github.com/dart-lang/sdk/issues/52295
		mkdir -p flutter/third_party/dart/.git/logs
		touch flutter/third_party/dart/.git/logs/HEAD

		# DEPS hooks
		python3 flutter/third_party/dart/tools/generate_package_config.py
		python3 flutter/third_party/dart/tools/generate_sdk_version_file.py
		python3 flutter/tools/pub_get_offline.py

		# reusable system library settings
		local use_system="
			freetype2
			harfbuzz
			icu
			libjpeg-turbo
			libpng
			libwebp
			sqlite
			zlib
			"
		for _lib in $use_system; do
			msg "Removing buildscripts for system provided $_lib"
			find . -type f -path "*third_party/$_lib/*" \
				\! -path "*third_party/$_lib/chromium/*" \
				\! -path "*third_party/$_lib/google/*" \
				\! -regex '.*\.\(gn\|gni\|isolate\|py\)' \
				-delete
		done

		msg "Replacing gn files"
		python3 build/linux/unbundle/replace_gn_files.py --system-libraries \
			$use_system
	)

	(
		cd packages/flutter_tools/

		# no pubspec.lock in repo (?)
		dart pub get --no-offline --no-precompile
	)
}

build() {

	(
		cd "$srcdir"/flutter-engine-$_enginever

		# shellcheck disable=2089
		local gn_args="
			host_libc=\"musl\"
			target_libc=\"musl\"
			use_custom_libcxx=false
			use_default_linux_sysroot=false
		"
		local gnw_args="
			--no-goma
			--no-dart-version-git-info
			--linux
			--linux-cpu=$_flutter_arch
			--lto
			--clang
			--target-triple=$CTARGET
			--no-backtrace
			--no-stripped
			--prebuilt-dart-sdk
			--build-glfw-shell
			--build-engine-artifacts
			--no-enable-unittests
			--enable-fontconfig
		"
		for _mode in $_modes; do
			msg "Generating ninja files for engine - $_mode"
			# shellcheck disable=2090,2116
			python3 ./flutter/tools/gn \
				$gnw_args \
				--runtime-mode="$_mode" \
				--gn-args="$(echo $gn_args)"
			msg "Build engine artifacts - $_mode"
			ninja -C "$_out"/linux_${_mode}_$_flutter_arch artifacts
		done
		msg "Build flutter common and product patched sdk"
		ninja -C "$_release_out" flutter flutter_patched_sdk sky
		msg "Build debug patched sdk"
		ninja -C "$_debug_out" flutter_patched_sdk
	)

	dart --verbosity=error \
		--snapshot="bin/cache/flutter_tools.snapshot" --snapshot-kind="app-jit" \
		--packages="packages/flutter_tools/.dart_tool/package_config.json" \
		--no-enable-mirrors "packages/flutter_tools/bin/flutter_tools.dart"

	sed -Ei 's|'"$PUB_CACHE"'|/usr/lib/flutter/pub_cache|g' packages/flutter_tools/.dart_tool/package_config.json

	# looks like they can get created in the process
	find "$PUB_CACHE" -name '*.aot' -delete
}

check() {
	(
		cd "$srcdir"/flutter-engine-$_enginever

		ln -s ./linux_debug_$_flutter_arch ./out/linux_debug_unopt

		python3 flutter/testing/run_tests.py --type=engine
	)
}

package() {
	depends="flutter-developer=$pkgver-r$pkgrel"

	# our entrypoint
	install -Dm755 "$srcdir"/flutter "$pkgdir"/usr/lib/flutter/bin/flutter
	mkdir -p "$pkgdir"/usr/bin/
	ln -s ../lib/flutter/bin/flutter "$pkgdir"/usr/bin/flutter

	# our dart
	mkdir -p "$pkgdir"/usr/lib/flutter/bin/cache/
	ln -s ../../../dart "$pkgdir"/usr/lib/flutter/bin/cache/dart-sdk
	_devtoolsver="$(grep 'devtools_rev' "$srcdir/dart-sdk-$_dartcanonver/DEPS" | head -n1 | awk -F\" '{ print $4 }')"
	cat > "$pkgdir"/usr/lib/flutter/bin/cache/flutter.version.json <<EOF
{
	"frameworkVersion": "$_canonver",
	"channel": "$_channel",
	"repositoryUrl": "https://github.com/flutter/flutter.git",
	"frameworkRevision": "alpineaports0000000000000000000000000000",
	"frameworkCommitDate": "2038-01-19 03:14:08",
	"engineRevision": "$_enginever",
	"dartSdkVersion": "$_dartcanonver",
	"devToolsVersion": "$_devtoolsver",
	"flutterVersion": "$_canonver"
}
EOF

	# flutter tool (cli) snapshot
	install -Dm755 "$builddir"/bin/cache/flutter_tools.snapshot "$pkgdir"/usr/lib/flutter/bin/cache/flutter_tools.snapshot

	# patched sdk
	mkdir -p "$pkgdir"/usr/lib/flutter/bin/cache/artifacts/engine/common
	cp -r "$_release_out"/flutter_patched_sdk/ \
		"$pkgdir"/usr/lib/flutter/bin/cache/artifacts/engine/common/flutter_patched_sdk_product
	cp -r "$_debug_out"/flutter_patched_sdk/ \
		"$pkgdir"/usr/lib/flutter/bin/cache/artifacts/engine/common/flutter_patched_sdk

	# dart:ui package
	mkdir -p "$pkgdir"/usr/lib/flutter/bin/cache/pkg
	cp -rL "$_release_out"/gen/dart-pkg/sky_engine "$pkgdir"/usr/lib/flutter/bin/cache/pkg/sky_engine

	# flutter tool and framework packages (as part of sdk)
	cp -r "$builddir"/packages "$pkgdir"/usr/lib/flutter/packages
	# needed for flutter analyze
	mkdir -p "$pkgdir"/usr/lib/flutter/dev "$pkgdir"/usr/lib/flutter/examples
	touch "$pkgdir"/usr/lib/flutter/dev/.intentionally-empty "$pkgdir"/usr/lib/flutter/examples/.intentionally-empty
	# pub dependencies of flutter (part of sdk)
	cp -r "$PUB_CACHE" "$pkgdir"/usr/lib/flutter/pub_cache

	# version files
	echo -n "$_canonver" > "$pkgdir"/usr/lib/flutter/version
	mkdir -p "$pkgdir"/usr/lib/flutter/bin/internal/
	echo -n "$_enginever" > "$pkgdir"/usr/lib/flutter/bin/internal/engine.version

	# fonts
	mkdir -p "$pkgdir"/usr/lib/flutter/bin/cache/artifacts/material_fonts
	unzip "$srcdir"/fonts-"$_materialfontsver".zip.noauto \
		-d "$pkgdir"/usr/lib/flutter/bin/cache/artifacts/material_fonts

	# gradle wrapper (-developer only, needed for flutter create)
	mkdir -p "$pkgdir"/usr/lib/flutter/bin/cache/artifacts/gradle_wrapper
	tar xf "$srcdir"/gradle-wrapper-"$_gradlewver".tar.gz.noauto \
		-C "$pkgdir"/usr/lib/flutter/bin/cache/artifacts/gradle_wrapper

	# build-time artifacts from engine (for use by tool)
	for _mode in $_modes; do
		case "$_mode" in
			debug) _mode_art_name=linux-$_flutter_arch ;;
			*) _mode_art_name=linux-$_flutter_arch-$_mode ;;
		esac
		mkdir -p "$pkgdir"/usr/lib/flutter/bin/cache/artifacts/engine/$_mode_art_name/
		unzip "$_out"/linux_${_mode}_$_flutter_arch/zip_archives/$_mode_art_name/artifacts.zip \
			-d "$pkgdir"/usr/lib/flutter/bin/cache/artifacts/engine/$_mode_art_name/
	done
	# they are all the same, simply copied from tree
	cp -r "$_release_out"/flutter_linux/ \
		"$pkgdir"/usr/lib/flutter/bin/cache/artifacts/engine/linux-$_flutter_arch/flutter_linux
	ln -s ../linux-$_flutter_arch/flutter_linux "$pkgdir"/usr/lib/flutter/bin/cache/artifacts/engine/linux-$_flutter_arch-profile/flutter_linux
	ln -s ../linux-$_flutter_arch/flutter_linux "$pkgdir"/usr/lib/flutter/bin/cache/artifacts/engine/linux-$_flutter_arch-release/flutter_linux
	mkdir -p "$pkgdir"/usr/include/
	ln -s ../lib/flutter/bin/cache/artifacts/engine/linux-$_flutter_arch/flutter_linux \
		"$pkgdir"/usr/include/flutter_linux

	install -Dm755 "$_release_out"/libflutter_linux_gtk.so \
		"$pkgdir"/usr/lib/libflutter_linux_gtk.so

	# see system-icu.patch.engine
	ln -s /usr/share/icu/*/icudt*l.dat "$pkgdir"/usr/lib/flutter/icudtl.dat

	install -Dm755 "$_release_out"/libflutter_linux_glfw.so "$pkgdir"/usr/lib/libflutter_linux_glfw.so
}

common() {
	pkgdesc="$pkgdesc (parts common to all targets)"
	depends="$_depends_common"

	amove usr/lib/flutter/bin/cache/artifacts/engine/common
	amove usr/lib/flutter/bin/cache/artifacts/material_fonts
	amove usr/lib/flutter/bin/cache/dart-sdk
	amove usr/lib/flutter/bin/cache/pkg
	amove usr/lib/flutter/bin/internal
	amove usr/lib/flutter/packages
	amove usr/lib/flutter/pub_cache
}

desktop() {
	pkgdesc="$pkgdesc (release linux desktop target)"
	depends="flutter-common=$pkgver-r$pkgrel flutter-glfw=$pkgver-r$pkgrel flutter-gtk=$pkgver-r$pkgrel flutter-tool=$pkgver-r$pkgrel"

	amove usr/include/flutter_linux
	amove usr/lib/flutter/bin/cache/artifacts/engine/linux-$_flutter_arch
	amove usr/lib/flutter/bin/cache/artifacts/engine/linux-$_flutter_arch-release
}

developer() {
	pkgdesc="$pkgdesc (app developer tools)"
	depends="flutter-desktop=$pkgver-r$pkgrel flutter-tool-developer=$pkgver-r$pkgrel"

	amove usr/lib/flutter/bin/cache/artifacts/engine/linux-$_flutter_arch-profile
	amove usr/lib/flutter/bin/cache/artifacts/gradle_wrapper
}

gtk() {
	pkgdesc="$pkgdesc (GTK embedder runtime)"
	depends=""

	amove usr/lib/libflutter_linux_gtk.so
	amove usr/lib/flutter/icudtl.dat
}

glfw() {
	pkgdesc="$pkgdesc (GLFW embedder runtime)"
	depends=""

	amove usr/lib/libflutter_linux_glfw.so
}

tool() {
	pkgdesc="$pkgdesc (CLI tool)"
	depends="flutter-common=$pkgver-r$pkgrel"

	amove usr/bin/flutter
	amove usr/lib/flutter/bin/cache/flutter_tools.snapshot
	amove usr/lib/flutter/bin/cache/flutter.version.json
	amove usr/lib/flutter/bin/flutter
	amove usr/lib/flutter/version
}

tool_developer() {
	pkgdesc="$pkgdesc (CLI tool, parts for app developers)"
	depends="flutter-tool=$pkgver-r$pkgrel"

	amove usr/lib/flutter/dev
	amove usr/lib/flutter/examples
}

sha512sums="
cd208254bbda68d6df93652f2b631bab53fcf5ad21f233e180eeb0f8606b31c2b6c069aeae1e6a70dabc0ec8a951d125070d225bc5541b7da5b337c5ded1a047  flutter-3.24.3.tar.gz
74f4127697131531ac73ac0e7faad80a15480f418148c56b19b05741cb1a5df44b20b3de75977198a5c533b1842529928667f88c5d2bc40fec01b286885a28d1  flutter-engine-36335019a8eab588c3c2ea783c618d90505be233.tar.zst
c01619fb0b9608dae28a19766784a10442deef416eefb5ab6442dfc3a2f20745d358d893a114e6c2e310fc7c5808cdac9aea3763cd760e92b6604e505f744951  dart-sdk-3.5.4.tar.zst
604f9a256fb1ec8c1b3eb6636baa97f195012b89b68197aef28104ed59dbcb70bde4fc1c7e154018999beabda160b60fa1378bcc1cd1ebe71c79071f40ebb26f  fonts-3012db47f3130e62f7cc0beabff968a33cbec8d8.zip.noauto
fcce6b1659f3a4ee7849bf7f65a9b8e9b3046fc0cfae70d364efabbff2d6c1ed41e6e9009e5fabc522117cbd2f92c5c664f64426e2606fce6b69d9960dd6ca63  gradle-wrapper-fd5c1f2c013565a3bea56ada6df9d2b8e96d56aa.tar.gz.noauto
a7d6e459a8884bd2b7083b15e6d113576e454c68feba8fcf467c6d288e6abca656a1e669eee2b9973d0b30a1dfbbd736d8ee65292d42bbd832059703b7067dd8  alpine-target.patch
a9db5df026cf641728f744ffd3428db9cf3918718233c707fd154a88b321c4a7af8e41ac6c3b08f1f4614c6b49e0689653b3b01e35c1e3ff3c5c9e7920fdd8d4  doctor.patch
f47f3bb1e97c79bdaf5fdd3b466936a335d80d48bffafa7c015a32d97a665d5450e63a33fd5eb0560215de57d556721eaa92db05848257427dd582b10543adda  no-lock.patch
b2705a855163660cc3c472f6416d5839d9d4947164e2ea0ed97e93645255f27b7b513e7dc7de196cae5685b5e328b35ebd65b614be9b32f2fddd1fc8053a3963  no-runtime-download.patch
91e598d9da383519f107c75669d8ec71f85afea38fa9e9a89e2f6c6088ce27f3107ab57e68931c5b27aec73788c9cd81123bcfae87152d878a170465aec9a193  opt-in-analytics.patch
b07b662c9647a21fdc7dbd2842436a53b4ce5b369a4641bf1fd8ab4bd0235f3e7d8cc0e9f2bd0bb5e2c871b3f9696505a6eedecf20cd93833949c081eb356525  system-dart.patch
9855cf075781e1d1032f6a8dcc160cdcfc4d360875bc4fe005c83ca87dec767f1ce15e7c55d653688f1f2f0cb6b0b0328ccfd826e18ececc50d5d41d8b5b89d2  unbundle.patch
5b4dc7cdfe6476748c458a0f61267f75a3ce959fff73327e4b9083e1291d46f59000ff5d853d34ecfaa55ecc317d002754653528566ca5a8fc9126b20148a0e5  version.patch
38673902256ee546c45cefd8e82f310ab7207ac50a038efcbf34c67dee04d626fa37462456729e7725729495ecd4ed7c0b16b72967ef768153d189d7b10c7439  pmos-if-touch-is-a-mouse-then-mouse-is-touch.patch
2c74f25be53500f1b91bd920751dae72913167f7c45aa5aeec28a1712cc723ae469957ebbae60c230439af15483d5186052cef8e4f0e2caf3111d5b73234ed0d  git-revision.patch.engine
d60578a06001d3c6393b4f429f8cf32d50289c34b5157404ddb945d4e52c7c6733f4659128b3dfce9dc02efd9b498b75c1f3ec7533ccceaab9496577a5d628f8  target-musl.patch.engine
d2c55af9dc64c380905cd5d9bc792cec2e37063265cc84643b77995f9787d2239fe34f596a575ac6aa9eae5d82cd55dcd1ed20d59f58c631291064a8c9ae47c0  musl-no-execinfo.patch.engine
5794a5f9f8532dc6615bc7c89c2f5953ad09a4e8cc1ef7618c9fa8a7816488714ab972b83fef0ff87da797404e182c516975d3376493b906be067edcda015914  musl-no-mallinfo.patch.engine
6634043f5618835dd9f33a3ec473cdf11bfae2456ee670e2ea24c14f8f1f9d45f9dc273dcb035400d18404e74833c4df1d57e78a00de8010aa7e1e68099db6cd  no-vpython.patch.engine
e138d2b393db3aa1328199fc4d1ac4b2e0ef8384faf04bf077c6fd97ef9813f94b7f25fccbe5fed4919d7ec0bbd800289b5d3a9b8bc654cbe5c5574fb6bee329  shared-libcxx.patch.engine
a56c70c79a6eda69bfbafc60402c246e8baea9158452e1b1d0d6129ce5df15aa9483822e0831a4556915c96e5b53f0a2162c77c856437e31008e0d85ba838975  libstdc++13.patch.engine
56ab8c8ee82be1e39698605888b71a829ac59e309d1d02f932794b0ed4b0f2e8e7382e4530217723234733509a7a0c65e0e7627084c88687f00ab464cdd307c1  system-icu.patch.engine
160813c47cc3d12b2623208566b81be167781d2f16b1e57493ba565a0efc8b78b30c9af1f2962cfa7e9e96b61bf0357427af38f9130e219875528bdcd1588c66  unbundle.patch.engine
38e6484b18cff811be30da4f770b281cbffccbcbd813121d55eb869edfca7e4ff751d59e6bfcc79c23d427d021d0fd363a7eff26bbc11a5dc90de2be1a153c4e  flutter
"
