pre_pkg_pretend() {
	if use js-type-check || use hangouts; then
		die "USE flags closure-compile and hangouts forbidden when using ungoogled-chromium patches"
	fi
}

from_pull_request() {
	wget https://github.com/Eloston/ungoogled-chromium/pulls -O - | grep -e '/Eloston/ungoogled-chromium/pull/[0-9]\+">[^<]' | grep -e 
}

post_pkg_setup() {
	local tag

	if [[ "$MERGE_TYPE" != "source" ]]; then
		return
	fi

	tag="`git ls-remote --tags https://github.com/Eloston/ungoogled-chromium | grep -Ee "refs/tags/${PV//./\\.}-[0-9]+\$" | tail -n 1 | grep -Poe '(?<=refs/tags/).*'`"
	[[ "${tag:0:${#PV}}" == "$PV" ]] || die

	pushd "$T"
#	cp -f /tmp/ungoogled-chromium.tar.gz . || die
	wget https://github.com/Eloston/ungoogled-chromium/archive/"$tag".tar.gz -O ungoogled-chromium.tar.gz || die
	popd
}

post_src_prepare() {
	if [[ "$MERGE_TYPE" != "source" ]]; then
		return
	fi

	pushd "$WORKDIR"
	tar -xf "$T"/ungoogled-chromium.tar.gz || die
	rm -f "$T"/ungoogled-chromium.tar.gz || die

	pushd ungoogled-chromium-"$PV"-*

	# do ungoogled-chromium patching
	PATH=/usr/bin:"$PATH" ./utils/prune_binaries.py ../"$P" pruning.list
	PATH=/usr/bin:"$PATH" ./utils/patches.py apply ../"$P" patches || die
	PATH=/usr/bin:"$PATH" ./utils/domain_substitution.py apply -r domain_regex.list -f domain_substitution.list -c ../domsubcache.tar.gz ../"$P" || die

	popd
	popd
}

pre_src_configure() {
	local line t

#	EXTRA_GN="${EXTRA_GN} is_clang=true use_thin_lto=true use_lld=true chrome_pgo_phase=1"
	EXTRA_GN="${EXTRA_GN} is_clang=true use_thin_lto=true use_lld=true chrome_pgo_phase=2 "'pgo_data_path="/tmp/chromium.profdata"'

	# add ungoogled-chromium flags.gn to EXTRA_GN
	t="${IFS}"
	IFS=$'\n'
	while read line; do
		case "$line" in
			enable_nacl=*) ;;
			enable_widevine=*) ;;
			fieldtrial_testing_like_official_build=*) ;;
			*)
				EXTRA_GN="${EXTRA_GN} ${line}"
				;;
		esac
	done <"$WORKDIR"/ungoogled-chromium-"$PV"-*/flags.gn
	IFS="${t}"

	# solve weird bug
	pushd "$WORKDIR/$P"
	sed -e '/password_store_signin_notifier_impl/ s/^/#/;' -i chrome/browser/BUILD.gn || die
	popd
}
