#!/bin/sh
#
# bootcd2disk
#
# DISK=auto, we will build an boot partition with size defined in BOOTS
  
TODOY="-y"
TODO=""
ENV_IMAGEURL=""
VERBOSE=""
SCRIPT=""
CONFDIR="/etc/bootcd"
ONLYMOUNT=""
ONLYUNMOUNT=""
CHROOTDIR=""
COPYDEST="/mnt"

# function: usage
# get: $1 -- error string
usage()
{
  [ "$1" ] && echo -e >&2 "\n$1\n"

  echo -e >&2 "\n $(basename $0) copy a system running from bootcd to a disk."
  echo -e >&2 " syntax:  -i           -- interactiv operation"
  echo -e >&2 "          -v           -- be noisy"
  echo -e >&2 "          -s           -- be silent and no interaction"
  echo -e >&2 "          -c <confdir> -- use another configuration directory"
  echo -e >&2 "          -url <url>   -- url from imageserver"
  echo -e >&2 "                          (overwrite IMAGEURL and USEIMAGESERVER)"
  echo -e >&2 "          -onlymount   -- only mount disks"
  echo -e >&2 "          -onlyunmount -- only unmount disks"
  echo -e >&2 "          -mkchroot <dir> -- bootcd will be copied to <dir>/chroot"
  echo -e >&2 ""
  exit 1
}


# get options
while [ "$1" ]; do
  case "$1" in
     "-h"|"--help")
        usage
        ;;
     "-i")
        TODOY=""
	shift
        ;;
   "-url")
        ENV_IMAGEURL="$2"
        shift 2
        ;;
     "-v")
        VERBOSE="$1"
	shift
        ;;
     "-c")
        CONFDIR="$2"
        shift 2
        ;;
     "-s")
        SCRIPT="$1"
	shift
        ;;
     "-onlymount")
        ONLYMOUNT="$1"
	shift
        ;;
     "-onlyunmount")
        ONLYUNMOUNT="$1"
	shift
        ;;
     "-mkchroot")
        CHROOTDIR="$2"
	COPYDEST="$CHROOTDIR/chroot"
	shift 2
	;;
        *)
        usage "ERROR: option \"$1\" unknown!"
        ;;
  esac
done

BOOTCD2DISKLIB=./bootcd2disk.lib
if [ -f $BOOTCD2DISKLIB ]; then
  [ "$SCRIPT" ] || echo "Using local $BOOTCD2DISKLIB" >&2
  . $BOOTCD2DISKLIB $*
else
  BOOTCD2DISKLIB=/usr/share/bootcd/bootcd2disk.lib
  if [ -f $BOOTCD2DISKLIB ];then
    . $BOOTCD2DISKLIB $*
  else
    echo "Error: Mandatory lib $BOOTCD2DISKLIB not found! ABORT!"
    exit 1
  fi
fi

DO_BEFORE_COPY=$(egrep "^[[:space:]]*(function)*[[:space:]]*before_copy" $CONFDIR/bootcd2disk.conf)
DO_AFTER_COPY=$(egrep "^[[:space:]]*(function)*[[:space:]]*after_copy" $CONFDIR/bootcd2disk.conf)

# getdata - print without empty lines and comments
getdata()
{
  echo "$*" | sed "s/#.*$//" | grep -v "^$"
}

### Main ###

domount()
{
  echo "--- mounting DISK ---" |
    if [ "$SCRIPT" ]; then cat >> $ERRLOG; else tee -a $ERRLOG >&2; fi
  run "$MOUNT"
}

excludemnt()
{
  #find mountpoints and exclude them, otherwise cpio might complain
  CPIOEXMOUNT="$(mount| awk '/ \/mnt\// {gsub(/\/mnt\//,"/");{print $3}}')"
  CPIOEXCLUDE=""
  if [ ! -z "$CPIOEXMOUNT" ];then
    for AEXCLUDE in $CPIOEXMOUNT; do
      CPIOEXCLUDE="$CPIOEXCLUDE -e '^$AEXCLUDE\$'"
    done
  fi
}

dounmount()
{
  echo "--- unmounting DISK ---" |
    if [ "$SCRIPT" ]; then cat >> $ERRLOG; else tee -a $ERRLOG >&2; fi
  run cp $ERRLOG $COPYDEST/var/log
  run "$UMOUNT"
}

