#! /bin/sh
#
#********* Copyright (c) 2002-2004 Trimble Navigation Limited **********
# $Id: fireball,v 1.44.2.2 2004/09/10 21:01:51 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

########################################################################
function checkChecksum
########################################################################
# Perform a checksum on a specified file and compare that to the 
# expected results.  This is designed to detect file system buffer
# corruptions which could result in faulty operation.
{
  file=$1
  cs=$2
  set - $(/usr/bin/cksum $1)
  #echo "checksum $file == $1 (exp==$cs)"
  if [[ $1 != $cs ]]
  then
    echo "BAD CHECKSUM ON $file.  CS is $1 not $cs."
    echo "->->->->->-> FSCK partition 4 then Reboot <-<-<-<-<-<-"
    umount /var/gps
    /sbin/e2fsck -fpy -C0 /dev/discs/disc0/part4
    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 [ ! -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


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"
  checkChecksum /sbin/modprobe 795814846
  checkChecksum /bin/bash 3496647556
  checkChecksum /bin/busybox 1079250080
  checkChecksum /usr/bin/find 3566075820
  checkChecksum /lib/libcrypt-2.2.3.so 2246793921
  checkChecksum /lib/libm-2.2.3.so 1171445454
  checkChecksum /lib/libc-2.2.3.so 3604821520
  checkChecksum /lib/ld-2.2.3.so 779241293
  checkChecksum /lib/librt-2.2.3.so 3899987835
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
