#!/bin/sh
# oapr build bootstrap script

BOOTSTRAP_CONF=config/bootstrap.conf

# Build configure arguments function

build_confargs()
{

  if test $CONF_DEBUG = 1; then
    CONF_ARGS="$CONF_ARGS --enable-debug --enable-maintainer-mode";
  fi

  if test $CONF_EFENCE = 1; then
    CONF_ARGS="$CONF_ARGS --enable-efence";
  fi

  if test $CONF_TESTS = 1; then
    CONF_ARGS="$CONF_ARGS --enable-tests";
  fi

  if test $CONF_APR; then
    CONF_ARGS="$CONF_ARGS --with-apr=$CONF_APR";
  fi

  if test $CONF_APU; then
    CONF_ARGS="$CONF_ARGS --with-apu=$CONF_APU";
  fi

  if test $CONF_OAPX = 1; then
    CONF_ARGS="$CONF_ARGS --enable-oapx";
  fi

  if test $CONF_BOOST; then
      CONF_ARGS="$CONF_ARGS --with-boost=$CONF_BOOST";
  fi

  if test $CONF_PREFIX; then
    CONF_ARGS="$CONF_ARGS --prefix=$CONF_PREFIX";
  fi

  if test $CONF_RPM = 1; then
      CONF_ARGS="$CONF_ARGS --enable-rpm";
  fi
};

# Check for first-time run and create config file if necessary

if test ! -f $BOOTSTRAP_CONF; then
  cat <<__EOF__;

Hi! You appear to be running bootstrap for the first time.

I've generated a build configuration file: $BOOTSTRAP_CONF

Please view this file and change it to suit your system if necessary,
particularly the paths to required programs, and the install prefix.

Once you are happy with the configuration, run $0 to configure the build.

For advanced build options, try $0 -h

If you encounter build problems, check your settings, make sure everything
required is installed, and if you still have no luck, consult documentation
and the web.

__EOF__
  cat >$BOOTSTRAP_CONF <<__EOF__;
# oapr build configuration
# this file will not be overwritten when checking out from cvs
# 0=no, 1=yes, comment out or leave blank to disable
CONF_DEBUG=1            # Do a debug build
CONF_EFENCE=0           # Use electric fence if its available
CONF_TESTS=1            # Build tests
CONF_OAPX=1             # Build oapx (extension library)
CONF_APR=/usr/local/bin # Location of apr-config
CONF_APU=/usr/local/bin # Location of apu-config
CONF_BOOST=             # Location of boost includes
CONF_RPM=               # Enable rpm builds
CONF_ARGS=              # Any additional args to pass to configure
#CONF_PREFIX=/usr       # Prefix to install to (e.g. /usr or /usr/local). Leave blank for default.
__EOF__
  exit 4;
fi

# Usage function

usage()
{
  echo "Usage: $0 [options]"
  echo "Options:"
  echo " -e show proposed configure arguments"
  echo " -a run autotools; libtoolize, aclocal, autoconf,automake "
  echo " -c run configure"
  echo " -d force debug build (overrides $BOOTSTRAP_CONF)"
  echo " -h display this message"
  echo "Default behavior is to create a release build and to run autotools and configure."
};

### MAIN CODE

# Parse command-line options

set -- `getopt am:cm:em:dm:lm:sm:h $*`;
if test $? != 0; then
  usage;
  exit 1;
fi

echo_confargs=off;
run_autoconf=off;
run_configure=off;
debug=off;
user_class=expert;

# No args, assume -ac and don't assume expert user
if test $# == 1; then
  run_autoconf=on;
  run_configure=on;
  user_class=unknown;
fi

# Parse options, if any
for i in $*; do
  case $i in
    -e) echo_confargs=on; shift;;
    -a) run_autoconf=on; shift;;
    -c) run_configure=on; shift;;
    -d) debug=on; shift;;
    -h) usage; exit 1;;
    --) shift; break;;
  esac
done;

# Read bootstrap.conf

if test $user_class != "expert"; then
  echo "Reading $BOOTSTRAP_CONF.";
fi
. $BOOTSTRAP_CONF

# -d: Force debug build
if test $debug = "on"; then
  CONF_DEBUG=1;
fi

# Run autotools
if test $run_autoconf = "on"; then

  echo "Running autotools."

  # Make sure config dir exists
  if ! test -d config ; then
    mkdir config || exit $?
  fi
  rm -f config.status
  rm -f config.cache
  rm -f acconfig.h

  echo "..libtoolizing."
  libtoolize --force --copy || exit $?
  echo "..aclocal."
  aclocal -I m4 || exit $?
  echo "..autoconf."
  autoconf || exit $?
  echo "..autoheader."
  autoheader - configure.in || exit $?
  echo "..automake."
  automake -a || exit $?

fi

# Build configure args from command line
build_confargs;

# Show the configure args if -e specified or running configure
if test $echo_confargs = "on" -o $run_configure = "on"; then
  echo "./configure ${CONF_ARGS}"
fi

# Run configure
if test $run_configure = "on"; then

  echo "Running configure."
  eval ./configure $CONF_ARGS || exit $?

  if test $user_class != "expert"; then
    echo
    echo "That worked. Now run 'make'."
    echo
  fi
fi
