#!/bin/bash -e

. $(dirname $0)/mbd-common.sh
mbd_opt_init "Mini-buildd: Repository update script."
mbd_opt_add "p" "Special handling for debian postinstall run."
mbd_opt_parse "$@"
mbdCheckUser mini-buildd

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

mkdir -p "rep" "log" ".mini-buildd/apt-secure.d" ".mini-buildd/debconf-preseed.d" ".mini-buildd/chroots-update.d"

# Add README for .mini-buildd/
cat <<EOF >.mini-buildd/README
Automatically produced by $(basename $0) on $(date).
Manual changes to this file are NOT preserved.

README for "~/.mini-buildd/": Place for local configuration

DO CHANGES ON THE REPOSITORY HOST ONLY. On builder-only hosts,
this directory is SYNCED from the repository host.

Preinstall hook
=====================================
Putting an executable file "preinstall" here will run this with
the full path to a "build" (i.e., all tests passed, to-be
installed) changes-file.

You may use this as temporary workaround to dput packages to
other repositories or to additionally use another package
manager like reprepro in parallel.

Base chroot maintenance customization
=====================================
Note that you only need any customization if you need to
apt-secure extra sources (for example bpo) or have other special
needs (like pre-seeding debconf variables).

 * "apt-secure.d/*.key":
   What   : Apt-secure custom keys for extra sources; keys are added to all base chroots.
   Used by: mbd-update-bld (/usr/share/mini-buildd/chroots-update.d/05_apt-secure).
   Note   : Don't touch auto-generated key 'auto-mini-buildd.key'.
 * "debconf-preseed.d/*.conf":
   What   : Pre-defined values for debconf (see debconf-set-selections).
   Used by: mbd-update-bld (/usr/share/mini-buildd/chroots-update.d/20_debconf-preseed).
   Note   : One noteable use case are licenses from non-free like in the sun-java packages.
 * "chroots-update.d/*.hook":
   What   : Custom hooks (shell snippets). Run in all base chroots as root (!).
   Used by: mbd-update-bld.
EOF

MBD_TMP_DISTS=$(mbdD2SList "${mbd_dists}")
cat <<EOF >"${MBD_MDINSTALLCONFIGFILE}"
[DEFAULT]
archivedir = ${MBD_HOME}/rep
mail_to = ${mbd_mail}

trigger_reindex = 1
mail_on_success = 0
# @todo Default is on when debian-keyring package is installed; we don't want this enabled ever (yet)
verify_sigs = 0

incoming_permissions = 0755

pre_install_script = ${MBD_LIB}/mbd-preinstall
#post_install_script = ${MBD_LIB}/postinstall
release_signscript = ${MBD_LIB}/mbd-sign-release

architectures = all, ${mbd_archs}
archive_style = flat
generate_release = 1
chown_changes_files = 0
experimental_release = 1
release_origin = Mini-Buildd for ${mbd_id} on ${mbd_rephost}

$(for d in ${MBD_TMP_DISTS}; do
   echo "[${d}-${mbd_id}]";
   echo "release_description = ${d} packages for ${mbd_id}.";
   echo "release_label = ${d}-${mbd_id}";
   echo "keep_old = 1";
   echo;
   echo "[${d}-${mbd_id}-experimental]";
   echo "release_description = EXPERIMENTAL ${d} packages for ${mbd_id}.";
   echo "release_label = ${d}-${mbd_id}-experimental";
   echo "keep_old = 0";
   echo;
 done)
EOF
${MBD_LOG} -s "I: ${MBD_MDINSTALLCONFIGFILE} updated."

${MBD_LIB}/mbd-update-common
${MBD_LIB}/mbd-update-rep-html

# Update repo-host key for 05_apt-secure
cp "public_html/pgp_key.asc" ".mini-buildd/apt-secure.d/auto-mini-buildd.key"

# Run mbd-update-bld on all build hosts
for host in $(mbdGetBldHosts); do
	mbdUpdateSshKeyring "${host}"
	${MBD_LOG} -s "I: Updating build host ${host}..."
	if ! ssh -o StrictHostKeyChecking="no" -p "${mbd_sshport}" "${host}" "${MBD_LIB}/mbd-update-bld"; then
		${MBD_LOG} -s "E: Updating build host ${host} FAILED (ignore if not set up yet)"
	fi
done

if ! mbd_opt_given p; then
	${MBD_LOG} -s "I: $(/etc/init.d/mini-buildd-rep restart 2>&1)"
fi

exit 0
