#! /bin/sh
#
#********* Copyright (c) 2002-2004 Trimble Navigation Limited **********
# $Id: fireball,v 1.52 2006/04/28 21:32:48 tom Exp $
# $Source: /home/CVS/fireballTop/fireballFS/root/etc/init.d/fireball,v $
#***********************************************************************
#
# Perform operations needed at the very start of the boot up process.
# This is probably the first script run in runlevel 3 after the file
# systems have been mounted.
# - Ensure presence of Fireball devices
# - Erase system lock files when the system starts up.
# - Load in kernel modules.
# - Set hostname if the user has specified a system name.
# - Deal with file system customizations needed due to version changes.

PATH=/sbin:/bin:/usr/sbin:/usr/bin

########################################################################
# In the rare case where a firmware installation failed to complete,
# but managed to reboot to here, with missing files, etc., we would like
# to restart the installation.  This could have been caused by a compact
# flash freezeup during the RPM unpacking.  If this occurs, the
# symlink /var/gps/incoming/TheRPMtoInstall will exist.  (It would
# normally be deleted as the last step in the install process.)
# If this is found, then as quick as we can, we trigger a re-install.
# There may be a similar test in the ramdiskKernel/model/sbin/linuxrc
# script that triggers reinstall even earlier in the boot process.
# This version covers the case where the other change hasn't yet been
# installed into the onboard-flash ramdiskKernel Image.
########################################################################
if [[ -e /var/gps/incoming/TheRPMtoInstall ]]
then
  # Signal the ramdiskKernel /sbin/linuxrc script that an installation
  # is requested.
  boot_status 0 255 f

  echo "Incomplete firmware installation detected.  Trying again."
  init 6
fi



# Clean out any runtime files from previous uptimes.
rm -f /etc/network/ifstate  # ??Unclear usage?? 
rm -f /var/run/*.pid        # Runtime Process ID indicators
rm -f /var/run/ftp.pids-*   # Leftovers from wu-ftpd (in.ftpd)
rm -f /var/log/ssl_mutex.*  # Leftovers from https/ssl
rm -f /var/gps/CGItemp*     # Leftovers from http file uploads

# Remove any old lock files, regardless of depth in /var/lock
find /var/lock -type f -exec rm -f {} \;

# Lockfile (pid file) for dhcpc.  This gets left
# behind if using a dhcp server, and the system crashes.  Would prevent
# being able to start dhcp system after the crash.
# The file in /etc/dhcpc is the default location.  We are using the
# alternate location in /etc/sysconfig/dhcpc
rm -f /etc/sysconfig/dhcpc/*.pid



if [ ! -f "/var/run/utmp" ];
then
    echo "Creating utmp"
    touch /var/run/utmp
fi

if [ ! -f "/var/log/wtmp" ];
then
    echo "Creating wtmp"
    touch /var/log/wtmp
fi

if [ ! -e /dev/rtai_shm ];
then
#      echo "NFS: Creating device nodes"
      /bin/mknod /dev/rtai_shm c 10 254
fi

if [ ! -e /dev/console ];
then
#  echo "Creating device console"
  mknod /dev/console c 5 1
fi

if [ ! -e /dev/rtc ]
then
#    echo  "Creating rtc link"
    ln -s /dev/misc/rtc /dev/rtc
fi

# /var/spool is used for some runtime internal socket files.
# Not present in compact flash prior to version 1.1.  Will be created if
# a 1.1x or later is installed.  But our development systems won't have
# it if transitioning from 1.0 to 1.1.  So we make it if necessary.
if [ ! -d /var/spool ]
then
    echo "Creating /var/spool"
    mkdir /var/spool
fi


MODULES="ppp_deflate maxwell_serial rtai_hal rtai_up rtai_shm rtai_fifos rtai_sem kernel_stinger sctp gps_data"
sync
for module in $MODULES
do
  echo
  echo "Loading module $module"  
  modprobe $module
  echo "Done Loading module $module"
done
echo

#echo "Setting permissions on misc devices"
chown 0:5000 /dev/misc/gps_data
chown 0:5000 /dev/misc/power
chown 0:5000 /dev/misc/led

# Set the system hostname if a name has been defined in the
# configuration file sysname.
# This has to be done relatively early in the boot process so that
# utilities (like syslogd) have a name to work with.  In the rare case
# where there is no file, then hostname will be blank (or what dhcp
# supplies).  This should only occur until networking is first started,
# since /etc/init.d/network will create a value in a default file based
# on serial number (which can't be determined until networking is up
# due to other problems).
if [[ -f /etc/sysconfig/sysname ]]
then
  NAME=$(</etc/sysconfig/sysname)
  hostname $NAME
  echo "System name is $NAME"
fi

########################################################################
# Handle some version compatibility problems. 
# Occasionally we make changes in the /etc/sysconfig area which would
# be implemented by a reset-to-defaults operation.  But that can't be
# forced on the users at every firmware upgrade.  This section is 
# to be used to handle those cases.  
#
# Although we could copy these files at every power up, effort should be
# made to only do the updates when needed.  See the existing samples
# below for ideas on how to do that.
#
# An alternate approach is to embed these instructions into the
# installation scripts, but that has other problems, primarily when
# trying to create patch-upgrades.  This approach is safe and the
# only penalties are a tiny bit of shell-code and a bit of wasted time
# at bootup.
########################################################################

# Early versions of code did not have the directory:
#    /usr/local/fireball/defaults/sysconfig/configFiles/
# and thus did not create the directory /etc/sysconfig/configFiles when
# reset-to-defaults.  This led to problems as in SPR 314.  Now, if the
# directory doesn't exist, we make it at power up time.
f=/etc/sysconfig/configFiles/
if [[ ! -d $f ]]
then
  echo "Creating $f"
  mkdir $f
  chmod 755 $f
fi

# The file at /etc/sysconfig/ppp/options is fixed data (part of the
# release) and not a user-alterable config file.  It would only be
# installed by reset-to-defaults.  Yet it has changed twice and needed
# updating each time.  Formerly this was done at firmware installation
# time as part of the post-install scripts.  Now it is done here every
# time we boot, but only if needed.
# Only copy in the new version if the destination file doesn't exist or
# shows a difference.  Note that diff returns true if there were no
# differences.
src=/usr/local/fireball/defaults/sysconfig/ppp/options
dst=/etc/sysconfig/ppp/options
if [[ ! -f $dst ]] || ! diff $src $dst > /dev/null
then
  echo "Updating $dst"
  cp $src $dst
fi
