#!/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/conf" "rep/incoming" "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_REPREPRO_DISTSFILE}"
$(for d in ${MBD_TMP_DISTS}; do
   echo "Codename: ${d}-${mbd_id}-experimental";
   echo "Suite: ${d}-${mbd_id}-experimental";
   echo "Label: ${d}-${mbd_id}-experimental";
   echo "Origin: mini-buildd-${mbd_id}";
   echo "Components: main contrib non-free";
   echo "Architectures: source $(mbdD2SList "${mbd_archs}")";
   echo "Description: EXPERIMENTAL ${d} packages for ${mbd_id}.";
   echo "SignWith: default";
   echo "NotAutomatic: yes";
   echo;
   echo "Codename: ${d}-${mbd_id}-unstable";
   echo "Suite: ${d}-${mbd_id}-unstable";
   echo "Label: ${d}-${mbd_id}-unstable";
   echo "Origin: mini-buildd-${mbd_id}";
   echo "Components: main contrib non-free";
   echo "Architectures: source $(mbdD2SList "${mbd_archs}")";
   echo "Description: Unstable ${d} packages for ${mbd_id}.";
   echo "SignWith: default";
   echo "NotAutomatic: yes";
   echo "ButAutomaticUpgrades: yes";
   echo;
   echo "Codename: ${d}-${mbd_id}-testing";
   echo "Suite: ${d}-${mbd_id}-testing";
   echo "Label: ${d}-${mbd_id}-testing";
   echo "Origin: mini-buildd-${mbd_id}";
   echo "Components: main contrib non-free";
   echo "Architectures: source $(mbdD2SList "${mbd_archs}")";
   echo "Description: Testing ${d} packages for ${mbd_id}.";
   echo "SignWith: default";
   echo "NotAutomatic: yes";
   echo "ButAutomaticUpgrades: yes";
   echo;
   echo "Codename: ${d}-${mbd_id}-stable";
   echo "Suite: ${d}-${mbd_id}-stable";
   echo "Label: ${d}-${mbd_id}-stable";
   echo "Origin: mini-buildd-${mbd_id}";
   echo "Components: main contrib non-free";
   echo "Architectures: source $(mbdD2SList "${mbd_archs}")";
   echo "Description: Stable ${d} packages for ${mbd_id}.";
   echo "SignWith: default";
   echo "NotAutomatic: yes";
   echo "ButAutomaticUpgrades: yes";
   echo;
 done)
EOF
${MBD_LOG} -s "I: ${MBD_REPREPRO_DISTSFILE} updated."

cat <<EOF >"${MBD_REPREPRO_INCOMINGFILE}"
Name: INCOMING
TempDir: /tmp
IncomingDir: ${MBD_INCOMING}
Allow: $(for d in ${MBD_TMP_DISTS}; do echo -n "${d}-${mbd_id}-unstable ${d}-${mbd_id}-experimental "; done)
EOF
${MBD_LOG} -s "I: ${MBD_REPREPRO_INCOMINGFILE} 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"

# Update all indices (or create on initial install) via reprepro
(cd rep && reprepro clearvanished && reprepro export)

# 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
