#!/bin/bash
# Filename:      grml2hd-chrooted
# Purpose:       helper script for grml2hd
# Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>, (c) Andreas Gredler <jimmy@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2.
################################################################################

PN='grml2hd'

if [ -z "$GRML2HD" ] ; then
  echo "$0 not invoked using environment variable GRML2HD=1." >&2
  echo "Will not continue to avoid damage to your system. Exiting" >&2
  exit 1
fi

# source config:
if ! [ -z "$GRML2HD_NONINTERACTIVE" ] ; then
   [ -r /etc/grml2hd/config ] && . /etc/grml2hd/config
fi

# source variables:
[ -r /etc/grml2hd/grml2hdtmp ] && . /etc/grml2hd/grml2hdtmp

# source customization file:
if [ -r /etc/grml2hd/customization ] ; then
   . /etc/grml2hd/customization
else
   echo "Error: /etc/grml2hd/customization not found, exiting." >&2
   exit 1
fi

# make sure we have the usual functions for boot-related stuff available
. /etc/grml/autoconfig.functions

LANG='C'
LC_ALL='C'
LANGUAGE='C'

GRML_SMALL=''
grep -q small /etc/grml_version && GRML_SMALL='1'

checkvalue() {
  if [ "$1" = "yes" ] ; then
    return 0
  else
    return 1
  fi
}

is_grml2hd_noninteractive() {
  if ! [ -z "$GRML2HD_NONINTERACTIVE" ] ; then
     return 0 # we running automatic installation
  else
     return 1 # we are running interactive installatio
  fi
}

root_account() {
  shadowconfig on 2>>$DEBUG # redirect as it complains about:
  # "pwconf: failed to change the mode of /etc/passwd- to 0600"

  if [ -z "$GRML2HD_NONINTERACTIVE" ] ; then
     ROOTPW1='PW1'
     ROOTPW2='PW2'
     while [ "$ROOTPW1" != "$ROOTPW2" ]; do
       ROOTPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
       "Please enter the password for the root account:" 10 60)
       ROOTPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox \
       "Please enter the password for the root account again for \
       confirmation:" 10 60)

       if [ "$ROOTPW1" != "$ROOTPW2" ]; then
         $(dialog --stdout --title "${PN}" --ok-label \
         "Retry" --msgbox "Passwords do not match!" 10 60)
       fi
     done
       if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
         echo root:"$ROOTPW1" | chpasswd -m
       else
         echo root:"$ROOTPW1" | chpasswd
       fi
       export ROOTPW1=''
       export ROOTPW2=''
  else
    if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
      echo "root:$ROOTPASSWD" | chpasswd -m
    else
      echo "root:$ROOTPASSWD" | chpasswd
    fi
    export ROOTPASSWD=''
  fi
}

setlanguage() {
  is_grml2hd_noninteractive || /usr/sbin/grml-setkeyboard
  is_grml2hd_noninteractive || /usr/sbin/grml-setlang
}

xconfig() {
  if [ -z "$GRML_SMALL" -a -x /usr/bin/grml-x ] ; then
     if [ -n "$GRML2HD_NONINTERACTIVE" ] ; then
        # running grml in noninteractive mode and if $GRMLX is set:
        [ -n "$GRMLX" ] && grml-x --force --nostart fluxbox >/dev/null 2>&1
     else
        if ! [ -f /etc/X11/xorg.conf ] ; then
           dialog --stdout --title "${PN}" --defaultno --yesno "You do not have the configuration file /etc/X11/xorg.conf
for running the X window system. It is recommended to create
the file when running the live-CD system. grml-x can create
the file according to your hardware for you.

Create /etc/X11/xorg.conf?" 12 70
           if [[ $? == 0 ]]; then
              SUCCESS=''
              grml-x --force --nostart fluxbox >/dev/null && SUCCESS='1'
           fi
           if ! [ -z "$SUCCESS" ] ; then
              is_grml2hd_noninteractive || dialog --stdout --title "${PN}" --msgbox "Running grml-x was successful and you should have a
working X window system now. Run 'startx ~/.xinitrc'
as user to start the X." 8 60
           fi
        fi # [ -f /etc/X11/xorg.conf ]
     fi # GRML2HD_NONINTERACTIVE
  fi
  # recreate Xwrapper.config with defaults
  rm -f /etc/X11/Xwrapper.config
  echo reset x11-common/xwrapper/allowed_users | debconf-communicate >/dev/null
  echo reset x11-common/xwrapper/actual_allowed_users | debconf-communicate >/dev/null
  dpkg-reconfigure -phigh x11-common
}