if [  "$ONLYMOUNT" ]; then
  domount
  exit $? 
elif [  "$ONLYUNMOUNT" ]; then
  dounmount
  exit $?
fi

A=""
while [ "$A" != "y" -a "$A" != "n" -a "$SCRIPT" == "" ]
do
  if [ "$CHROOTDIR" ]; then
    echo "Directory $CHROOTDIR will be deleted and created again! " | tee -a $ERRLOG >&2
    [ "$DO_BEFORE_COPY" ] && echo "do_before_copy is defined." | tee -a $ERRLOG >&2
    [ "$DO_AFTER_COPY" ] && echo "do_after_copy is defined." | tee -a $ERRLOG >&2
    [ "$RESTORECMD" ] && echo "RESTORECMD is defined." | tee -a $ERRLOG >&2
  else
    for i in $DISKVARS; do
      echo "Harddisk $i ($(eval "echo \"\$DISK$i\"")) will be erased!!!" |
        tee -a $ERRLOG >&2
    done
    [ "$SWAP" ] && echo "Partition $SWAP will be newly created as SWAP !!!" | tee -a $ERRLOG >&2
    [ "$EXT2FS" ] && echo "Partition $EXT2FS will be newly created as $OPT !!!" | tee -a $ERRLOG >&2
    [ "$VFAT" ] && echo "Partition $VFAT will be newly created as vfat !!!" | tee -a $ERRLOG >&2
  fi
  echo -n "(y/n) " | tee -a $ERRLOG >&2
  if [ "$SCRIPT" ]; then
    A="y"
  else
    read A
  fi
  echo "$A" >> $ERRLOG >&2
done
if [ "$A" = "n" ]; then
  exit 1
fi

trap cleanup 2

lvmreset()
{
  if [ "$LVMGRP" -a "$LVMVOL" ]; then

    # delete all pvdisplay | grep "PV Name"
    ALLPV="$(pvdisplay -c 2>/dev/null | 
      sed -n "s|[[:blank:]]*\(/dev/[^:]*\):.*|\1|p")"
    echo "All physical Volumes ALLPV=<$ALLPV>" >>$ERRLOG

    for i in $DISKVARS; do
      # Search for old Partitions on reused disks
      DELPV="$(echo "$ALLPV" | grep "^$(eval "echo \"\$DISK$i\"")")"
      # All on one line
      DELPV="$(echo $DELPV|awk '{printf("%s ",$0)}')"

      if [ "$(echo "$DELPV" | grep "[[:alnum:]]")" ]; then
        # Try to use pvremove first (ignore anything, only write to log)
        ignore ".*"
        run "pvremove -ff -y $DELPV"
      fi

      # Add the disk itself
      DELPV="$DELPV $(eval "echo \"\$DISK$i\"")"

      echo "--- Destroy old disk infos $DELPV ---" | tee -a $ERRLOG >&2
      ignore "1+0 records in"
      ignore "1+0 records out"
      ignore "1024 bytes.*copied.*seconds"
      run "
        for dsk in $DELPV; do
          dd if=/dev/zero of=\$dsk bs=1k count=1
        done
      "
    done
  fi
}
if [ \( ! "$CHROOTDIR" \) -a "$LVMGRP" -a "$LVMVOL" ]; then
  TODO="$TODO \"lvmreset # deactivate lvm, delete lvm info on disk\""
fi

# partdisk <nr>
# Partition DISK<nr> as defined in SFDISK<nr>
partdisk()
{
  local d=$(eval "echo \"\$DISK$1\"")
  local s=$(eval "echo \"\$SFDISK$1\"")
  local o=$(eval "echo \"\$SFDISKOPTS$1\"")

  echo "--- Partitioning DISK$1 ($d) ---" | tee -a $ERRLOG >&2
  ignore "^ $d: unrecognized partition table type"
  ignore "^sfdisk: ERROR: sector .* does not have an msdos signature"
  ignore "^No partitions found"
  run "echo \"$s\" | sfdisk $o -qL -uM $d >/dev/null"
}
if [ ! "$CHROOTDIR" ]; then
  for i in $DISKVARS; do
    TODO="$TODO \"partdisk $i # partition Disk $(eval "echo \"\$DISK$i\"")\""
  done
fi

