--- /dev/null
+#!/bin/bash
+set -e
+
+. /usr/share/debconf/confmodule
+
+db_capb backup # allow to go back, for that we implement that STATE loop
+
+debug()
+{
+ [ "${DEBCONF_DEBUG:-}" != "developer" ] || echo -e "D: $*" >&2
+}
+
+db_settitle neurodebian/title
+
+if [ "$1" = "configure" ]; then
+ # Must have been called in preinst state, so no nd-configurerepo
+ # tool is yet installed, thus delay asking questions to
+ # whenever will be called in postinst state
+ which nd-configurerepo 1>/dev/null 2>&1 || exit 0
+fi
+
+# Create temporary directory which will be reused
+export ND_AE_TEMPDIR=$(mktemp -d)
+trap "rm -rf \"$ND_AE_TEMPDIR\"" TERM INT EXIT
+
+neurodebian_releases="auto"
+
+# This implements a simple state machine so the back button can be handled.
+# taken from debconf demo example
+STATE=1
+while [ "$STATE" != 0 -a "$STATE" != 8 ]; do
+ case $STATE in
+ 1)
+ export neurodebian_releases="$(ND_IFS=', ' nd-configurerepo --print-releases)"
+ debug "releases: $neurodebian_releases"
+ db_subst neurodebian/release releases "$neurodebian_releases"
+ db_input medium neurodebian/release || true
+ ;;
+ 2)
+ export neurodebian_mirrors="$(ND_IFS=', ' nd-configurerepo --print-mirrors)"
+ debug "mirrors: $neurodebian_mirrors"
+ db_subst neurodebian/mirror mirrors "$neurodebian_mirrors"
+ db_input medium neurodebian/mirror || true
+ mirror_selection_ret="$RET"
+ debug "mirror select return: <$mirror_selection_ret>"
+ ;;
+ 3) db_input medium neurodebian/flavor || true ;;
+ 4) db_input medium neurodebian/components || true ;;
+ 5) db_input low neurodebian/overwrite || true ;;
+ 6) db_input low neurodebian/suffix || true ;;
+ 7) db_input medium neurodebian/run-update-note || true ;;
+ esac
+
+ if db_go; then
+# Ubuntus managed to remove netselect "to accompany apt-netselect"
+# https://bugs.launchpad.net/ubuntu/+source/netselect/+bug/337377
+# So for now remove this mandatory demanding of netselect for "best", and
+# rely on logic in nd-configurerepo to select default mirror (currently origin)
+# as the 'best'
+#
+# case $STATE in
+# 2)
+# db_metaget neurodebian/mirror value
+# selected_mirror="$RET"
+# # TODOs:
+# # - handle custom
+# # - separate out alias from url
+# debug "selected mirror: $selected_mirror"
+# if [ "$selected_mirror" = "best" ] && ! which netselect &>/dev/null; then
+# if [ "$mirror_selection_ret" = "30 question skipped" ]; then
+# # if the question was not even shown, we would
+# # not get a chance to specify mirror, thus
+# # better just fail altogether and demand intervention
+# db_input high neurodebian/netselect-cannot-be-used
+# exit 1
+# else
+# db_input high neurodebian/netselect-not-found
+# continue;
+# fi
+# fi
+# esac
+ STATE=$(($STATE + 1))
+ else
+ STATE=$(($STATE - 1))
+ fi
+done
+
+db_stop
+