+# early helper functions {{{
+GOOD='\e[32;01m'
+WARN='\e[33;01m'
+BAD='\e[31;01m'
+NORMAL='\e[0m'
+HILITE='\e[36;01m'
+BRACKET='\e[34;01m'
+
+einfo() {
+ einfon "$1\n"
+ return 0
+}
+
+einfon() {
+ [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
+ printf " ${GOOD}*${NORMAL} $*"
+ LAST_E_CMD=einfon
+ return 0
+}
+
+eerror() {
+ [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
+ printf " ${BAD}*${NORMAL} $*\n"
+ LAST_E_CMD=eerror
+ return 0
+}
+
+eend() {
+ local retval="${1:-0}"
+ shift
+ if [ $retval -gt 0 ]; then
+ printf " ${BAD}-> Failed (rc=${retval})${NORMAL}\n"
+ fi
+ return $retval
+}
+
+check4root(){
+ if [ "$(id -u 2>/dev/null)" != 0 ] ; then
+ echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
+ fi
+}
+
+check4progs(){
+ local RC=''
+ for arg in $* ; do
+ which $arg >/dev/null 2>&1 || RC="$arg"
+ done
+ if [ -n "$RC" ] ; then
+ echo "$RC not installed"
+ return 1
+ fi
+}
+# }}}
+
+# helper functions {{{
+cleanup() {
+ if [ -n "$CHROOT_VARIABLES" ] ; then
+ einfo "Removing ${CHROOT_VARIABLES}" ; rm "$CHROOT_VARIABLES" ; eend $?
+ fi
+
+ if [ -n "$STAGES" ] ; then
+ einfo "Removing ${STAGES}" ; rmdir "$STAGES" ; eend $?
+ fi
+
+ # Remove temporary mountpoint again
+ if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
+ rmdir "$MNTPOINT" 2>/dev/null
+ fi
+
+ # make sure $TARGET is not mounted when exiting grml-debootstrap
+ if [ -n "$MNTPOINT" ] ; then
+ if grep -q "$MNTPOINT" /proc/mounts ; then
+ # make sure nothing is left inside chroot so we can unmount it
+ [ -x "$MNTPOINT"/etc/init.d/ssh ] && "$MNTPOINT"/etc/init.d/ssh stop
+ [ -x "$MNTPOINT"/etc/init.d/mdadm ] && "$MNTPOINT"/etc/init.d/mdadm stop
+
+ [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a >/dev/null 2>&1
+
+ # ugly, but make sure we really don't leave anything (/proc /proc is intended)
+ for ARG in /sys /proc /proc ; do
+ [ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1
+ umount "$MNTPOINT"/$ARG >/dev/null 2>&1
+ done
+ umount "$MNTPOINT"/dev >/dev/null 2>&1
+
+ if [ -n "$ISODIR" ] ; then
+ [ -d "$MNTPOINT/$ISODIR" ] && umount "$MNTPOINT/$ISODIR" >/dev/null 2>&1
+ fi
+
+ if [ -n "$DIRECTORY" ] ; then
+ einfo "Not unmounting $MNTPOINT as you requested me to install into a directory of your own choice." ; eend 0
+ else
+ einfo "Unmounting $MNTPOINT"
+ umount "$MNTPOINT"
+ eend $?
+ fi
+
+ if [ -n "$STAGES" ] ; then
+ echo -n "Removing stages directory ${STAGES}: "
+ rm -rf "$STAGES" && echo done
+ fi
+
+ # remove directory only if we used the default with process id inside the name
+ if echo "$MNTPOINT" | grep -q '/mnt/debootstrap\.' ; then
+ einfo "Removing directory ${MNTPOINT}"
+ rmdir "$MNTPOINT"
+ eend $?
+ fi
+ fi
+ fi
+
+ if [ -n "${ORIG_TARGET}" ] ; then
+ einfo "Removing loopback mount of file ${ORIG_TARGET}."
+ kpartx -d "${ORIG_TARGET}" ; eend $?
+ fi
+}
+
+# we want to exit smoothly and clean:
+bailout(){
+
+ cleanup
+
+ [ -n "$1" ] && EXIT="$1" || EXIT="1"
+ [ -n "$2" ] && einfo "Notice: remove $STAGES/$2 to reexecute the stage"
+
+ exit "$EXIT"
+}
+trap bailout HUP INT QUIT TERM
+
+# we want to execute all the functions only once, simple check for it:
+stage() {
+ if [ -n "$2" ] ; then
+ echo "$2" > "${STAGES}/${1}"
+ return 0
+ elif grep -q done "${STAGES}/${1}" 2>/dev/null ; then
+ ewarn "Notice: stage $1 has been executed already, skipping execution therefore." ; eend 0
+ ewarn " To reexecute it clean up the according directory inside $STAGES" ; eend 0
+ return 1
+ fi
+}
+# }}}
+