project=foundationpkgs
triggers="refs/heads/main: checks plan packages transfer deploy"
# Reviewed per-format allowlists; Debian artifacts use the signed APT publisher.
ci_apk_packages=${FPKGS_APK_PACKAGES:-}
# initify is a required CI tool artifact, even when application selection changes.
# Builds exercise conversion fixtures; package jobs never install into the host.
ci_deb_packages="initify ${FPKGS_DEB_PACKAGES:-minimal-fxa-server sqlite-vector sqlite-memory saait}"

checks() {
	for file in "$srcdir"/scripts/*.sh "$srcdir"/tests/*.sh; do
		sh -n "$file" || return 1
	done
	for test in "$srcdir"/tests/*.sh; do
		sh "$test" || return 1
	done
}

plan() {
	for format in apk deb; do
		case "$format" in apk) selected=$ci_apk_packages ;; deb) selected=$ci_deb_packages ;; esac
		[ -n "$selected" ] || continue
		FPKGS_FORMAT=$format FPKGS_PACKAGES=$selected \
		FPKGS_ROOT=$srcdir FPKGS_WORK_ROOT=$workdir/fpkgs \
			$srcdir/scripts/fpkgs-ci.sh plan "$FETCH_HEAD" || return 1
	done
}

packages() {
	# /tmp is a small tmpfs on the CI host; Go build work belongs on disk.
	mkdir -p "$workdir/tmp"
	# Build/stage the native converter from this revision; no host installation.
	ci_tools=$(TMPDIR="$workdir/tmp" sh "$srcdir/scripts/prepare-ci-tools.sh" "$workdir/ci-tools") || return 1
	export PATH="$ci_tools:$PATH"
	for format in apk deb; do
		case "$format" in apk) selected=$ci_apk_packages ;; deb) selected=$ci_deb_packages ;; esac
		[ -n "$selected" ] || continue
		TMPDIR=$workdir/tmp FPKGS_FORMAT=$format FPKGS_PACKAGES=$selected \
		FPKGS_ROOT=$srcdir FPKGS_WORK_ROOT=$workdir/fpkgs \
			$srcdir/scripts/fpkgs-ci.sh run "$FETCH_HEAD" || return 1
	done
	artifact_name=foundationpkgs
	artifact_url=https://nply.org/dl/foundationpkgs/
}

transfer() {
	FPKGS_ROOT=$srcdir sh "$srcdir/scripts/apt-delivery.sh" transfer "$FETCH_HEAD" $ci_deb_packages
}

deploy() {
	sh "$srcdir/scripts/apt-delivery.sh" deploy "$FETCH_HEAD" || return 1
	artifact_name=foundationpkgs-apt
	artifact_url=https://nply.org/dl/foundationpkgs-binaries/apt/
}
