#!/bin/bash -e

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

# Update config file; this must be first
if [ ! -e "/etc/init.d/mini-buildd-rep" ]; then
	# -bld w/o -rep only; retrieve config from rephost.
	mbdGetUrl "${MBD_REPCONFIGFILE}" "http://${mbd_rephttphost}/~mini-buildd/config" || true
fi

mbd_opt_init "Mini-buildd: Builder update script."
mbd_opt_parse "$@"
mbdCheckUser mini-buildd

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

cd "${MBD_HOME}"
. "${MBD_REPCONFIGFILE}"

mkdir -p "bld/builds" "bld/chroots"

# Lenny sbuild compatability hack for pgp_options (see bug
# #592592). Default is the new format:
SBUILD_PGP_OPTIONS="['-us', '-k Mini-Buildd Automatic Signing Key']"
# ...but for sbuild < 0.60 (lenny), use ye olde format
if dpkg --compare-versions $(dpkg-query --show --showformat='${Version}' sbuild) lt 0.60.0; then
	SBUILD_PGP_OPTIONS="'-us -k\"${MBD_GNUPG_KEYNAME}\"'"
fi

cat <<EOF >"${MBD_SBUILDCONFIGFILE}"
# Mode "user": Use retval to check if build was successful.
\$sbuild_mode = 'user';
# We always want to update the cache as we generate sources.list on thy fly.
\$apt_update = 1;
# APT-Policy to "1", so we can satisfy dependencies via all sources we added.
\$apt_policy = 1;
# Allow unauthenticated apt toggle; see ~/.mini-buildd/README how to set up keys for extra sources.
\$apt_allow_unauthenticated = $(if ${mbd_apt_allow_unauthenticated}; then echo -n "1"; else echo -n "0"; fi);

# Add path for ccache
\$path = '/usr/lib/ccache:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin:/usr/games';

# Builder identity
\$mailfrom = '${MBD_AUTOBUILD_MAINTAINER} on $(hostname -f) <${mbd_mail}>';
\$mailto = '${mbd_mail}';
\$maintainer_name = '${MBD_AUTOBUILD_MAINTAINER}';
\$pgp_options = ${SBUILD_PGP_OPTIONS};

# don't remove this, Perl needs it:
1;
EOF
${MBD_LOG} -s "I: ${MBD_SBUILDCONFIGFILE} updated."

${MBD_LIB}/mbd-update-common

# Make some items accessible via http
${MBD_LOG} -s "I: $(ln -s -f -v ../bld public_html/ 2>&1)"

mbdUpdateSshKeyring "${mbd_rephost}"

# Update local configuration dir on builder-only hosts
if [ ! -e "/etc/init.d/mini-buildd-rep" ]; then
	# -bld w/o -rep only; retrieve local config from rephost.
	${MBD_LOG} -s "I: Builder host only: Mirroring local config from repo host ${mbd_rephttphost}."
	rm -rf "${MBD_LOCALCONFIG}"
	wget --quiet --mirror --no-parent --no-host-directories --reject '*html*' --directory-prefix="${MBD_LOCALCONFIG}" \
		--cut-dirs 2 "http://${mbd_rephttphost}/~mini-buildd/config-local/"
fi

# 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}"

			(
				echo "I: ${MBD_TMP_CHROOT}: Updating..."
				mbdAptEnv
				for hook in $(find ${MBD_LIB}/chroots-update.d/ -name "*.hook" | sort) $(find ${MBD_HOME}/.mini-buildd/chroots-update.d/ -name "*.hook" | sort); do
					echo "I: ${MBD_TMP_CHROOT}: Running chroot update hook \"${hook}\"..."
					if ( echo "set -e" && cat "${hook}" ) | schroot -c "${MBD_TMP_CHROOT}-source" -u root; then
						echo "I: ${MBD_TMP_CHROOT}: Chroot update hook \"${hook}\" succeeded."
					else
						echo "E: ${MBD_TMP_CHROOT}: Chroot update hook \"${hook}\" FAILED."
					fi
				done
			) 2>&1 | ${MBD_LOG} -s
		fi
	done
done

exit 0
