#!/bin/bash
#
# prepareroot
#
# --------------------------------------------------------------------------
# Copyright notice
# --------------------------------------------------------------------------
# Copyright: Rene Mayrhofer, Mar. 2000
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# On Debian GNU/Linux systems, the complete text of the GNU General
# Public License can be found in `/usr/share/common-licenses/GPL'.
# --------------------------------------------------------------------------
#

# do not edit below this line
# ------------------------------------------------------------

. /etc/default/rcS
. /etc/default/common-definitions.sh
. /etc/default/ide-scsi-probe.sh

umask 077
set -e

old_printk=`cat /proc/sys/kernel/printk`
kernelver=`cat /proc/sys/kernel/osrelease`

echo -n "Unmounting and freeing initrd image ... "
if umount /initrd/dev; then
  if umount /initrd; then
    if /sbin/freeramdisk "${ramdisks}0"; then
      echo "done"
    else
      echo "Could not free initrd image."
    fi
  else
    echo "Could not unmount."
  fi
else
  echo "Could not unmount."
fi

probemod() {
  if insmod_helper $1; then
    echo "Loaded $1 module.";
  else
    local path=`find /lib/modules/$kernelver/ -name "$1.o"`
    # ignore it if the module does not exist
    if [ -n "$path" ] && [ -r $path ]; then
      # if the module is already loaded, ignore the error
      if ! lsmod | grep "$1" > /dev/null; then
        echo "Error: module $1 is available, but could not be loaded !"
      fi
    fi
  fi
}

# try to load the configuration from the given device $1 with filesystem $2
# returns 0 on success, 1 on failure
try_load() {
  echo "0 0 0 0" > /proc/sys/kernel/printk
  if mount -n -o ro -t $2 $1 $save_mntpoint >/dev/null 2>/dev/null; then
    echo "$old_printk" > /proc/sys/kernel/printk
    if [ -r $save_etcimage ]; then
      # get the config
      echo "Found configuration on $1. Loading from it."
      restore-etc $save_etcimage /etc
      # remember the location from where we loaded the configuration
      echo "$1" > /etc/gibraltar_config_source
      # first script hook for config-loaded
      call_script_hook config-loaded $save_mntpoint $save_etcimage /etc
      umount $save_mntpoint
      return 0
    elif [ -r $save_etcimage_old ]; then
      echo "Found configuration on $1 in old format. Loading and converting it to new format."
      # restore the configuration to a temporary location and then convert it
      convert-config $save_etcimage_old /etc
      # first script hook for config-loaded
      call_script_hook config-loaded $save_mntpoint $save_etcimage /etc
      umount $save_mntpoint
      return 0
    fi
    umount $save_mntpoint
  fi
  echo "$old_printk" > /proc/sys/kernel/printk
  return 1
}

# try mounting all currently known harddisk partitions and try to load a
# stored configuration from them
check_hd_partitions() {
  local partitions=`cat /proc/partitions | egrep "/part[0-9]*" | mawk '{print $4;}'`
  for part in $partitions; do
    for fs in $save_filesystems_hdd; do
      if try_load "/dev/$part" $fs; then
        return 0
      fi
    done
  done
  return 1
}

# to mount the image, we may need to load the loop module
#probemod loop
# and possibly the floppy and vfat modules
probemod floppy
probemod fat
for fs in $save_filesystems_floppy; do
  probemod $fs
done
for fs in $save_filesystems_hdd; do
  probemod $fs
done

# first restore the /etc directory
# check if this is an unconfigured (default) boot or if there is already a
# config disk
found_disk=0
echo "Trying to load configuration."
try=0
tried_hdd=0
tried_floppies=0
while true; do
  # Try to load from other floppy drives (that might be installed) as well,
  # but only once. This is for people using one floppy drive for the bootdisk
  # and the second floppy drive for the configuration data.
  if [ $tried_floppies -eq 0 ]; then
    tried_floppies=1
    for drive in ${save_floppies}?; do
      for fs in $save_filesystems_floppy; do
        if try_load $drive $fs; then
          found_disk=1
          break 3
        fi
      done
    done
  else
    for fs in $save_filesystems_floppy; do
      if try_load ${save_floppies}0 $fs; then
        found_disk=1
        break 2
      fi
    done
  fi

  # Also try to load from a harddisk partition, but only once. It is unlikely
  # that the harddisk partitions change in between, so there is no need to
  # check twice....
  if [ $tried_hdd -eq 0 ]; then
    tried_hdd=1
    echo "Searching for configuration on harddisk partitions ... "
    # load IDE and SCSI modules and probe for SCSI adapters (if not done already)
    install_ide_hd_mods
    if check_hd_partitions; then
      found_disk=1
      break
    else
      # only check scsi adapters if this has not been done before (during
      # locating the CD-ROM drive)
      if lsmod | grep "scsi_mod" | grep -v "grep" > /dev/null; then
        scsi_probed=1
      else
        scsi_probed=0
      fi
      install_scsi_hd_mods
      if [ $scsi_probed -eq 0 ]; then
        check_scsi
      fi
      if check_hd_partitions; then
        found_disk=1
        break
      else
        #if [ $scsi_probed -eq 0 ]; then
        #  probe_scsi_mods
        #  if check_hd_partitions; then
        #    found_disk=1
        #    break
       	#  else
        #    echo "No configuration found on the harddisks(s), trying to load from floppy."
        #  fi
        #else
          echo "No configuration found on the harddisks(s), trying to load from floppy."
        #fi
      fi
    fi
  fi

  # break if we already tried it MAX_DISK_TRIES times or when the fastboot
  # option has been used - in this case we do not wait at all for the
  # administrator to insert a disk
  if [ $try -ge $MAX_DISK_TRIES ] || `grep "fastboot" /proc/cmdline >/dev/null`; then
    break;
  fi

  echo "No configuration disk found in drive. If you already have one, please insert it"
  echo "now in your disk drive."
  echo "If you want to pause the boot procedure now, press any key."
  echo ""
  beepconsole -f 500 -l 200 -r 2 -d 200
  if read -n 1 -r -s -t 10; then
    # the user wants to pause
    echo "Boot procedure paused. Press enter to continue."
    echo ""
    read -s
  fi

  try=`expr $try + 1`
done
if [ $found_disk -eq 0 ]; then
  echo "No configuration disk supplied. Loading default values."
  restore-etc $DEFAULT_ETC /etc
fi
# /etc should be there now
beepconsole -f 750 -l 200

# script hook for pre-update
call_script_hook pre-update

# is the version of the currently running system the same as the version that
# the config image has been created with ?
# if not, try to update it
old_version=`cat /etc/gibraltar_version`
new_version=`cat /system/etc-static/gibraltar_version`
if [ "$old_version" != "$new_version" ]; then
  update-config /etc $DEFAULT_ETC /etc/update-${old_version}_to_${new_version}.log || true
fi

# script hook for post-update
call_script_hook post-update /etc/update-${old_version}_to_${new_version}.log

# if the kernel supports devfs, but it is not mounted until now, do it
if grep "devfs" /proc/filesystems >/dev/null; then
  echo -n "Good, the kernel supports devfs ... "
  if ! grep "devfs" /proc/mounts >/dev/null; then
    mount -t devfs none /dev
    echo "mounted."
  else
    echo "already mounted."
  fi
fi

exit 0
