#!/bin/bash
#
# buildmpich
#
# -- This script downloads and installs the MPICH library (http://www.mpich.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 MPICH from source"
    echo ""
    echo " Usage (optional arguments in square brackets): "
    echo "      $cmd <version-number> <install-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.1.4 /opt/mpich/3.1.4 4"
    echo "   $cmd -v" 
    echo "   $cmd --help"
    echo ""
    echo " Note: For a list of available MPICH versions, visit"
    echo " http://www.mpich.org/static/downloads/"
    echo ""
    exit 1
}

# Default to compiling with the GNU compilers if the environment 
# variables CC, FC, and CXX, are empty:
if [ -z "$CC" ]; then
  CC=gcc
fi
if [ -z "$FC" ]; then
  FC=gfortran
fi
if [ -z "$CXX" ]; then
  CXX=g++
fi

# Default to installing MPICH 3.1.4 if no version specified in the first
# command-line argument
if [[ $1 == 'default' ]]; then
  version=3.1.4
else
  version=$1 
fi
# Default to installing in the current directory if no path is provided
# in the second command-line argument
if [ -z $2 ]; then
  install_path=${PWD}mpich-$version-installation
else
  install_path=$2
fi

# Default to building with 2 threads if no thread count is specified
# in the third command-line argument
if [ -z $3 ]; then
  num_threads=2
else
  num_threads=$3
fi

src=mpich-$version

# Make the build directory, configure, and build
build()
{
   mkdir -p $src\-build &&
   cd $src\-build &&
   CC=$CC FC=$FC CXX=$CXX ../$src/configure --prefix=$install_path && 
   CC=$CC FC=$FC CXX=$CXX 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 "MPICH 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 mpich
  time \
  {
    if ! type wget > /dev/null; then
      echo 
      echo "$cmd requires 'wget'.  Please install it.  Aborting." 
      exit 1;
    else
      # Download MPICH
      wget http://www.mpich.org/static/downloads/$version/$src.tar.gz &&
      # Unpack the downloaded tape archive
      tar xvzf $src.tar.gz &&
      # Compile MPICH source
      build $version $install_path $num_threads
    fi
  } >&1 | tee build.log
  echo "Check build.log for results.  If the build was successful, type"
  echo "'cd $src-build && make install' (or 'cd $src-build && sudo make install') "
  echo "to complete the installation."
fi
