#!/bin/bash
# $Id: rcS_fai,v 1.89 2002/04/15 16:15:16 lange Exp $
#*********************************************************************
#
# rcS_fai -- main installation script executed after booting
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 1999-2002 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

# some variables
timeout=30 # bootpc timeout
FAI_VERSION=FAIVERSIONSTRING
stamp=/tmp/FAI_INSTALLATION_IN_PROGRESS
romountopt="-o ro,rsize=8192"
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"
unset oclass

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
fai_init() {

    # everything that must be done, before the ramdisk is available
    local sub
    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

    # read subroutine definitions
    sub=$FAI_SHAREDIR/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
    divertlist=$LOGDIR/divert.lis
    # rcsfaivar saves variables which are sourced by remote logins during isntallation
    rcsfaivar=/tmp/rcsfai.var

    # variables for cfengine
    chroot=/usr/sbin/chroot
    cf_prefix="cfengine:"
    files=$FAI/files
    target=$FAI_ROOT

    umask 022
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:/usr/local/bin:
    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 -n -t devpts devpts /dev/pts
	mount -n -o remount,rw,rsize=8192 /dev/root /
	cat /proc/kmsg >/dev/tty4 &
    fi
    if [ X$OS_TYPE = Xsunos ]; then
	osname='Sun Solaris'
    fi

    {
    cat <<-EOF

    -----------------------------------------------------
      $FAI_VERSION
      Fully Automatic Installation for $osname

      Copyright (c) 1999-2002, Thomas Lange
              lange@informatik.uni-koeln.de
    -----------------------------------------------------

EOF

    save_dmesg
    } > >( tee -a $rcslog )  2>&1
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
create_ramdisk() {

    # create a writeable area on the install client
    mount -n -t proc proc /proc
    # if we have shm use it as ramdisk
    mount -t shm shm /tmp || {
	local ramdevice=/dev/ram0
	mke2fs -q -m 0 $ramdevice && echo "ramdisk $ramdevice created"
	mount -n $ramdevice /tmp
    }
    # now create the required subdirectories
    mkdir /tmp/var /tmp/etc /tmp/target
    cd    /tmp/var && mkdir tmp log lock run spool
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Main routine
# callarg=$1

# Solaris has already a writable /tmp directory
[ `uname -s` = Linux ] && create_ramdisk

fai_init
{
task confdir
task setup
task defclass
task defvar
} > >( tee -a $rcslog )  2>&1

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

(task action > >( tee -a $rcslog ) 2>&1)
