#!/bin/sh
#
# ----------------------------------------------------------
# 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.
#
# ----------------------------------------------------------

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

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

# Check for dos2unix
which dos2unix > /dev/null 2>&1
[ $? -gt 0 ] && echo "FATAL: Unable to locate dos2unix utility" && exit -1

echo "INFO: Starting Spine build process"

# Remove software build specific directories
echo "INFO: Removing cache directories"
rm -rf autom4te.cache .deps

# Make sure all files are unix formatted files 
find . -type f -exec dos2unix --d2u \{\} \; > /dev/null 2>&1

# Prepare a build state
echo "INFO: Running auto-tools to verify buildability"
autoreconf --force --install
[ $? -ne 0 ] && echo "ERROR: 'autoreconf' exited with errors" && exit -1

# Provide some meaningful notes
echo "INFO: Spine bootstrap process completed"
echo ""
echo "  To compile and install Spine do the following:"
echo ""
echo "  ./configure"
echo "  make"
echo "  make install"
echo ""

exit 0