sethostname() {
  if [ -z "$GRML2HD_NONINTERACTIVE" ] ; then
    if [ -x /usr/sbin/grml-hostname ] ; then
       GRML2HD=1 grml-hostname # set GRML2HD=1 so we tell grml-hostname we are running inside grml2hd
    fi
  else # non-interactive
    [ -n "$HOSTNAME" ] || HOSTNAME='grml'
    grml-hostname "$HOSTNAME"
  fi
}

newuser() {
  if [ -z "$GRML2HD_NONINTERACTIVE" ] ; then
    NEWUSER="$(dialog --stdout --title "${PN}" --inputbox "Please enter username \
(e.g. grml) for the main user you want to use on your grml system.

Please note that the user has some administrative rights on a few commands using the sudo mechanism.
If you want to change that behaviour please adjust sudo setup via running visudo as root.
" 15 60 grml)"
    REALNAME="$(dialog --stdout --title "${PN}" --inputbox "Your real name (eg'John GRML User'):" 10 60)"
  fi

# fix b0rken apps which don't allow relative paths for configuration:
  [ -r /home/grml/.gkrellm2/user-config ]  && sed -i "s#/home/grml#/home/$NEWUSER#g" /home/grml/.gkrellm2/user-config  2>/dev/null
  [ -r /home/grml/.gkrellm2/theme_config ] && sed -i "s#/home/grml#/home/$NEWUSER#g" /home/grml/.gkrellm2/theme_config 2>/dev/null
  [ -d /home/grml/.idesktop ] && sed -i "s#/home/grml#/home/$NEWUSER#g" /home/grml/.idesktop/*.lnk 2>/dev/null

  if [ -r /etc/grml/autoconfig ] ; then
     sed -i "s/CONFIG_FSTAB_USER=.*/CONFIG_FSTAB_USER='$NEWUSER'/" /etc/grml/autoconfig
  fi

  su -s /bin/sh grml -c "rsync -Ha --ignore-existing /etc/skel/ /home/grml/"

  if ! [ "$NEWUSER" == grml ] ; then
    sed -i "s/grml User/$REALNAME/" /etc/passwd
    sed -i "s/grml/$NEWUSER/g"      /etc/passwd
    sed -i "s/grml/$NEWUSER/g"      /etc/group
    sed -i "s/grml/$NEWUSER/g"      /etc/shadow
    sed -i "s/grml/$NEWUSER/g"      /etc/gshadow
    sed -i "s/uid=[a-z]*/uid=$NEWUSER/g"  /etc/fstab
    sed -i "s/gid=[a-z]*/gid=$NEWUSER/g"  /etc/fstab
    [ -f /etc/grml/screen_multisession ] && sed -i "s/grml/$NEWUSER/g" /etc/grml/screen_multisession
    [ -f /etc/aliases ]            && sed -i "s/grml/$NEWUSER/g" /etc/aliases
    [ -f /etc/sudoers ]            && sed -i "s/grml/$NEWUSER/g"      /etc/sudoers
    [ -f /etc/automount.sh ]       && sed -i "s/=grml/=$NEWUSER/g"    /etc/automount.sh
    [ -f /etc/samba/smb.conf ]     && sed -i "s#/home/grml#/home/${NEWUSER}#g"    /etc/samba/smb.conf
    [ -f /etc/pcmcia/shared ]      && sed -i "s#/home/grml/#/home/${NEWUSER}/#g"  /etc/pcmcia/shared
    [ -f /etc/cups/printers.conf ] && sed -i "s#/home/grml#/home/$NEWUSER#g"      /etc/cups/printers.conf
    find /etc/skel/ -type f -exec sed -i "s#/home/grml/#/home/${NEWUSER}/#g" {} \;
    # fix dirstack
    [ -r /home/grml/.zdirs ] && sed -i "s#/home/grml#/home/$NEWUSER#g" /home/grml/.zdirs 2>/dev/null
    mv /home/grml /home/"$NEWUSER"
  fi

  if [ -z "$GRML2HD_NONINTERACTIVE" ] ; then
    USERPW1='PW1'
    USERPW2='PW2'
    while [ "$USERPW1" != "$USERPW2" ]; do
      USERPW1=$(dialog --insecure --stdout --title "${PN}" --passwordbox "Please enter the password for user $NEWUSER:" 10 60)
      USERPW2=$(dialog --insecure --stdout --title "${PN}" --passwordbox "Please enter the password for user $NEWUSER again for confirmation:" 10 60)

      if [ "$USERPW1" != "$USERPW2" ]; then
        $(dialog --stdout --title "${PN}" --ok-label \
        "Retry" --msgbox "Passwords do not match!" 10 60)
      fi
    done
    if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
      echo "$NEWUSER:$USERPW1" | chpasswd -m
    else
      echo "$NEWUSER:$USERPW1" | chpasswd
    fi
    export USERPW1=''
    export USERPW2=''
  else
    if chpasswd --help 2>&1 | grep -q -- '-m,' ; then
      echo "$NEWUSER:$USERPASSWD" | chpasswd -m
    else
      echo "$NEWUSER:$USERPASSWD" | chpasswd
    fi
    export USERPASSWD=''
  fi
}

