#!/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"
    echo "   $cmd 3.3 /opt/cmake/3.3 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
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

# Make the build directory, configure, and build
build()
{
  cd cmake-$version.0 &&
  ./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 and build CMake
  time \
  {
    if ! type wget > /dev/null; then
      echo 
      echo "$cmd requires 'wget'.  Please install it.  Aborting." 
      exit 1;
    else
      # Download CMake
      wget http://www.cmake.org/files/v$version/cmake-$version.0-1-src.tar.bz2 &&
      # Unpack the downloaded tape archive
      tar xvjf cmake-$version.0-1-src.tar.bz2 &&
      tar xvjf cmake-$version.0.tar.bz2 &&
      # Compile Cmake source
      build $version $install_path $num_threads
    fi
  } >&1 | tee build.log
  echo ""
  echo "Check build.log for results.  If the build was successful, type"
  echo "'cd cmake-$version.0 && make install' (or 'cd cmake-$version.0 && sudo make install')"
  echo "to complete the installation."
  echo ""
fi
