#!/bin/bash
# $Id: rcS_fai,v 1.112 2004/01/06 14:13:47 lange Exp $
#*********************************************************************
#
# rcS_fai -- main installation script executed after booting
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 1999-2003 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="FAI 2.5.2, 6 jan 2004"
stamp=/tmp/FAI_INSTALLATION_IN_PROGRESS
romountopt="-o async,noatime,nolock,ro,actimeo=1800"
fstab=fstab  # Solaris uses vfstab

# 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

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 here
    unset KERNELPACKAGE NFSROOT NFSROOT_PACKAGES FAI_ROOTPW SSH_IDENTITY
    unset FAI_DEBOOTSTRAP FAI_DEBOOTSTRAP_OPTS FAI_CONFIGDIR FAI_SOURCES_LIST
    unset installserver ftpserver debdist

    # 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
    # rcsfaivar saves variables which are sourced by remote logins during installation
    rcsfaivar=/tmp/rcsfai.var

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

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

    if [ X$OS_TYPE = Xlinux ]; then
	osname='Debian GNU/Linux'
	ifup lo
	[ -x /sbin/portmap ] && /sbin/portmap
	mount -t devpts devpts /dev/pts
	mount  $romountopt -o remount,rsize=8192 /dev/root /
	cat /proc/kmsg >/dev/tty4 &
    fi
    if [ X$OS_TYPE = Xsunos ]; then
	osname='Sun Solaris'
    fi

    echo -ne "\ec\e[1;31m"
    {
    cat <<-EOF
             -----------------------------------------------------
               Fully Automatic Installation for $osname
               $FAI_VERSION

               Copyright (c) 1999-2004 Thomas Lange
               <lange@informatik.uni-koeln.de>
             -----------------------------------------------------
EOF
    save_dmesg
    } > >( tee -a $rcslog )  2>&1
    echo -ne "\e[8;25r"
    echo -ne "\e[9B\e[1;m"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Main routine
# callarg=$1

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

fai_init
{
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 > >( tee -a $rcslog ) 2>&1)