create_sudoers() {
  cat > /etc/sudoers << EOF
# sudoers file.
# This file MUST be edited with the "visudo" command as root.
# See the man page for details on how to write a sudoers file.

# Host alias specification

# User alias specification

# Cmnd alias specification

Cmnd_Alias GRML = /usr/sbin/grml-nessus, /usr/sbin/grml-network, \\
/usr/sbin/grml-pptp-inode, /usr/sbin/grml-pptp-vcgraz, \\
/usr/sbin/grml-pptp-xdsl-students, /usr/sbin/grml-vpnc-tugraz, \\
/usr/bin/grml-setlang, /usr/sbin/hwinfo

Cmnd_Alias DEBIAN = /usr/bin/apt-get, /usr/bin/dpkg, /usr/bin/auto-apt, /usr/bin/apt-file, /usr/bin/aptitude, /usr/sbin/dpkg_running

# User privilege specification
root ALL=(ALL) ALL

# $NEWUSER WARNING: This allows the unprivileged $NEWUSER user to start commands as root
# $NEWUSER WARNING: This is totally insecure and (almost) makes $NEWUSER a second root account.
# $NEWUSER WARNING: Never allow external access to the $NEWUSER user!!!
# $NEWUSER ALL=NOPASSWD: ALL

# Only allow a few grml scripts
$NEWUSER ALL=NOPASSWD: GRML
$NEWUSER ALL=NOPASSWD: DEBIAN

# usage examples:
# $NEWUSER ALL=NOPASSWD: /sbin/hdparm
# $NEWUSER ALL=NOPASSWD: /usr/sbin/hwinfo
# Cmnd_Alias DEBIAN = /usr/bin/apt-get, /usr/bin/dpkg, /usr/bin/auto-apt, /usr/bin/apt-file
# $NEWUSER ALL=NOPASSWD: DEBIAN
# chroot       ALL=NOPASSWD: /usr/sbin/chroot, /bin/su - chroot
# chroot       ALL=NOPASSWD: /usr/sbin/chroot, /bin/su - chroot

secvpn  ALL=NOPASSWD: /usr/sbin/secvpn, /usr/sbin/pppd

EOF
}

install_lilo() {
  lilo > /dev/null 2>/tmp/lilo.error
  if [ "$?" -eq 0 ]; then
    is_grml2hd_noninteractive || dialog --stdout --title "${PN}" --msgbox "Lilo exited successfully" 5 60
  else
    dialog --stdout --title "${PN}" --msgbox "Installation of Lilo failed! Please fix manually.

Error:

$(cat /tmp/lilo.error)

" 0 0
  fi
}

