#!/bin/sh
#
# "SystemImager" 
#
#  Copyright (C) 1999-2001 Brian Elliott Finley 
#                          <brian@bgsw.net>
#  Copyright (C) 2002-2003 Bald Guy Software 
#                          <brian@bgsw.net>
#
#  $Id: rcS,v 1.57 2003/11/17 19:35:35 brianfinley Exp $
#
#  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  #XXX comment out before each release. -BEF-

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

#################################################################################
#
#   main
#
get_arch

switch_root_to_tmpfs

mount_dev_on_devfs

mount_proc

adjust_arch

ifconfig_loopback

load_my_modules

read_local_cfg

start_network
beep

ping_test

start_syslogd

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

tmpfs_watcher

get_boel_binaries_tarball
beep

if [ ! -z $FLAMETHROWER_DIRECTORY_PORTBASE ]; then
    # Hosts file is included here
    get_scripts_directory
fi

autodetect_hardware_and_load_modules

if [ ! -z $SSH_DOWNLOAD_URL ]; then
    echo
    echo start_ssh
    start_ssh
fi

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

if [ -z $HOSTNAME ]; then
    echo
    echo get_hostname_by_dns
    get_hostname_by_dns
fi

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

# If neither HOSTNAME or IMAGENAME is set, then we cannot proceed.  
# (IMAGENAME may have been set by local.cfg).  -BEF-
if [ -z $IMAGENAME ] && [ -z $HOSTNAME ]; then
    echo
    echo "HOSTNAME for $IPADDR was not specified in a local.cfg file, and I could"
    echo "not find it via the hosts file or DNS.  Additionally, IMAGENAME was not"
    echo "specified.  I must have one or the other in order to proceed!!!"
    echo
    shellout
fi
    
if [ ! -z $IMAGENAME ]; then
    # If IMAGENAME is specified, then the IMAGENAME.master script takes
    # precedence over the HOSTNAME.sh script. -BEF-
    echo
    echo "This host will be installed with image: ${IMAGENAME}"
    SCRIPTNAME="${IMAGENAME}.master"
    get_script || shellout

else 

    # Try to get an autoinstall script based on $HOSTNAME.
    SCRIPTNAME="${HOSTNAME}.sh"
    get_script

    if [ $? != 0 ]; then

        echo "$CMD failed!"

        # Try to get a master file based on the "base hostname".  For example,
        # if the hostname is compute99, then try to get compute.master. -BEF-
        #
        BASE_HOSTNAME=`echo $HOSTNAME | sed "s/[0-9]*$//"` 

        SCRIPTNAME="${BASE_HOSTNAME}.master"
        get_script || shellout
    fi
fi

write_variables

beep
run_autoinstall_script
#
#################################################################################

exit 0


