#!/bin/bash
# $Id: rcS_fai,v 1.120 2004/10/23 21:20:01 lange Exp $
#*********************************************************************
#
# rcS_fai -- main installation script executed after booting
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 1999-2004 by Thomas Lange, lange@informatik.uni-koeln.de
# Universitaet zu Koeln
#
#*********************************************************************
# 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.
#
# 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.
# 
# A copy of the GNU General Public License is available as
# `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
# can also obtain it by writing to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#*********************************************************************

#set -xv # for full debugging

export PATH=/usr/local/sbin:/usr/local/bin:/usr/lib/fai:/bin:/sbin:/usr/bin:/usr/sbin:
# some variables
FAI_VERSION=FAIVERSIONSTRING
stamp=/tmp/FAI_INSTALLATION_IN_PROGRESS
romountopt="-o async,noatime,nolock,ro,actimeo=1800"
fstab=fstab  # Solaris uses vfstab
lpipe=/tmp/logfifo

# default classes before calling task defvar. Will be overwritten by task defvar
# the type of operating system (linux, sunos)
oclass=$(uname -s | tr /a-z/ /A-Z/)
classes="DEFAULT $oclass $HOSTNAME LAST"
faimond=0

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
prcopyleft() {

    cat <<-EOF
             -----------------------------------------------------
               Fully Automatic Installation for $osname
               $FAI_VERSION     Copyright (c) 1999-2004

               Thomas Lange      <lange@informatik.uni-koeln.de>
             -----------------------------------------------------
EOF
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
fai_init() {

    local sub osname
    set -a # now export all variables

    # read fai.conf
    # linux dir
    [ -f /etc/fai/fai.conf ] && . /etc/fai/fai.conf
    # solaris dir
    [ -f /tmp/install_config/fai/fai.conf ] && . /tmp/install_config/fai/fai.conf
    # some variables from are not needed any more
    unset NFSROOT FAI_CONFIGDIR installserver

    # read subroutine definitions
    sub=/usr/share/fai/subroutines
    [ -f $sub ] && . $sub
    [ -f $sub-$OS_TYPE ] && . $sub-$OS_TYPE

    DEBIAN_FRONTEND=noninteractive
    # local disks are mounted to $FAI_ROOT
    [ -z "$FAI_ROOT" ] && FAI_ROOT=/tmp/target
    # executed command in the environment of the new system
    ROOTCMD="chroot $FAI_ROOT"
    # no chroot needed
    [ "$FAI_ROOT" = '/' ] && ROOTCMD=
    # directory where temporary log files are stored
    # set default value if nothing is set in fai.conf
    [ -z "$LOGDIR" ] && LOGDIR=/tmp/fai
    mkdir -p $LOGDIR

    # several log files
    diskvar=$LOGDIR/disk_var.sh
    moduleslog=$LOGDIR/modules.log
    rcslog=$LOGDIR/rcS.log

    # variables for cfengine
    files=$FAI/files
    target=$FAI_ROOT

    umask 022
    trap 'echo "Now rebooting";faireboot' INT QUIT

    if [ X$OS_TYPE = Xlinux ]; then
	grep -q '[[:space:]]sysfs' /proc/filesystems && mount -t sysfs sysfs /sys
	osname='Debian GNU/Linux'
	ifup lo
	[ -x /sbin/portmap ] && /sbin/portmap
	mount -t devpts devpts /dev/pts
	# add other options for nfs mount of /dev/root to root-path in dhcpd.conf
	mount -o remount,ro /dev/root /
	cat /proc/kmsg >/dev/tty4 &
    fi
    if [ X$OS_TYPE = Xsunos ]; then
	osname='Sun Solaris'
    fi

    # set red color, but not on sparc
    case $HOSTTYPE in
	sparc*) ;;
	*) test -e /.nocolorlogo || echo -ne "\ec\e[1;31m" ;;
    esac

    prcopyleft
    prcopyleft > $rcslog

    # set scroll region, but not on sparc
    case $HOSTTYPE in
	sparc*) ;;
	*) 
	    test -e /.nocolorlogo || echo -ne "\e[7;0r"
	    test -e /.nocolorlogo || echo -ne "\e[9B\e[1;m"
	    ;;
    esac
    save_dmesg
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
fstart() {

    # these tasks can define variables, that are needed later
    task confdir
    task setup
    task defclass
    task defvar
    load_keymap_consolechars
    set_disk_info
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Main routine
# callarg=$1

# Solaris has already a writable /tmp directory
[ "$oclass" = LINUX ] && create_ramdisk
unset oclass

fai_init

mkfifo $lpipe
tee -a $rcslog < $lpipe &
# in bash &> redirect stdout and stderr to file
fstart &> $lpipe
rm $lpipe
sleep 1 # wait for tee to complete. One second should be ok

# old code
# {
# # a bash group command with { does not work on sparc
# task confdir
# task setup
# task defclass
# task defvar
# load_keymap_consolechars
# set_disk_info
# } > >( tee -a $rcslog )  2>&1

# if you want to recall rcS_fai
# [ -n $callarg ] && FAI_ACTION=$callarg

task action 2>&1 | tee -a $rcslog

# normally this is never reached
echo "End of rcS_fai"