get_rootpartition() {
  if [ -n "$PARTITION" ] ; then
     # do not use root=UUID=... when using SW-RAID or LVM to avoid the:
     # '/dev/disk/by-uuid/<uuid> does not exist. Dropping to a shell!'-problem
     if [ -n "$SWRAID" -o -n "$LVM" ] ; then
        ROOTPARTITION="$PARTITION"
     else
        PARTUUID="$(blkid -s UUID -o value $PARTITION)"
        ROOTPARTITION="UUID=$PARTUUID"
     fi
  else
     echo "\$PARTITION not set, error.">&2
     return 1;
  fi
}

# get grub's syntax for /dev/ice
# usage example: 'grubdevice /dev/sda2' returns '(hd0,1)'
grubdevice() {
  if [ -z "$1" ] ; then
     echo "Usage: grubdevice <device>">&2
     return 1
  fi

  device="$1"
  device_map=/boot/grub/device.map

  # create device.map
  if ! [ -f "$device_map" ] ; then
    # grub2:
    if which grub-mkdevicemap >/dev/null 2>&1 ; then
      grub-mkdevicemap --device-map="$device_map" >/dev/null 2>/tmp/grub.device-map
    else
      echo 'quit' | grub --device-map="$device_map" >/dev/null 2>/tmp/grub.device-map
    fi
  fi

  # taken from d-i's trunk/packages/arch/i386/grub-installer/grub-installer:
  tmp_disk=`echo "$device" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \
                    -e 's%\(fd[0-9]*\)$%\1%' \
                    -e 's%/part[0-9]*$%/disc%' \
                    -e 's%\(c[0-7]d[0-9]*\).*$%\1%'`
  tmp_part=`echo "$device" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' \
                    -e 's%.*/fd[0-9]*$%%' \
                    -e 's%.*/floppy/[0-9]*$%%' \
                    -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
                    -e 's%.*c[0-7]d[0-9]*p*%%'`
  tmp_drive=$(grep -v '^#' $device_map | grep "$tmp_disk *$" | sed 's%.*\(([hf]d[0-9][a-g0-9,]*)\).*%\1%')
  case $1 in
     /dev/[sh]d[a-z]) # we expect something like '(hd0)'
        GRUBDEVICE="$(echo "$tmp_drive")"
        GRUBSETUPDEVICE="$(echo "$tmp_drive")"
        ;;
      *) # we expect something like '(hd0,0)'
        GRUBDEVICE="$(echo "$tmp_drive" | sed "s%)$%,`expr $tmp_part - 1`)%")"
        # make sure we don't install into MBR if $USE_MBR is not set
        if [ -z "$USE_MBR" ] ; then
           GRUBSETUPDEVICE="$(echo "$tmp_drive" | sed "s%)$%,`expr $tmp_part - 1`)%")"
        else
           GRUBSETUPDEVICE="$(echo "$tmp_drive" | sed "s%,[0-9]%%")"
        fi
        ;;
  esac
}

