#!/bin/sh
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

# $Id

# This script should be used to initalize a Condor build.  It checks
# for the required versions of various GNU autoconf tools (autoconf,
# autoheader, etc) needed for building Condor, and runs them to setup
# a working configure script.  Once this script is done, the build is
# ready for "./configure; make".

# Global flag that indicates we have everything we need.  will be set
# to 'no' if we find something we're missing or that isn't new enough.
have_required=yes

# ------------------------------------------------------------
# Check that a given command (arg $1) has version >= arg $2
# returns 0 on success, 1 on failure
# ------------------------------------------------------------
check_version()
{
  cmd=$1
  target_version=$2
  success=no;
  echo -n "Checking for version of $1 >= $target_version..."
  path=`type $cmd 2>/dev/null | awk '{print $(NF)}'`
  if [ -x "$path" ]; then
    cmdline="$path --version";
    version=`$cmdline 2>/dev/null | head -n 1 | awk '{print $(NF)}'`
    if [ -z "$version" ]; then version=0; fi
    success=`echo "$version" "$target_version" | awk '{ if ($1 >= $2) {print "yes";} else {print "no";}} '`
  else
    echo "command not found";
    have_required=no;
    return 1;
  fi

  if [ $success = "yes" ] ; then
    echo "succeeded. ($version)"
    return 0;
  else
    echo "failed. ($version)"
    have_required=no;
    return 1;
  fi
}


# ------------------------------------------------------------
# Check prerequisites
# ------------------------------------------------------------

check_version autoheader 2.59

check_version autoconf 2.59

if [ $have_required != "yes" ] ; then
  echo "Some of the required tools are missing or have the wrong version."
  echo "For each of the tools that failed above, please install the"
  echo "requested version (or higher), and run ./build_init again."
  exit 1
fi


# ------------------------------------------------------------
# Prepare configure script
# ------------------------------------------------------------

echo "Required tools are present and valid, attempting to initialize build"
autocmd="autoheader && autoconf"

if eval $autocmd; then
  echo "Build initialized, you can now run \"./configure; make\""
  exit 0 
else
  echo "Failed to initialize build, check errors and try again once fixed"
  exit 1
fi

