#! /bin/sh
#----------------------------------------------------------------------------
# /etc/init.d/boot - initial boot script
#
# Copyright (c) 2005 Frank Meyer <frank@eisfair.org>
#
# Creation   : 08.08.2001 fm
# Last Update: $Id$
#
# 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 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

# don't include eislib, maybe /var is on an other partition and not mounted yet
/usr/local/bin/colecho "Running $0" gn

# set path
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin; export PATH

# include configuration
. /etc/config.d/base

#----------------------------------------------------------------------------
# mount devices, e.g. /proc
#----------------------------------------------------------------------------
cd /
swapon -a
/bin/mount -n -o remount,ro / 2>/dev/null

if [ $? -eq 0 ]
then
    echo "Checking file systems..."
    /sbin/fsck -A -p 2>/dev/null

    if [ $? -gt 1 ]
    then
	echo
	echo "fsck failed.  Please repair manually and reboot. The root"
	echo "file system is currently mounted read-only. To remount it"
	echo "read-write do:"
	echo
	echo "   bash# mount -n -o remount,rw /"
	echo
	echo "Attention: Only CONTROL-D will reboot the system in this"
	echo "maintanance mode. shutdown or reboot will not work."
	echo
	PS1="(repair filesystem) # "
	export PS1
	/bin/sh </dev/console >/dev/console 2>&1

	# if the user has mounted something rw, this should be umounted
	echo "Unmounting file systems (ignore error messages)"
	/bin/umount -avn

	# on umsdos fs this would lead to an error message. so direct 
	# errors to /dev/null
	/bin/mount -no remount,ro / 2> /dev/null
	/bin/sync
	/sbin/reboot -f
    fi
fi

for j in /var/boot-ext/*
do
  if [ -f $j ]
  then
      /bin/sh $j
  fi
done

/bin/sync
/bin/umount /initrd 2>/dev/null
/bin/mount /proc 2>/dev/null			# mount /proc
/bin/mount -n -o remount,rw /                   # remount / rw without writing mtab
rm -f /etc/mtab /etc/mtab~			# remove old mtabs
rm -f /var/boot-ext/*
/bin/mount -av 2>/dev/null			# mount remaining fs from fstab

# remount all ext2/3 filesystems with acl,user_xattr, if kernel supports acl (excluding /boot)
for pseudofile in /proc/{ksyms,kallsyms}
do
  if [ -e "$pseudofile" ]
  then
      if grep -q 'posix_acl_' "$pseudofile"
      then
          /bin/mount -t ext2,ext3 |
          while read dev on point type typ options
          do
            if [ "$point" != "/boot" ]
            then
                /bin/mount -o remount,acl,user_xattr $point
            fi
          done
      fi
  fi
done

chmod 644 /etc/mtab				# set permission

if [ -d /etc/init.d/boot.d ]
then
    for j in /etc/init.d/boot.d/S*              # start cleanup scripts
    do
      if [ -x $j ]
      then
          $j start
      fi
    done
fi

# set hostname
/bin/hostname $HOSTNAME

# apply environment
/bin/sh /var/install/config.d/environment.sh

case "$TIME_ZONE"
in
  met|cet|CET) TIME_ZONE='CET';;		# compatibility
  gmt|GMT) TIME_ZONE='GMT';;
esac

/usr/sbin/zic -l $TIME_ZONE			# set TIME_ZONE