install_grub_legacy()
{
  get_rootpartition >/dev/null 2>/tmp/grub.rootpartition

  if ! [ -r /boot/grub/grml-template_menu.lst ] ; then
     echo "Error: could not find /boot/grub/grml-template_menu.lst">&2
     return 1
  else
     if [ -z "$GRUBDEVICE" -o -z "$GRUBSETUPDEVICE" ] ; then
        echo "Error: could not find a grub device">&2
        return 1
     else
        # avoid cp :`/boot/grub/grml-... menu.lst' are the same file
        [ -L /boot/grub/menu.lst ] && rm -f /boot/grub/menu.lst
        cp /boot/grub/grml-template_menu.lst /boot/grub/menu.lst

        sed -i "s|^# kopt=root=.*|# kopt=root=${ROOTPARTITION} ro ${BOOTOPTIONS_APPEND} ${BOOT_APPEND}|" /boot/grub/menu.lst
        sed -i "s|^# groot=.*|# groot=${GRUBDEVICE}|" /boot/grub/menu.lst

        # if we have the symlink we get entries without initrd option in menu.lst
        # by update-grub, see http://bts.grml.org/grml/issue135
        [ -L /boot/vmlinuz ] && rm -f /boot/vmlinuz

        # make sure we have everything we need for grub-installation
        if ! [ -r /boot/grub/stage1 ] ; then
           if [ $(dpkg --print-architecture) = amd64 ] ; then
              ln -sf /usr/lib/grub/x86_64-pc/stage1 /boot/grub/stage1
              ln -sf /usr/lib/grub/x86_64-pc/stage2 /boot/grub/stage2
           fi
           if [ $(dpkg --print-architecture) = i386 ] ; then
              ln -sf /usr/lib/grub/i386-pc/stage1 /boot/grub/stage1
              ln -sf /usr/lib/grub/i386-pc/stage2 /boot/grub/stage2
           fi
        fi

        update-grub -y >/dev/null 2>/tmp/update-grub.error

        echo -en "debug: root ${GRUBDEVICE}\nsetup ${GRUBSETUPDEVICE}\n" > /tmp/grub.debug
        echo -en "root ${GRUBDEVICE}\nsetup ${GRUBSETUPDEVICE}\n" | grub install

     fi
  fi
}

install_grub2()
{
  # append bootoptions:
  if ! [ -r /etc/default/grub ] ; then
    echo "Error: /etc/default/grub not present.">&2
  else
    if [ -n "$BOOTOPTIONS_APPEND" ] || [ -n "$BOOT_APPEND" ] ; then
      # string already present?
      if grep -q '^GRUB_CMDLINE_LINUX=' /etc/default/grub ; then
        sed -i "s|^GRUB_CMDLINE_LINUX=\"\(.*\)\"|GRUB_CMDLINE_LINUX=\"\1 ${BOOTOPTIONS_APPEND} ${BOOT_APPEND}\"|" /etc/default/grub
      else # append:
        echo "# appended by grml2hd:" >> /etc/default/grub
        echo "GRUB_CMDLINE_LINUX=\"${BOOTOPTIONS_APPEND} ${BOOT_APPEND}\"" >> /etc/default/grub
      fi
    fi
  fi

  grub-mkdevicemap >/dev/null 2>/tmp/grub.device-map.error

  # if installing to MBR we can call grub-install with defaults
  if [ -n "$USE_MBR" ] ; then
    grub-install         "$BOOT_PARTITION" >/dev/null 2>/tmp/grub.error
  else
    # but when not installing to MBR we have to force grub-install
    # because of the blocklists warning
    grub-install --force "$BOOT_PARTITION" >/dev/null 2>/tmp/grub.error
  fi

  update-grub >/dev/null 2>/tmp/update-grub.error

}

install_grub() {
  if [ -z "$PARTITION" ] ; then
     echo "\$PARTITION not set, can not install grub.">&2
     exit 1
  fi

  # make sure we don't have a /etc/lilo.conf present if using grub
  # as several Debian packages might think we use lilo
  [ -r /etc/lilo.conf ] && mv -f /etc/lilo.conf /etc/lilo.conf.unused

  # /dev/mapper/* isn't available in /boot/grub/device.map:
  if [ -n "$LVM" ] ; then
     GRUBDEVICE="$BOOT_PARTITION"
     GRUBSETUPDEVICE="$BOOT_PARTITION"
  else
    # TODO - shouldn't be relevant for grub2 anymore
     grubdevice "$PARTITION" >/dev/null 2>/tmp/grubdevice
  fi

  # grub2:
  if which grub-mkdevicemap >/dev/null 2>&1 ; then
    install_grub2 >/tmp/grub.install 2>/tmp/grub.error
  else # grub1:
    install_grub_legacy >/tmp/grub.install 2>/tmp/grub.error
  fi

  if [ "$?" -eq 0 ]; then
     is_grml2hd_noninteractive || \
     dialog --stdout --title "${PN}" --msgbox "Installation of Grub exited successfully." 5 60
  else
     dialog --stdout --title "${PN}" --msgbox "Installation of Grub failed! Please fix manually.

Error:

$(cat /tmp/grub.error)

" 0 0
  fi
}