lvm_pvcreate()
{
  local TODO=""

  echo "--- pvcreate ---" | tee -a $ERRLOG >&2
  # After Disk Partitioning, there could be new places with old
  # LVM Info, so we delete it before running pvcreate
  TODO="$TODO $(
    getdata "$LVMGRP" | sed "s/vgcreate.*//" |
    while read grp dsks; do
      for dsk in $dsks; do
        echo -n " \"dd if=/dev/zero of=$dsk bs=1k count=1\""
        echo -n " \"pvcreate --quiet -d -ff -y $dsk\""
      done
    done
  )"

  if [ "$TODO" ]; then
    if [ "$TODOY" ]; then
      ignore "WARNING: Forcing physical volume creation on .* of volume group \".*\""
      ignore "Physical volume \".*\" successfully created"
      ignore "^[[:digit:]]\++[[:digit:]]\+ records in$"
      ignore "^[[:digit:]]\++[[:digit:]]\+ records out$"
      ignore "^[[:digit:]]\+ bytes\>.*\<copied\>.*\<seconds\>.*"
      run "interactive -y $TODO"
    else
      eval "interactive $TODO"
    fi
  fi
}
if [ \( ! "$CHROOTDIR" \) -a "$LVMGRP" -a "$LVMVOL" ]; then
  TODO="$TODO \"lvm_pvcreate\""
fi

lvm_vgcreate()
{
  local TODO=""

  echo "--- vgcreate ---" | tee -a $ERRLOG >&2
  TODO="$TODO $(
    getdata "$LVMGRP" |
    while read grp dsks; do
      cmd="$(echo "$dsks"|sed "s/.*\(vgcreate.*\)/\1/"|grep vgcreate)"
      if [ "$cmd" ]; then
        echo -n " \"$cmd\""
      else
        echo -n " \"vgcreate --quiet $grp $dsks\""
      fi
      echo -n " \"vgchange --quiet -aey $grp\""
    done
  )"
  if [ "$TODO" ]; then
    if [ "$TODOY" ]; then
      ignore "Volume group \".*\" successfully created"
      ignore "0 logical volume(s) in volume group \".*\" now active"
      run "interactive -y $TODO"
    else
      eval "interactive $TODO"
    fi
  fi
}
if [ \( ! "$CHROOTDIR" \) -a "$LVMGRP" -a "$LVMVOL" ]; then
  TODO="$TODO \"lvm_vgcreate\""
fi

lvm_lvcreate()
{
  local TODO=""
  echo "--- lvcreate ---" | tee -a $ERRLOG >&2

  TODO="$TODO $(
    getdata "$LVMVOL" |
    while read vol s grp cmd; do
      if [ "$cmd" ]; then
        echo -n " \"$cmd\""
      else
        echo -n " \"lvcreate -n $vol -L $s $grp\""
      fi
    done
  )"

  if [ "$TODO" ]; then
    if [ "$TODOY" ]; then
      ignore "Logical volume \".*\" created"
      stdout "Rounding up size to full physical extent"
      run "interactive -y $TODO"
    else
      eval "interactive $TODO"
    fi
  fi
}
if [ \( ! "$CHROOTDIR" \) -a "$LVMGRP" -a "$LVMVOL" ]; then
  TODO="$TODO \"lvm_lvcreate\""
fi

lvm_vgchange()
{
  echo "--- Activate LVM ---" | tee -a $ERRLOG >&2
  ignore "Reading all physical volumes.  This may take a while..."
  ignore "Found volume group \".*\" using metadata type lvm2"
  run vgscan --quiet
  ignore "[1-9][0-9]* logical volume(s) in volume group \".*\" now active"
  run vgchange --quiet -ay
}
if [ \( ! "$CHROOTDIR" \) -a "$LVMGRP" -a "$LVMVOL" ]; then
  TODO="$TODO \"lvm_vgchange\""
fi

vfat()
{
  for i in $VFAT; do
    echo "--- Building Filesystem $i with mkdosfs ---" | tee -a $ERRLOG >&2
    stdout "^mkdosfs "
    run "mkdosfs $i"
  done
}
if [ \( ! "$CHROOTDIR" \) -a "$VFAT" ]; then
  TODO="$TODO \"vfat # mkdosfs $VFAT\""
fi

