#!/bin/sh
#
#   "SystemImager" 
#
#   Copyright (C) 1999-2010 Brian Elliott Finley 
#
#   $Id: rcS 4521 2011-03-14 02:42:20Z finley $
#
#   Others who have contributed to this code:
#       Charles C. Bennett, Jr. <ccb@acm.org>
#       Sean Dague <japh@us.ibm.com>
#       Dann Frazier <dannf@dannf.org>
#       Curtis Zinzilieta <czinzilieta@valinux.com>
#

#set -x         # can be uncommented for debug purposes

#################################################################################
#   Load functions and other variables
#
. /etc/init.d/functions
#
################################################################################

#################################################################################
#
#   main
#
{

mount_initial_filesystems

monitor_save_dmesg

get_arch

adjust_arch

ifconfig_loopback

load_my_modules

start_udevd

# Need to do this prior to read_local_cfg to get last_root parameter, 
# if specified. -BEF-
variableize_kernel_append_parameters

read_kernel_append_parameters

read_local_cfg

# Re-read kernel append parameters -- we want them to override what's in 
# the local.cfg files. -BEF-
read_kernel_append_parameters

start_network
beep

ping_test

if [ ! -z $MONITOR_SERVER ]; then
    init_monitor_server
fi

start_syslogd

if [ ! -z $FLAMETHROWER_DIRECTORY_PORTBASE ]; then
    get_flamethrower_directory
fi

get_torrents_directory

beep

if [ "$TMPFS_STAGING" = "yes" ]; then
    tmpfs_watcher
fi

if [ "x$SSHD" = "xy" ]; then
    logmsg
    logmsg start_sshd
    start_sshd
fi

if [ ! -z $SSH_DOWNLOAD_URL ]; then
    SSH=y
fi
if [ "x$SSH" = "xy" ]; then
    logmsg
    logmsg start_ssh
    start_ssh
fi

get_scripts_directory

show_loaded_modules

# HOSTNAME may already be set via local.cfg -BEF-
if [ -z $HOSTNAME ]; then
    get_hostname_by_hosts_file
fi

if [ -z $HOSTNAME ]; then
    get_hostname_by_dns
fi

if [ ! -z $HOSTNAME ]; then
    logmsg
    logmsg "This hosts name is: $HOSTNAME"
fi

run_pre_install_scripts

# If none of SCRIPTNAME, HOSTNAME, or IMAGENAME is set, then we cannot proceed.
# (IMAGENAME may have been set by local.cfg).  -BEF-
if [ -z $SCRIPTNAME ] && [ -z $IMAGENAME ] && [ -z $HOSTNAME ]; then
    logmsg
    logmsg "FATAL:  None of SCRIPTNAME, IMAGENAME, or HOSTNAME were set, and I"
    logmsg "can no proceed!  (Scottish accent -- think Groundskeeper Willie)"
    logmsg
    logmsg "HOSTNAME is what most people use, and I try to determine it automatically"
    logmsg "from this hosts IP address ($IPADDR) in the following order:"
    logmsg
    logmsg "  1)  /var/lib/systemimager/scripts/hosts on your imageserver"
    logmsg "  2)  DNS"
    logmsg
    logmsg "Finally, you can explicitly set many variables pre-boot, including SCRIPTNAME,"
    logmsg "IMAGENAME, and HOSTNAME, as kernel append parameters or with a local.cfg file."
    logmsg
    shellout
fi
    
choose_autoinstall_script

write_variables
beep

run_autoinstall_script

} 2>&1 | send_monitor_stdout

#
#################################################################################

exit 0