bootmanager_selection() {
  # in non-interactive mode we use grub by default, unless $BOOT_MANAGER is set to lilo;

  if ! is_grml2hd_noninteractive ; then
     BOOT_MANAGER=$(dialog --stdout --title "grml2hd" --default-item grub \
                          --radiolist "Which bootmanager do you want to install?" 0 0 0 \
                          grub  "install grub on partition $BOOT_PARTITION" on \
                          lilo  "install lilo on partition $BOOT_PARTITION" off \
                          none  "do not install any boot manager" off)
     [ $? -eq 0 ] || exit 20
  fi

  if [ -x /sbin/dmraid ] ; then
     if dmraid -r "$BOOT_PARTITION" | grep -q ok ; then
        dialog --title "${PN}" --msgbox "Warning: $BOOT_PARTITION seems to have a dmraid signature.

There might be problems with booting your systems due to the
presense of the dmraid signature. Please fix it manually
removing the dmraid signature running:

  dmraid -r $BOOT_PARTITION -E

and reinstalling the bootmanager then.

Usage example if your system is installed on /dev/sda3 and
you want grub on /dev/sda:

  mount /mnt/sda3
  grub-install --recheck --root-directory /mnt/sda3 /dev/sda

"
     fi
  fi

}

adjust_timezone() {
  # /proc/cmdline might not be available inside chroot?
  if grep -q -e utc -e gmt /proc/cmdline 2>/dev/null ; then
     sed -i 's/^UTC=.*/UTC=yes/' /etc/default/rcS
     if [ "$?" -eq 0 ]; then
        is_grml2hd_noninteractive ||  dialog --stdout --title "${PN}" --msgbox "Adjusted timezone for UTC as requested via bootoption." 5 60
     fi
  fi

  if checkbootparam tz ; then
     KTZ="$(getbootparam tz 2>>$DEBUG)"
     [ -f "/usr/share/zoneinfo/$KTZ" ] && TZ="$KTZ"
     rm -f /etc/localtime
     cp "/usr/share/zoneinfo/$TZ" /etc/localtime
     if [ "$?" -eq 0 ]; then
        is_grml2hd_noninteractive || dialog --stdout --title "${PN}" --msgbox "Adjusted timezone for $KTZ as requested via bootoption." 5 60
     else
        is_grml2hd_noninteractive || dialog --stdout --title "${PN}" --msgbox "Specified timezone [${KTZ}] could not be found, sorry." 5 60
     fi
  fi
}

adjust_services() {
  if [ -x /usr/sbin/grml-setservices ] ; then
     is_grml2hd_noninteractive || /usr/sbin/grml-setservices
  fi
}

remove_unwanted_packages() {
  dpkg --purge grml-firefox >/dev/null 2>&1
  # dpkg --purge live-initramfs >/dev/null 2>&1
  rm -rf /etc/skel/.mozilla/firefox >/dev/null 2>&1
}

adjust_filerc() {
  if ! dpkg --list file-rc 2>/dev/null | grep -q '^ii' ; then
    echo "Looks like file-rc isn't installed, skipping adjust_filerc therefore."
    return 0
  fi

  # make sure /etc/runlevel.conf looks like needed for a Debian harddisk system
  # based on /var/lib/dpkg/info/initscripts.postinst
  update-rc.d bootlogd               start 05 S . >/dev/null || true
  update-rc.d checkroot.sh           start 10 S . >/dev/null || true
  update-rc.d mtab.sh                start 12 S . >/dev/null || true
  update-rc.d checkfs.sh             start 30 S . >/dev/null || true
  update-rc.d mountall.sh            start 35 S . >/dev/null || true
  update-rc.d mountall-bootclean.sh  start 36 S . >/dev/null || true
  update-rc.d mountoverflowtmp       start 37 S . >/dev/null || true
  update-rc.d networking             start 40 S . start 35 0 6 . >/dev/null || true
  update-rc.d mountnfs.sh            start 45 S . >/dev/null || true
  update-rc.d mountnfs-bootclean.sh  start 46 S . >/dev/null || true
  update-rc.d urandom                start 55 S . start 30 0 6 . >/dev/null || true
  update-rc.d umountroot             start 60 0 6 . >/dev/null || true
  update-rc.d umountfs               start 40 0 6 . >/dev/null || true
  update-rc.d umountnfs.sh           start 31 0 6 . >/dev/null || true
  update-rc.d sendsigs               start 20 0 6 . >/dev/null || true
  update-rc.d killprocs              start 30 1 . >/dev/null || true
  update-rc.d bootlogs               start 70 1 2 3 4 5 . >/dev/null || true
  update-rc.d rc.local               start 99 2 3 4 5 . >/dev/null || true
  update-rc.d stop-bootlogd-single   start 99 S . >/dev/null || true
  update-rc.d stop-bootlogd          start 99 2 3 4 5 . >/dev/null || true
}

