+# }}}
+
+# ask for Debian mirror {{{
+prompt_for_mirror()
+{
+ [ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net'
+
+ CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \
+ --menu "Where do you want to install from?" 0 0 0 \
+ net "install via network (downloading from mirror)" \
+ local "install from local directory/mirror"
+ )
+
+ if [ "$CHOOSE_MIRROR" = 'net' ] ; then
+ [ -n "$MIRROR" ] || MIRROR='http://cdn.debian.net/debian'
+ MIRROR="$(dialog --stdout --title "${PN}" --inputbox \
+ "Please enter Debian mirror you would like to use for installing packages." \
+ 0 0 $MIRROR)"
+ else # CHOOSE_MIRROR == local
+ [ -n "$ISO" ] || ISO='/mnt/mirror'
+ ISO="$(dialog --stdout --title "${PN}" --inputbox \
+ "Please enter directory name you would like to use for installing packages." \
+ 0 0 $ISO)"
+ fi
+}
+# }}}
+
+# software raid setup {{{
+config_swraid_setup()
+{
+TMPFILE=$(mktemp)
+
+# Currently we support only raid1:
+RAIDLEVEL='raid1'
+
+#RAIDLEVEL=$(dialog --stdout --title "$PN" --default-item raid1 \
+# --menu "Which RAID level do you want to use?" 0 0 0 \
+# raid1 "Software RAID level 1" \
+# raid5 "Software RAID level 5")
+#[ $? -eq 0 ] || bailout 20
+
+MD_LIST=$(for i in $(seq 0 9) ; do
+ awk '{print $4}' /proc/partitions | grep -q md$i || \
+ echo "/dev/md$i /dev/md$i"
+ done)
+
+TARGET=$(dialog --stdout --title "$PN" --default-item /dev/md0 \
+--menu "Which device do you want to use for ${RAIDLEVEL}?
+
+Notice: activated devices will not be listed for security reasons. Anyway, please make sure the selected device is not in use already!" 0 0 0 \
+$MD_LIST)
+[ $? -eq 0 ] || bailout 20
+
+AVAILABLE_PARTITIONS=$(LANG=C fdisk -l 2>/dev/null | \
+ sed 's/*//' | \
+ grep -v 'Extended$' | \
+ gawk -v num=0 -v ORS=' ' '/^\/dev\// {print $1}')
+[ -n "$AVAILABLE_PARTITIONS" ] || echo "FIXME: no partitions available?"
+PARTITION_LIST=$(for i in $(echo $AVAILABLE_PARTITIONS) ; do
+ echo "$i $(blkid -s TYPE -o value $i 2>/dev/null || echo [no_filesystem_yet]) off"
+ done)
+
+dialog --title "$PN" --separate-output \
+ --checklist "Please select the partitions you would like to use for your $RAIDLEVEL on ${TARGET}:" 0 0 0 \
+ $PARTITION_LIST 2>$TMPFILE
+RETVAL=$?
+SELECTED_PARTITIONS="$(cat $TMPFILE)"
+
+NUM_PARTITIONS=0
+for i in $(cat $TMPFILE) ; do
+ NUM_PARTITIONS=$((${NUM_PARTITIONS}+1))
+done
+
+# force metadata version 0.90 for lenny so old grub can boot from this array.
+METADATA_VERSION=""
+if [ $RELEASE = "lenny" ]; then
+ METADATA_VERSION="-e0"
+fi
+
+ERRORFILE=$(mktemp)
+set +e
+# TODO: better error handling?
+yes | mdadm --create "${TARGET}" --level="${RAIDLEVEL}" \
+ --raid-devices="${NUM_PARTITIONS}" ${METADATA_VERSION} ${SELECTED_PARTITIONS} >/dev/null 2>$ERRORFILE
+RC=$?
+set -e
+
+if [ "$RC" = 0 ] ; then
+ dialog --title "$PN" --msgbox \
+ "Creating $TARGET was successful." 0 0
+ rm -f "$TMPFILE" "$ERRORFILE"
+else
+ dialog --title "$PN" --msgbox \
+ "There was an error setting up $TARGET:
+
+$(cat $ERRORFILE)
+
+Exiting." 0 0
+ rm -f "$TMPFILE" "$ERRORFILE"
+ exit 1
+fi
+
+}
+
+prompt_for_swraid()
+{
+if dialog --stdout --title "$PN" \
+ --defaultno --yesno "Do you want to configure Software RAID?
+
+Please notice that only RAID level 1 is supported by ${PN} currently. Configuration will take place using mdadm." 0 0 ; then
+ config_swraid_setup
+fi
+}
+# }}}
+
+# user should recheck his configuration {{{
+# support full automatic installation:
+checkforrun() {
+ dialog --timeout 10 --title "$PN" \
+ --yesno "Do you want to stop at this stage?
+
+Notice: you are running ${PN} in non-interactive mode.
+${PN} will install Debian ${RELEASE} on ${TARGET}.
+Last chance to quit. Timeout of 10 seconds running....
+
+Do you want to stop now?" 0 0 2>/dev/null
+}
+
+# make sure the user is aware of the used configuration {{{
+checkconfiguration()
+{
+if [ -n "$AUTOINSTALL" ] ; then
+ if checkforrun ; then
+ eerror "Exiting as requested" ; eend 0
+ exit 1
+ fi
+elif [ -n "$INTERACTIVE" ] ; then
+
+ INFOTEXT="Please recheck configuration before execution:
+ "
+ [ -n "$TARGET" ] && INFOTEXT="$INFOTEXT
+ Target: $TARGET"
+ [ -n "$GRUB" ] && INFOTEXT="$INFOTEXT
+ Install grub: $GRUB"
+ [ -n "$RELEASE" ] && INFOTEXT="$INFOTEXT
+ Using release: $RELEASE"
+ [ -n "$HOSTNAME" ] && INFOTEXT="$INFOTEXT
+ Using hostname $HOSTNAME"
+ [ -n "$MIRROR" ] && INFOTEXT="$INFOTEXT
+ Using mirror: $MIRROR"
+ [ -n "$ISO" ] && INFOTEXT="$INFOTEXT
+ Using ISO: $ISO"
+
+ INFOTEXT="$INFOTEXT
+
+Is this ok for you? Notice: selecting 'No' will exit ${PN}."
+
+ dialog --title "$PN" --no-collapse \
+ --yesno "$INFOTEXT" 0 0
+
+else # if not running automatic installation display configuration and prompt for execution:
+ einfo "$PN - Please recheck configuration before execution:"
+ echo
+ echo " Target: $TARGET"
+
+ # do not display if MNTPOINT is the default one
+ case "$MNTPOINT" in /mnt/debootstrap*) ;; *) echo " Mount point: $MNTPOINT" ;; esac
+
+ [ -n "$GRUB" ] && echo " Install grub: $GRUB" || echo " Install grub: no"
+ [ -n "$RELEASE" ] && echo " Using release: $RELEASE"
+ [ -n "$MIRROR" ] && echo " Using mirror: $MIRROR"
+ [ -n "$HOSTNAME" ] && echo " Using hostname: $HOSTNAME"
+ [ -n "$ISO" ] && echo " Using ISO: $ISO"
+
+ echo " Important! Continuing will delete all data from ${TARGET}!"
+
+ echo
+ einfon "Is this ok for you? [y/N] "
+ read a
+ if ! [ "$a" = 'y' -o "$a" = 'Y' ] ; then
+ eerror "Exiting as requested." ; eend 1
+ exit 1
+ fi
+fi
+}
+# }}}
+
+# interactive mode {{{
+interactive_mode()
+{
+ welcome_dialog
+
+ prompt_for_release
+
+ prompt_for_swraid
+
+ prompt_for_target
+
+ prompt_for_bootmanager
+
+ prompt_for_hostname
+
+ prompt_for_password
+
+ prompt_for_mirror
+}
+
+# run interactive mode if we didn't get the according configuration yet
+if [ -z "$TARGET" -o -n "$INTERACTIVE" ] ; then
+ # only target might be unset, so make sure the INTERACTIVE flag is set as well
+ INTERACTIVE=1
+ interactive_mode
+fi
+# }}}
+
+checkconfiguration