extfs()
{
  OPT=""
  [ "$EXT3" = "yes" ] && OPT="-j"
  for i in $EXT2FS; do
    echo "--- Building Filesystem $i with mke2fs $OPT ---" | tee -a $ERRLOG >&2
    stdout "^mke2fs "
    run "mke2fs $OPT -q $i"
  done
}
if [ \( ! "$CHROOTDIR" \) -a "$EXT2FS" ]; then
  TODO="$TODO \"extfs # mke2fs $EXT2FS\""
fi

swap()
{
  for i in $SWAP; do
    echo "--- making SWAP $i ---" | tee -a $ERRLOG >&2
    stdout "^Setting up swapspace"
    stdout "^no label, "
    run "mkswap $i"
  done
}
if [ \( ! "$CHROOTDIR" \) -a "$SWAP" ]; then
  TODO="$TODO \"swap # mkswap $SWAP\""
  TODO="$TODO \"domount # mount all partitions to /mnt\""
fi

TODO="$TODO \"excludemnt\""

if [ "$CHROOTDIR" ]; then
  TODO="$TODO \"rm -rf $CHROOTDIR\""
  TODO="$TODO \"mkdir -p $COPYDEST\""
fi

# remove all interfaces from udev persistent rules file
# needed if you install on other machines
udev_fixnet()
{
  [ -e "/etc/udev/rules.d/z25_persistent-net.rules" ] && sed -i -ne "/^#.*/p" /etc/udev/rules.d/z25_persistent-net.rules
}

[ "$UDEV_FIXNET" = "yes" ] && TODO="$TODO udev_fixnet"