create_initrd() {
  INITRD=/boot/initrd.img-$(uname -r)

  ( echo "0" ;
    echo "42"
    echo "XXX";
    echo "Building initrd/initramfs..."
    echo "XXX";
    update-initramfs -c -t -k $(uname -r) >/tmp/initramfs.error 2>&1
    echo "100") | \
  dialog --title "${PN}" --gauge "Building initrd/initramfs..." 9 50 0

  if [ "$?" -eq 0 ]; then
    sed -i "s#.*initrd=ADJUSTME#        initrd=$INITRD#g" /etc/lilo.conf
  else
    dialog --msgbox "There was an error running initramfs for creating an initrd:
$(cat /tmp/initramfs.error)

Sorry. Continuing anyway, you might need to rerun update-initramfs on your own." 0 0
  fi
}

finalize() {
  umount /proc
  umount /sys
  sync
}

# now run the functions:
checkvalue $CHROOT_SETHOSTNAME    && sethostname
checkvalue $CHROOT_SETLANGUAGE    && setlanguage
checkvalue $CHROOT_ADJUSTTIMEZONE && adjust_timezone
checkvalue $CHROOT_XCONFIG        && xconfig
checkvalue $CHROOT_ROOT_ACCOUNT   && root_account
checkvalue $CHROOT_NEWUSER        && newuser

  ( echo "0" ;
    checkvalue $CHROOT_CREATE_SUDOERS && create_sudoers
    echo "42"
    if checkvalue $CHROOT_REMOVE_UNWANTED_PACKAGES ; then
      echo "XXX";
      echo "Removing unwanted packages which are not necessary on harddisk installation.";
      echo "XXX";
      remove_unwanted_packages
    fi
    echo "100") | \
  dialog --title "${PN}" --gauge "Running some fixes for the harddisk installation." 9 50 0
  is_grml2hd_noninteractive || dialog --msgbox "Finished running fixes." 5 50

if checkvalue $CHROOT_GRML_AUTOCONFIG ; then
   is_grml2hd_noninteractive || grml-autoconfig        # script in /usr/sbin/
fi

checkvalue $CHROOT_ADJUSTSERVICES && adjust_services

if checkvalue $CHROOT_REMOVE_PACKAGES_SERVER ; then
   if [ -z "$NOSWREMOVAL" ] ; then
      if ! is_grml2hd_noninteractive ; then
         [ -r /usr/sbin/remove-packages-server ] && /usr/sbin/remove-packages-server
      fi
   fi
fi

checkvalue $CHROOT_ADJUST_FILERC && adjust_filerc

is_grml2hd_noninteractive && POSTEXECUTE # execute defined function in /etc/grml2hd/config

checkvalue $CHROOT_CREATE_INITRD && create_initrd

# use status bar for lilo/grub to provide feedback what's running...
if checkvalue $CHROOT_INSTALL_BOOTMANAGER ; then

  bootmanager_selection

  ( echo "0" ;
    echo "42"
    echo "XXX";
    echo "Installing bootmanager..."
    echo "XXX";
    case "$BOOT_MANAGER" in
      grub)
        install_grub
        ;;
      lilo)
        install_lilo
        ;;
      *)
	;;
    esac
    echo "100") | \
  dialog --title "${PN}" --gauge "Installing bootmanager..." 9 50 0
fi

checkvalue $CHROOT_FINALIZE && finalize

## END OF FILE #################################################################
