#!/bin/bash
#
# buildcmake
#
# -- This script downloads and installs the CMake cross-platform Makefile generator
#    (http://www.cmake.org).
#
# OpenCoarrays is distributed under the OSI-approved BSD 3-clause License:
# Copyright (c) 2015, Sourcery, Inc.
# Copyright (c) 2015, Sourcery Institute
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, 
# are permitted provided that the following conditions are met:
# 
# 1. Redistributions of source code must retain the above copyright notice, this 
#    list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice, this 
#    list of conditions and the following disclaimer in the documentation and/or 
#    other materials provided with the distribution.
# 3. Neither the names of the copyright holders nor the names of their contributors 
#    may be used to endorse or promote products derived from this software without 
#    specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
# IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

cmd=`basename $0` 
usage()
{
    echo ""
    echo " $cmd - Bash script for building CMake from source"
    echo ""
    echo " Usage (optional arguments in square brackets): "
    echo "      $cmd <version-number> [<installation-path> <number-of-threads>]"
    echo " or   $cmd [options] "
    echo ""
    echo " Options:"
    echo "   --help, -h           Show this help message"
    echo "   --version, -v, -V    Report version and copyright information"
    echo ""
    echo " Example usage:"
    echo ""
    echo "   $cmd default"
    echo "   $cmd 3.3.2"
    echo "   $cmd 3.3.2 /opt/cmake/3.3.2 4"
    echo "   $cmd -v" 
    echo "   $cmd --help"
    echo ""
    echo " Note: For a list of available CMake versions, visit"
    echo " http://www.cmake.org/files/"
    echo ""
    exit 1
}

# Default to installing CMake 3.3 if no version specified in the first
# command-line argument
if [[ $1 == 'default' ]]; then
  version=3.3.2
else
  version=$1 
fi

# Default to installing in the present working directory if no install path is specified:
if [ -z $2 ]; then
  install_path=${PWD}
else
  install_path=$2
fi

# Default to 2 threads if no specified thread count:
if [ -z $3 ]; then
  num_threads=2
else
  num_threads=$3
fi

check_prerequisites()
{
  unamestr=`uname`
  if [[ "$unamestr" == 'Darwin' ]]; then
    if [ -f /usr/bin/gcc ]; then
      CC=/usr/bin/gcc
    else
      echo "Missing /usr/bin/gcc, which must exist and be an Apple LLVM version."
      exit 1
    fi
    if [ -f /usr/bin/g++ ]; then
      CXX=/usr/bin/g++
    else
      echo "Missing /usr/bin/g++, which must exist and be an Apple LLVM version."
      exit 1
    fi
  else
    # If this is not OS X, default to the user-specified compiler.
    # If there is no user-specified compiler, use whatever the 
    # GNU compilers are in the path.
    if [ -z $CC ]; then
      CC=gcc
    fi
    if [ -z $CXX ]; then
      CXX=g++
    fi
  fi

  # Verify that 'wget' is is in the path.
  if ! type wget > /dev/null; then
    echo 
    echo "$cmd requires 'wget'.  Please ensure that it is installed and in your path.  Aborting." 
    exit 1
  fi
  if ! type make > /dev/null; then
    echo 
    echo "$cmd requires 'make'.  Please ensure that it is installed and in your path.  Aborting." 
    exit 1
  fi
}

# Download CMake if the tar ball is not already in the present working directory
download()
{
  if [ ! -f "cmake-$version.tar.gz" ]; then
    echo "Downloading cmake-$version.tar.gz"
    major_minor="${version%.*}"
    wget http://www.cmake.org/files/v$major_minor/cmake-$version.tar.gz 
  fi
}

# Unpack CMake if the unpacked tar ball is not in the present working directory
unpack()
{
  if [ ! -d "cmake-$version" ]; then
    tar xvzf cmake-$version.tar.gz 
  fi
}

# Make the build directory, configure, and build
build()
{
  cd cmake-$version &&
  CC=$CC CXX=$CXX ./bootstrap --prefix=$install_path &&
  make -j $num_threads
}

if [ $# == 0 ]; then
  # Print usage information if script is invoked without arguments
  usage | less
elif [[ $1 == '--help' || $1 == '-h' ]]; then
  # Print usage information if script is invoked with --help or -h argument
  usage | less
elif [[ $1 == '-v' || $1 == '-V' || $1 == '--version' ]]; then
  # Print script copyright if invoked with -v, -V, or --version argument
  echo ""
  echo "CMake Build Script"
  echo "Copyright (C) 2015 Sourcery, Inc."
  echo "Copyright (C) 2015 Sourcery Institute"
  echo ""
  echo "$cmd comes with NO WARRANTY, to the extent permitted by law."
  echo "You may redistribute copies of $cmd under the terms of the"
  echo "BSD 3-Clause License.  For more information about these matters, see"
  echo "http://www.sourceryinstitute.org/license.html"
  echo ""
else
  # Download, unpack, and build CMake
  time \
  {
    check_prerequisites &&
    download &&
    unpack  &&
    CC=$CC CXX=$CXX build $version $install_path $num_threads
  } >&1 | tee build.log
  echo ""
  echo "Check build.log for results.  If the build was successful, type"
  echo "'cd cmake-$version && make install' (or 'cd cmake-$version && sudo make install')"
  echo "to complete the installation."
  echo ""
fi
