#! /bin/bash
# INTEL CONFIDENTIAL
# Copyright (c) 2008-2012  Intel Corporation All Rights Reserved.
# The source code contained or described herein and all documents related
# to the source code ("Material") are owned by Intel Corporation or its
# suppliers or licensors. Title to the Material remains with Intel Corporation
# or its suppliers and licensors. The Material contains trade secrets and
# proprietary and confidential information of Intel or its suppliers and
# licensors. The Material is protected by worldwide copyright and trade
# secret laws and treaty provisions. No part of the Material may be used,
# copied, reproduced, modified, published, uploaded, posted, transmitted,
# distributed, or disclosed in any way without Intel's prior express
# written permission.
#
# No license under any patent, copyright, trade secret or other intellectual
# property right is granted to or conferred upon you by disclosure or delivery
# of the Materials, either expressly, by implication, inducement, estoppel or
# otherwise. Any license under such intellectual property rights must be
# express and approved by Intel in writing.

# [ICS VERSION STRING: @(#) ./do_build 3_18_3_4 [01/25/18 09:30]

# rebuild selected MPI to target a specific compiler

Usage()
{
	echo "Usage: do_build [-d] [-Q] [mpi [config_opt [install_dir]]]" >&2
	echo "        -d - use default settings for selected MPI's options" >&2
	echo "            if omitted, will be prompted for each option" >&2
	echo "        -Q - build the MPI targeted for the PSM API." >&2
	echo "        mpi - MPI to build (mvapich2 or openmpi)" >&2
	echo "        config_opt - a compiler selection option (gcc, pathscale, pgi or intel)" >&2
	echo "             if config_opt is not specified, the user will be prompted" >&2
	echo "             based on compilers found on this system" >&2
	echo "        install_dir - where to install MPI, see MPICH_PREFIX below" >&2
	echo "" >&2
	echo "Environment:" >&2
	echo "    STACK_PREFIX - where to find IB stack." >&2
	echo "            Default obtained from /etc/infiniband/info, typically /usr" >&2
	echo "    BUILD_DIR - temporary directory to use during build of MPI" >&2
	echo "            /var/tmp/Intel-mvapich2 or /var/tmp/Intel-openmpi" >&2
	echo "    MPICH_PREFIX - selects location for installed MPI" >&2
	echo "            default is /usr/mpi/<COMPILER>/<MPI>-<VERSION>" >&2
	echo "            where COMPILER is selected compiler (gcc, pathscale, etc above)" >&2
	echo "            MPI is mvapich2 or openmpi" >&2
	echo "            and VERSION is MPI version (eg. 1.0.0)" >&2
	echo "    CONFIG_OPTIONS - additional configuration options to be" >&2
	echo "            specified to srpm, default is ''" >&2
	echo "    INSTALL_ROOT - location of system image in which to install." >&2
	echo "            Default is '/'" >&2
	echo "" >&2
	echo "The RPMs built during this process will be installed on this system" >&2
	echo "they can also be found in /opt/iba/src/MPI" >&2
	exit 2
}

build_opts=
while getopts "Qd" o
do
	case "$o" in
	d) build_opts="$build_opts -d";;
	Q) build_opts="$build_opts -Q";;
	*) Usage;;
	esac
done
shift $((OPTIND -1))
if [ $# -gt 3 ]
then
	Usage
fi

if [ "$(/usr/bin/id -u)" != 0 ]
then
	echo "You must be 'root' to run this program" >&2
	exit 1
fi
cd /opt/iba/src/MPI
if [ $? != 0 ]
then
	echo "Unable to cd to /opt/iba/src/MPI" >&2
	exit 1
fi

echo
echo "OFED MVAPICH MPI Library/Tools rebuild"
mpi="$1"
compiler="$2"
if [ ! -z "$3" ]
then
	export MPICH_PREFIX="$3"
fi

nompi()
{
	echo "No MPI Build scripts Found, unable to Rebuild MPI" >&2
	exit 1
}

if [ -z "$mpi" ]
then
	choices=""
	for i in openmpi mvapich2
	do
		if [ -e do_${i}_build ]
		then
			choices="$choices $i"
		fi
	done
	if [ x"$choices" = x ]
	then
		nompi
	else
		PS3="Select MPI to Build: "
		select mpi in $choices
		do
			case "$mpi" in
			mvapich2|openmpi) break;;
			esac
		done
	fi
fi

./do_${mpi}_build $build_opts "$compiler"
exit $?
