#!/bin/ksh
#
# ----------------------------------------------------------
# Name: bootstrap
#
# Function: build spine from scratch
#
# Description: This script will take a vanilla Spine source
#              package and attempt to compile it.  It will 
#              attempt to handle nasty things like dos2unix
#              issues in all files and searching for the
#              presence of required modules.
#
#              It is not a replacement for the auto tools,
#              but simply a supplement.
#
# ----------------------------------------------------------
# Set debugging
# ----------------------------------------------------------
#set -x

# ----------------------------------------------------------
# Help function
# ----------------------------------------------------------
display_help () {
  echo "Spine bootstrap build function"
  echo "Attempts to build spine based on 'normal' system"
  echo "If you install things in non-common locations"
  echo "you may have to use the install instructions to"
  echo "build."
}

# ----------------------------------------------------------
# Check for parameters
# ----------------------------------------------------------
if [ "${1}x" == "--helpx" ]; then
  display_help
  exit 0
fi

# ----------------------------------------------------------
# Remove software build specific directories
# ----------------------------------------------------------
echo "--------------------------------------------------------------"
echo "Spine build process is beginnning"
echo "--------------------------------------------------------------"
echo "NOTE: Removing hardware specific directories"
rm -rf autom4te.cache .deps

# ----------------------------------------------------------
# Get rid of nasty Microsoftisms
# ----------------------------------------------------------
if [ `which dos2unix` != "" ]; then
  echo "--------------------------------------------------------------"
  echo "NOTE: Performing a dos2unix conversion of all files"
  for FILE in `ls`; do
    if [ ! -d $FILE ]; then
      dos2unix $FILE > /dev/null 2>&1
    fi
  done

  for FILE in `ls config`; do
    if [ -d $FILE ]; then
      dos2unix $FILE > /dev/null 2>&1
    fi
  done
else
  echo "--------------------------------------------------------------"
  echo "NOTE: dos2unix not found, not double checking file formats"
fi

# ----------------------------------------------------------
# Make sure things are executable if they exist
# ----------------------------------------------------------
echo "--------------------------------------------------------------"
echo "NOTE: Setting some permissions"
[ -f ./configure ] && chmod +x configure

# ----------------------------------------------------------
# Prepare a build state
# ----------------------------------------------------------
echo "--------------------------------------------------------------"
echo "NOTE: Running auto-tools to verify buildability"
aclocal
[ $? -ne 0 ] && echo "FATAL: aclocal has errors" && exit -1

autoheader
[ $? -ne 0 ] && echo "FATAL: autoheader has errors" && exit -1

automake --foreign --add-missing --copy
[ $? -ne 0 ] && echo "FATAL: automake has errors" && exit -1

autoconf
[ $? -ne 0 ] && echo "FATAL: autoconf has errors" && exit -1

libtoolize --force
[ $? -ne 0 ] && echo "FATAL: libtool has errors" && exit -1

# ----------------------------------------------------------
# perform the build
# ----------------------------------------------------------
echo "--------------------------------------------------------------"
echo "NOTE: Performing build of spine"
echo "--------------------------------------------------------------"
./configure --sysconfdir=/etc --bindir=/usr/bin
make clean
make install
sleep 3
clear

# ----------------------------------------------------------
# Provide some meaningful notes
# ----------------------------------------------------------
echo "--------------------------------------------------------------"
echo "Spine build process complete!!!"
echo "--------------------------------------------------------------"
echo "NOTE: Spine has been installed in /usr/bin"
echo "NOTE: A spine config file has been located in /etc"
echo "NOTE: This file must be renamed from spine.conf.dist to"
echo "NOTE: spine.conf and then updated to include the correct"
echo "NOTE: settings for you cacti install."