# use image from a image server and copy it to the later swap-partion
# the partition must be large enough to hold the whole disk image
copyfromserver_before()
{
  # timeout for dns-query and connection timeout (seconds)
  local timeout=30
  echo "--- copying image from image server ---" |
    if [ "$SCRIPT" ]; then cat >> $ERRLOG; else tee -a $ERRLOG >&2; fi

  # commandline overwrite the bootcd2disk.conf option!
  [ "$ENV_IMAGEURL" ] && local url=$ENV_IMAGEURL || local url=$IMAGEURL
 
  if [ ! "$url" ]; then
    echo "No parameter -url given and IMAGEURL not set!"| tee -a $ERRLOG >&2
    exit 2
  fi
  mke2fs $SWAP
  mkdir -p /tmp/swapfs
  mount $SWAP /tmp/swapfs

  if [ "$(echo "$url" | grep "^file://")" ]; then
    url="$(echo "$url" | sed "s|^file://\(.*\)$|\1|")"
    if [ -b "$url" ]; then
      dd if=$url of=/tmp/swapfs/cdimage.iso
    elif [ -f "$url" ]; then
      cp $url /tmp/swapfs/cdimage.iso
    else
      echo "No file <$url>" | tee -a $ERRLOG >&2
      exit 3
    fi
  else
    run "wget --no-check-certificate --dns-timeout=$timeout --connect-timeout=$timeout -O /tmp/swapfs/cdimage.iso $url"
  fi
  mkdir -p /tmp/isoloopback
  mount -o loop /tmp/swapfs/cdimage.iso /tmp/isoloopback
}

[ "$USEIMAGESERVER" = "yes" -o "$ENV_IMAGEURL" ] && TODO="$TODO copyfromserver_before"

do_before_copy()
{
  echo "--- do function before_copy ---" | tee -a $ERRLOG >&2
  before_copy
}

# only call the function, if it is defined
if [ "$DO_BEFORE_COPY" ]; then
  TODO="$TODO \"do_before_copy # run function before_copy defined in bootcd2disk.conf\""
fi

copycdram()
{
  echo "--- copying CD and RAM to $COPYDEST ---" | tee -a $ERRLOG >&2

  # Normally there is no /tmp/isoloopback after booting from CD. But it could be
  # created in function before_copy.
  if [ -d /tmp/isoloopback ]; then
    run "cd /tmp/isoloopback; find . -mount | sed 's|^./|/|g' | grep -v -e '^/var.ro\>' -e '^/var\>' $CPIOEXCLUDE | sed 's|^/|./|g' |cpio --quiet -pdum $COPYDEST"

  else
    run "find / -mount|grep -v -e '^/var.ro\>' -e '^/var\>' $CPIOEXCLUDE |cpio --quiet -pdum $COPYDEST"
  fi
  run "cd /var.ro; find . -mount | cpio --quiet -pdum $COPYDEST/var"
  run "find $COPYDEST/etc $COPYDEST/tmp $COPYDEST/dev $COPYDEST/home $COPYDEST/root -type l | xargs -r rm"
  run rm -r $COPYDEST/etc.ro $COPYDEST/tmp.ro $COPYDEST/dev.ro $COPYDEST/home.ro $COPYDEST/root.ro
  run cp -a -x /ram1/etc /ram1/tmp /ram1/home /ram1/root $COPYDEST
  if [ -d /dev/.static/dev -a -x /etc/init.d/udev ]; then
    echo "--- handling udev ---" 
    # -- udev --
    # The filesystem has static device files in /dev.
    # But tmpfs is mounted over /dev and hides the static device files.
    # The static device files are mounted again in /dev/.static/dev/.
    # This means bootcd has only to copy /dev/.static/dev.
    run cp -a -x /ram1/dev/.static/dev $COPYDEST
    run mkdir -p $COPYDEST/dev/.static
    run chown root:root $COPYDEST/dev/.static
    run chmod 700 $COPYDEST/dev/.static
  else
    run cp -a -x /ram1/dev $COPYDEST
  fi


  run rm -f $COPYDEST/fastboot $COPYDEST/cdboot.catalog $COPYDEST/cdboot.img $COPYDEST/ram1.cpio.gz $COPYDEST/ram2.cpio.gz
  run rm -rf $COPYDEST/rr_moved/ $COPYDEST/isolinux/
  run rm $COPYDEST/etc/rcS.d/S12bootcdram.sh \
    $COPYDEST/etc/rcS.d/S13bootcdflop.sh \
    $COPYDEST/usr/bin/bootcd2disk \
    $COPYDEST/usr/bin/bootcdmk2diskconf \
    $COPYDEST/usr/bin/bootcdflopcp \
    $COPYDEST/etc/bootcd/bootcd2disk.conf \
    $COPYDEST/etc/bootcd/thisbootcd.conf
  run rm -rf $COPYDEST/ram1 $COPYDEST/ram2

  echo "--- deleting NOT_TO_RAM symlinks ---" 
  NOT_TO_RAM_RO=$(find /ram1 /ram2 -xdev -type l | xargs /bin/ls -l | grep -e " -> /etc.ro/" -e " -> /home.ro/" -e " -> /root.ro/" | awk '{ print $(NF) }')
  for i in $NOT_TO_RAM_RO; do     # exp: i=/etc.ro/X11
    j=$(echo $i | sed "s/\.ro//") #   => j=/etc/X11
    run rm -f $COPYDEST/$j        #   => $COPYDEST/etc/X11 -> /etc.ro/X11
    [ -e $i ] && run cp -a $i $COPYDEST/$j
  done
}
if [ ! "$RESTORECMD" ]; then
  TODO="$TODO \"copycdram # copy cd and ram to $COPYDEST, delete unneeded stuff\""
fi

# configure the swap-partition back to swap
copyfromserver_after()
{
  # cleanup again
  umount /tmp/isoloopback
  # rebuild swap
  swap
}
if [ ! "$RESTORECMD" ]; then
[ "$USEIMAGESERVER" = "yes" -o "$ENV_IMAGEURL" ] && TODO="$TODO copyfromserver_after"
fi

if [ "$RESTORECMD" ]; then
  TODO="$TODO \"run \\\"$RESTORECMD\\\"\""
fi

do_after_copy()
{
  echo "--- do function after_copy ---" | tee -a $ERRLOG >&2
  after_copy
}
# only call the function, if it is defined
if [ ! -z "$DO_AFTER_COPY" ]; then
  TODO="$TODO \"do_after_copy # run function after_copy defined in bootcd2disk.conf\""
fi

do_fstab()
{
  if [ "$FSTAB" = "unchanged" ]; then
    echo "--- Will let fstab unchanged ---" | tee -a $ERRLOG >&2
  else
    echo "--- Building fstab ---" | tee -a $ERRLOG >&2
    run "echo \"$FSTAB\" > $COPYDEST/etc/fstab"

    echo "--- Resetting mtab ---" | tee -a $ERRLOG >&2
    run "rm -f $COPYDEST/etc/mtab"
  fi
}
TODO="$TODO \"do_fstab # create $COPYDEST/etc/fstab\""

bootloadercfg()
{
  if [ "$GRUB" = "unchanged" ]; then
    echo "--- Will let grub/menu.lst unchanged ---" | tee -a $ERRLOG >&2
  elif [ "$GRUB" ]; then
    echo "--- Building grub/menu.lst ---" | tee -a $ERRLOG >&2
    mkdir -p $COPYDEST/boot/grub/
    run "echo \"$GRUB\" > $COPYDEST/boot/grub/menu.lst"
  fi

  if [ "$LILO" = "unchanged" ]; then
    echo "--- Will let lilo.conf unchanged ---" | tee -a $ERRLOG >&2
  elif [ "$LILO" ]; then
    echo "--- Building lilo.conf ---" | tee -a $ERRLOG >&2
    run "echo \"$LILO\" > $COPYDEST/etc/lilo.conf"
  fi

  if [ "$ELILO" = "unchanged" ]; then
    echo "--- Will let elilo.conf unchanged ---" | tee -a $ERRLOG >&2
  elif [ "$ELILO" ]; then
    echo "--- Building elilo.conf ---" | tee -a $ERRLOG >&2
    run "echo \"$ELILO\" > $COPYDEST/etc/elilo.conf"
  fi
}
TODO="$TODO bootloadercfg"

createsshkeys()
{
  # each installed PC gets a unique hostkey (only if hostkey already existed)
  create_host_keys $COPYDEST/etc/ssh
}
if [ "$SSHHOSTKEY" = "unchanged" ]; then
  echo "--- Will let ssh keys unchanged ---" | tee -a $ERRLOG >&2
elif [ "$SSHHOSTKEY" = "yes" ]; then
  TODO="$TODO createsshkeys"
elif [ "$SSHHOSTKEY" != "no" ]; then
  warn 'SSHHOSTKEY is not defined as "yes" or "no".' \
       'It will be treated as "no".'
fi

enablecron()
{
  echo "--- Enabling turned off stuff ---" | tee -a $ERRLOG >&2
  for f in $DISABLE_CRON; do
    if [ -f $COPYDEST/$f.no_run_on_bootcd ]; then 
      run rm $COPYDEST/$f
      run mv $COPYDEST/$f.no_run_on_bootcd $COPYDEST/$f
    fi
  done
}
if [ ! "$RESTORECMD" ]; then
  if [ "$DISABLE_CRON" ]; then
    TODO="$TODO enablecron"
  fi
fi

dogrub()
{
  [ -z "$GRUBBOOTDIR" ] && GRUBBOOTDIR=0
  [ -z "$GRUBBOOTDISK" ] && GRUBBOOTDISK="hd0"
  echo "--- Running grub-install ---" | tee -a $ERRLOG >&2
  grub-install --no-floppy --root-directory=$COPYDEST $GRUBBOOTDISK

  if [ $? -eq 0 ]; then
    return 0
  fi
  echo "--- grub-install did not work trying option --recheck ---" | tee -a $ERRLOG >&2
  grub-install --recheck --no-floppy --root-directory=$COPYDEST $GRUBBOOTDISK

  if [ $? -eq 0 ]; then
    return 0
  fi
  echo "--- grub-install did not work trying grub ---" | tee -a $ERRLOG >&2
echo "root ($GRUBBOOTDISK,$GRUBBOOTDIR)
setup ($GRUBBOOTDISK)
quit
" | grub --no-floppy --batch
}
if [ \( ! "$CHROOTDIR" \) -a "$GRUB" ]; then
  TODO="$TODO \"dogrub # run grub-install --root-directory=$COPYDEST $GRUBBOOTDISK\""
fi

dolilo()
{
  echo "--- Running lilo ---" | tee -a $ERRLOG >&2
  run "chroot $COPYDEST mount /proc"
  stdout "^Added "
  run "chroot $COPYDEST lilo -w"
  run "chroot $COPYDEST umount /proc"
}
if [ \( ! "$CHROOTDIR" \) -a "$LILO" ]; then
  TODO="$TODO dolilo"
fi

doelilo()
{
  echo "--- Running elilo ---" | tee -a $ERRLOG >&2
  run "chroot $COPYDEST elilo"
}
if [ \( ! "$CHROOTDIR" \) -a "$ELILO" ]; then
  TODO="$TODO doelilo"
fi

if [ ! "$CHROOTDIR" ]; then
  TODO="$TODO dounmount"
fi

eval "interactive $TODOY $TODO"

echo "Please Reboot now !" | tee -a $ERRLOG >&2
