#!/bin/bash -e

. $(dirname $0)/mbd-common.sh
. "${MBD_REPCONFIGFILE}"
. "${MBD_BLDCONFIGFILE}"

mbd_opt_init "Mini-buildd: Setup base chroots (root only)."
mbd_opt_add "f" "Force fresh setup (i.e., delete old setup first)."
mbd_opt_add "d" "Delete existing setup."
mbd_opt_parse "$@"

if ${mbd_defer}; then
	${MBD_LOG} -s "W: Configuration deferred, skipping."
	exit 0
fi

CHROOT_FS="ext2"

mbdGetChrootPersonality()
{
	local arch="${1}"
	case ${arch} in
		i386)
			echo -n "linux32"
			;;
		*)
			echo -n "linux"
			;;
	esac
}

mbdCheckUser root

mbdDeleteMarkedConfig "${MBD_SCHROOTCONFIGFILE}"

MBD_TMP_SCHROOTCONFIGFILE="${MBD_SCHROOTCONFIGFILE}.mini-buildd"
cat <<EOF >"${MBD_TMP_SCHROOTCONFIGFILE}"
${MBD_CONFIG_MARK}
# Auto-generated by ${0} on $(date).

EOF

MBD_TMP_MNTPOINT=$(mktemp -d /tmp/mini-buildd.XXXXXX)
trap "rmdir ${MBD_TMP_MNTPOINT}; rm -f ${MBD_TMP_SCHROOTCONFIGFILE}" EXIT

# Check/prepare chroots for all dists.
for dist in $(mbdD2SList "${mbd_dists}"); do
	for arch in $(mbdD2SList "${mbd_archs}"); do
		# Are we responsible for that arch?
		MBD_TMP_BLDHOST="mbd_bldhost_${arch}"
		if [ "${mbd_bldhost}" = "${!MBD_TMP_BLDHOST}" ]; then
			MBD_TMP_CHROOT="mbd-${dist}-${mbd_id}-${arch}";
			MBD_TMP_CHROOT_EXPERIMENTAL="mbd-${dist}-${mbd_id}-experimental-${arch}";
			MBD_TMP_DEV="/dev/$(mbdLvmVgName)/${MBD_TMP_CHROOT}"
			MBD_TMP_CHROOT_PERSONALITY=$(mbdGetChrootPersonality ${arch});

			if (mbd_opt_given f || mbd_opt_given d) && lvdisplay | grep -q "${MBD_TMP_CHROOT}"; then
				${MBD_LOG} -s "I: LV ${MBD_TMP_CHROOT}: Forcing removal: $(lvremove --force "${MBD_TMP_DEV}" 2>&1)"
			fi

			if ! mbd_opt_given d; then
				if lvdisplay | grep -q "${MBD_TMP_CHROOT}"; then
					${MBD_LOG} -s "I: LV ${MBD_TMP_CHROOT} exists, leaving alone."
				else
					${MBD_LOG} -s "I: Setting up chroot ${MBD_TMP_CHROOT}..."

					MBD_TMP_BASESRC=$(mbdGetSrcVar ${dist} base ${arch})
					MBD_TMP_MIRROR=$(echo "${!MBD_TMP_BASESRC}" | cut -d' ' -f1)
					${MBD_LOG} -s "I: Found mirror for ${dist}(${arch}): ${MBD_TMP_MIRROR}"

					(
						mbdAptEnv
						if lvcreate -L 4G -n "${MBD_TMP_CHROOT}" "$(mbdLvmVgName)" &&
							mkfs.${CHROOT_FS} "${MBD_TMP_DEV}" &&
							mount -v -t${CHROOT_FS} "${MBD_TMP_DEV}" "${MBD_TMP_MNTPOINT}" &&
							debootstrap --variant=buildd --arch ${arch} --include=apt "${dist}" "${MBD_TMP_MNTPOINT}" "${MBD_TMP_MIRROR}" &&
							umount -v "${MBD_TMP_MNTPOINT}"; then
							echo "I: LV ${MBD_TMP_CHROOT} created successfully."
						else
							echo "E: Failed to create LV ${MBD_TMP_CHROOT}; rerun ${0} as root any time to retry; rewinding..."
							umount -v "${MBD_TMP_MNTPOINT}" || true
							lvremove --force "${MBD_TMP_DEV}" || true
						fi
					) 2>&1 | ${MBD_LOG} -s
				fi
				# We need two configs to be able to distinguish experimental for sources setup (does not work with aliases).
				for chroot_name in ${MBD_TMP_CHROOT} ${MBD_TMP_CHROOT_EXPERIMENTAL}; do
					cat <<EOF >>"${MBD_TMP_SCHROOTCONFIGFILE}"
[${chroot_name}]
type=lvm-snapshot
description=Mini-Buildd ${MBD_TMP_CHROOT} snapshot chroot
priority=3
groups=sbuild
users=mini-buildd
root-users=mini-buildd
source-root-users=mini-buildd
device=${MBD_TMP_DEV}
mount-options=-t ${CHROOT_FS} -o noatime,user_xattr
lvm-snapshot-options=--size 4G
personality=${MBD_TMP_CHROOT_PERSONALITY}
run-setup-scripts=true
run-exec-scripts=true

EOF
				done
			fi
		fi
	done
done
echo "${MBD_CONFIG_MARK}" >>"${MBD_TMP_SCHROOTCONFIGFILE}"

if ! mbd_opt_given d; then
	cat "${MBD_TMP_SCHROOTCONFIGFILE}" >>"${MBD_SCHROOTCONFIGFILE}"
	${MBD_LOG} -s "I: ${MBD_SCHROOTCONFIGFILE} updated."
	# Trigger a -bld update
	su - mini-buildd -c "${MBD_LIB}/mbd-update-bld"
fi
