+function bootstrap ()
+{
+ determine_target_platform
+
+ determine_vcs
+
+ # Set up whatever we need to do to use autoreconf later
+ require_libtoolise
+ if ! autoreconf_setup; then
+ return 1
+ fi
+
+ if [ -z "$MAKE_TARGET" ]; then
+ MAKE_TARGET="make_default"
+ fi
+
+ if $PRINT_SETUP_OPTION -o $DEBUG; then
+ echo
+ print_setup
+ echo
+
+ # Exit if all we were looking for were the currently used options
+ if $PRINT_SETUP_OPTION; then
+ exit
+ fi
+ fi
+
+ # Use OLD_TESTS_ENVIRONMENT for tracking the state of the variable
+ local OLD_TESTS_ENVIRONMENT=
+
+ # Set ENV PREFIX in order to set --prefix for ./configure
+ if [[ -n "$PREFIX" ]]; then
+ PREFIX_ARG="--prefix=$PREFIX"
+ fi
+
+ # We should always have a target by this point
+ assert MAKE_TARGET
+
+ local MAKE_TARGET_ARRAY=($MAKE_TARGET)
+
+ for target in "${MAKE_TARGET_ARRAY[@]}"
+ do
+ # If we are running inside of Jenkins, we want to only run some of the possible tests
+ if $jenkins_build_environment; then
+ check_make_target $target
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ die "Unknown MAKE_TARGET option: $target"
+ fi
+ fi
+
+ local snapshot_run=false
+
+ case $target in
+ 'self')
+ self_test
+ ;;
+ 'gdb')
+ make_gdb
+ ;;
+ 'install-html')
+ make_install_html
+ ;;
+ 'clean_op')
+ make_clean_option
+ ;;
+ 'autoreconf')
+ make_for_autoreconf
+ ;;
+ 'install-system')
+ make_install_system
+ ;;
+ 'configure')
+ run_configure
+ ;;
+ 'make_default')
+ make_default
+ ;;
+ 'clang')
+ if ! check_clang; then
+ die "clang was not found"
+ fi
+
+ if ! make_for_clang; then
+ die "Failed to build clang: $?"
+ fi
+ ;;
+ 'clang-analyzer')
+ if ! check_clang_analyzer; then
+ die "clang-analyzer was not found"
+ fi
+ if ! check_clang; then
+ die "clang was not found"
+ fi
+
+ if ! make_for_clang_analyzer; then
+ die "Failed to build clang-analyzer: $?"
+ fi
+ ;;
+ 'mingw')
+ if ! check_mingw; then
+ die "mingw was not found"
+ fi
+
+ if ! make_for_mingw; then
+ die "Failed to build mingw: $?"
+ fi
+ ;;
+ 'snapshot')
+ make_for_snapshot
+ snapshot_run=true
+ ;;
+ 'rpm')
+ make_rpm
+ ;;
+ 'valgrind')
+ make_valgrind
+ ;;
+ 'universe')
+ make_universe
+ ;;
+ 'jenkins')
+ make_for_continuus_integration
+ ;;
+ *)
+ run_configure_if_required
+ make_target "$target"
+ ;;
+ esac
+
+ if $jenkins_build_environment; then
+ if ! $snapshot_run; then
+ run_make_maintainer_clean_if_possible
+ fi
+ fi
+
+ done
+}
+
+function main ()
+{
+ # Variables we export
+ declare -x VCS_CHECKOUT=
+
+ # Variables we control globally
+ local MAKE_TARGET=
+ local CONFIGURE=
+
+ # Options for getopt
+ local AUTORECONF_OPTION=false
+ local CLEAN_OPTION=false
+ local CONFIGURE_OPTION=false
+ local DEBUG_OPTION=false
+ local PRINT_SETUP_OPTION=false
+ local TARGET_OPTION=false
+ local TARGET_OPTION_ARG=
+ local VERBOSE_OPTION=false
+
+ local OLD_CONFIGURE=
+ local OLD_CONFIGURE_ARG=
+ local OLD_PREFIX=
+ local OLD_MAKE=
+ local OLD_TESTS_ENVIRONMENT=
+
+ # If we call autoreconf on the platform or not
+ local AUTORECONF_REBUILD_HOST=false
+ local AUTORECONF_REBUILD=false
+
+ local -r top_srcdir=`pwd`
+
+ # Default configure
+ if [ -z "$CONFIGURE" ]; then
+ CONFIGURE="$top_srcdir/configure"
+ fi
+
+
+ # Variables for determine_target_platform () and rebuild_host_os ()
+ # UNAME_MACHINE_ARCH= uname -m
+ # VENDOR= apple, redhat, centos, canonical
+ # VENDOR_RELEASE=
+ # RHEL{rhel,Tikanga,Santiago}
+ # Ubuntu{ubuntu,Lucid,Maverick,Natty,Oneiric,Precise,Quantal}
+ # Fedora{fedora,Verne,Beefy}
+ # OSX{osx,lion,snow,mountain}
+ # VENDOR_DISTRIBUTION= darwin,fedora,rhel,ubuntu
+ # UNAME_KERNEL= Linux, Darwin,...
+ # UNAME_KERNEL_RELEASE= Linux, Darwin,...
+ local UNAME_MACHINE_ARCH=unknown
+ local VENDOR=unknown
+ local VENDOR_RELEASE=unknown
+ local VENDOR_DISTRIBUTION=unknown
+ local UNAME_KERNEL=unknown
+ local UNAME_KERNEL_RELEASE=unknown
+ local HOST_OS=
+
+ rebuild_host_os no_output
+
+ parse_command_line_options $@
+
+ # If we are running under Jenkins we predetermine what tests we will run against
+ # This MAKE_TARGET can be overridden by parse_command_line_options based MAKE_TARGET changes.
+ # We don't want Jenkins overriding other variables, so we NULL them.
+ if [ -z "$MAKE_TARGET" ]; then
+ if $jenkins_build_environment; then
+ if [[ -n "$label" ]]; then
+ check_make_target $label
+ if [ $? -eq 0 ]; then
+ MAKE_TARGET="$label"
+ fi
+ fi
+
+ if [ -z "$MAKE_TARGET" ]; then
+ MAKE_TARGET='jenkins'
+ fi
+ fi
+ fi
+
+ bootstrap
+
+ jobs -l
+ wait
+
+ exit 0
+}
+
+function set_branch ()
+{
+ if [ -z "$BRANCH" ]; then
+ if [ -z "$CI_PROJECT_TEAM" ]; then
+ die "Variable CI_PROJECT_TEAM has not been set"
+ fi
+ if [ -z "$PROJECT" ]; then
+ die "Variable PROJECT has not been set"
+ fi
+ if [ -z "$BUILD_TAG" ]; then
+ die "Variable BUILD_TAG has not been set"
+ fi
+
+ BRANCH="lp:~$CI_PROJECT_TEAM/$PROJECT/$BUILD_TAG"
+ export BRANCH
+ fi
+
+ if [ -z "$BRANCH" ]; then
+ die "Missing values required to build BRANCH variable."
+ fi
+}
+
+function merge ()
+{
+ if [ -z "$VCS_CHECKOUT" ]; then
+ die "Merges require VCS_CHECKOUT."
+ fi
+
+ set_branch
+
+ if [[ "$VCS_CHECKOUT" == 'bzr' ]]; then
+ if test -n "$BRANCH_TO_MERGE"; then
+ bzr merge $BRANCH_TO_MERGE
+ bzr commit --message="Merge $BRANCH_TO_MERGE Build: $BUILD_TAG" --unchanged
+ fi
+
+ bzr push "$BRANCH"
+ elif [[ -n "$VCS_CHECKOUT" ]]; then
+ die "Merge attempt occured, current VCS setup does not support this"
+ fi
+}
+
+function enable_debug ()
+{
+ if ! $DEBUG; then
+ local caller_loc=`caller`
+ if [ -n $1 ]; then
+ echo "$caller_loc Enabling debug: $1"
+ else
+ echo "$caller_loc Enabling debug"
+ fi
+ set -x
+ DEBUG=true
+ fi
+}
+
+function usage ()
+{
+ cat << EOF
+ Usage: $program_name [OPTION]..
+
+ Bootstrap this package from the checked-out sources, and optionally walk through CI run.
+
+ Options:
+
+EOF
+}
+
+function disable_debug ()
+{
+ set +x
+ DEBUG=true
+}
+
+# Script begins here
+
+program_name=$0
+
+env_debug_enabled=false
+if [[ -n "$JENKINS_HOME" ]]; then
+ declare -r jenkins_build_environment=true
+else
+ declare -r jenkins_build_environment=false
+fi
+
+export ACLOCAL
+export AUTOCONF
+export AUTOHEADER
+export AUTOM4TE
+export AUTOMAKE
+export AUTORECONF
+export CONFIGURE_ARG
+export DEBUG
+export GNU_BUILD_FLAGS
+export LIBTOOLIZE
+export LIBTOOLIZE_OPTIONS
+export MAKE
+export PREFIX_ARG
+export TESTS_ENVIRONMENT
+export VERBOSE
+export WARNINGS
+
+case $OSTYPE in
+ darwin*)
+ export MallocGuardEdges
+ export MallocErrorAbort
+ export MallocScribble
+ ;;
+esac
+
+# We check for DEBUG twice, once before we source the config file, and once afterward
+env_debug_enabled=false
+if [[ -n "$DEBUG" ]]; then
+ env_debug_enabled=true
+ enable_debug
+ print_setup
+fi
+
+# Variables which only can be set by .bootstrap
+BOOTSTRAP_SNAPSHOT=false
+BOOTSTRAP_SNAPSHOT_CHECK=
+
+if [ -f '.bootstrap' ]; then
+ source '.bootstrap'
+fi
+
+if $env_debug_enabled; then
+ enable_debug
+else
+ if [[ -n "$DEBUG" ]]; then
+ enable_debug "Enabling DEBUG from '.bootstrap'"
+ print_setup
+ fi
+fi
+
+# We do this in order to protect the case where DEBUG
+if ! $env_debug_enabled; then
+ DEBUG=false
+fi