# This is the config file for the catalyst-auto script. It should be pretty
# self-explanatory.

UPLOAD_USER=riscv
UPLOAD_KEY=/root/.ssh/id_ed25519

host=$(hostname)

SPECS_DIR=${REPO_DIR}/releases/specs-qemu/riscv

EMAIL_SUBJECT_PREPEND="[riscv-qemu-auto]"

SETS="
	lp64d_openrc_23
	lp64d_systemd_23
	lp64d_musl_23
	lp64_openrc_23
	lp64_systemd_23
	lp64_musl_23
	multilib_openrc_23
	multilib_systemd_23
	lp64d_openrc
	lp64d_musl
	lp64_openrc
	lp64_musl
	multilib_openrc
"

SET_lp64d_musl_SPECS="stage1-lp64d-musl.spec stage3-lp64d-musl.spec"
SET_lp64d_openrc_SPECS="stage1-lp64d-openrc.spec stage3-lp64d-openrc.spec"

SET_lp64_musl_SPECS="stage1-lp64-musl.spec stage3-lp64-musl.spec"
SET_lp64_openrc_SPECS="stage1-lp64-openrc.spec stage3-lp64-openrc.spec"

SET_multilib_openrc_SPECS="stage1-multilib-openrc.spec stage3-multilib-openrc.spec"

SET_lp64d_musl_23_SPECS="stage1-lp64d-musl-23.spec stage3-lp64d-musl-23.spec"
SET_lp64d_openrc_23_SPECS="stage1-lp64d-openrc-23.spec stage3-lp64d-openrc-23.spec"
SET_lp64d_systemd_23_SPECS="stage1-lp64d-systemd-23.spec stage3-lp64d-systemd-23.spec"

SET_lp64_musl_23_SPECS="stage1-lp64-musl-23.spec stage3-lp64-musl-23.spec"
SET_lp64_openrc_23_SPECS="stage1-lp64-openrc-23.spec stage3-lp64-openrc-23.spec"
SET_lp64_systemd_23_SPECS="stage1-lp64-systemd-23.spec stage3-lp64-systemd-23.spec"

SET_multilib_openrc_23_SPECS="stage1-multilib-openrc-23.spec stage3-multilib-openrc-23.spec"
SET_multilib_systemd_23_SPECS="stage1-multilib-systemd-23.spec stage3-multilib-systemd-23.spec"

update_symlinks() {
	# Symlink the latest stages3 to build from
	local d f
	for d in "${BUILD_SRCDIR_BASE}/builds"/* ; do
		mkdir -p "${d}"
		pushd "${d}" >/dev/null
		for f in $(ls stage3*xz | grep -v latest | give_latest_from_dates) ; do
			local of=$(echo "${f}" | convert_filename)
			ln -sf "${f}" "${of}"
		done
		popd >/dev/null
	done
}

post_build() {
	local set=$1 spec=$2

	pushd "${BUILD_SRCDIR_BASE}" >/dev/null
	case ${spec} in
		stage3-lp64d-openrc-23.spec)
			upload builds/23.0-default/stage3-rv64_lp64d-openrc-${TIMESTAMP}*.xz*
			;;
		stage3-lp64d-systemd-23.spec)
			upload builds/23.0-default/stage3-rv64_lp64d-systemd-${TIMESTAMP}*.xz*
			upsync_binpackages packages/23.0-default/stage3-rv64_lp64d riscv/23.0/rv64_lp64d
			;;
		stage3-lp64-openrc-23.spec)
			upload builds/23.0-default/stage3-rv64_lp64-openrc-${TIMESTAMP}*.xz*
			;;
		stage3-lp64-systemd-23.spec)
			upload builds/23.0-default/stage3-rv64_lp64-systemd-${TIMESTAMP}*.xz*
			upsync_binpackages packages/23.0-default/stage3-rv64_lp64 riscv/23.0/rv64_lp64
			;;
		stage3-multilib-openrc-23.spec)
			upload builds/23.0-default/stage3-rv64_multilib-openrc-${TIMESTAMP}*.xz*
			;;
		stage3-multilib-systemd-23.spec)
			upload builds/23.0-default/stage3-rv64_multilib-systemd-${TIMESTAMP}*.xz*
			upsync_binpackages packages/23.0-default/stage3-rv64_multilib riscv/23.0/rv64_multilib
			;;
		stage3-lp64d-musl-23.spec)
			upload builds/23.0-musl/stage3-rv64_lp64d_musl-${TIMESTAMP}*.xz*
			upsync_binpackages packages/23.0-musl/stage3-rv64_lp64d_musl riscv/23.0/rv64_lp64d_musl
			;;
		stage3-lp64-musl-23.spec)
			upload builds/23.0-musl/stage3-rv64_lp64_musl-${TIMESTAMP}*.xz*
			upsync_binpackages packages/23.0-musl/stage3-rv64_lp64_musl riscv/23.0/rv64_lp64_musl
			;;
		stage3*openrc.spec)
			upsync_binpackages packages/default/stage3-rv64_lp64d riscv/20.0/rv64_lp64d
			upsync_binpackages packages/default/stage3-rv64_lp64 riscv/20.0/rv64_lp64
			upsync_binpackages packages/default/stage3-rv64_multilib riscv/20.0/rv64_multilib
			;;
		stage3*musl.spec)
			upsync_binpackages packages/musl/stage3-rv64_lp64d_musl riscv/20.0/rv64_lp64d_musl
			upsync_binpackages packages/musl/stage3-rv64_lp64_musl riscv/20.0/rv64_lp64_musl
			;;
		*)
			echo "Finished ${spec}"
			;;
	esac
	popd >/dev/null
}
