#!/bin/sh
#
# Copyright (C) 2003-2015 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 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.
#

function is_enabled()
{
    local var=$1
    if [[ "${var}" == "enable" || "${var}" == "yes" || "${var}" == "1" ||  "${var}" == "on" || ${var} == "true" ]]; then 
        echo true
    else
        echo false
    fi
}

function is_enabled_cmd()
{
    local var=$1
    if [[ $(is_enabled $var) == true || $var == "-"* || $var == "" ]]; then
        echo true
    else
        echo false
    fi
}

# Environment

fast_on=$(is_enabled "${I_MPI_TUNE_FAST}")
rank_placement_on=$(is_enabled "${I_MPI_TUNE_RANK_PLACEMENT}")

# Command line

params=
skip=false
while (( "$#" )); do

    param=$1

    if [[ "x${param}" == "x\"" ]]; then
        if [[ $skip == true ]]; then
            skip=false
        else
            skip=true
        fi
    else
        if [[ $param == "\""* ]]; then
            skip=true
        fi

        if [[ $param == *"\"" && "x${param}" != "x" ]]; then
            skip=false
        fi
    fi
 
    ret_val=""
    if [[ $skip == false ]]; then 
        val=$2

        if [[ $param == "--rank-placement" || $param == "-rp" ]]; then
            ret_val=$(is_enabled_cmd $val)
            if [[ $ret_val == "true" && $val != "-"* && "x${val}" != "x" ]]; then
                shift
            fi
            rank_placement_on=$ret_val
        elif [[ $1 == "--fast" || $1 == "-f" ]]; then
            ret_val=$(is_enabled_cmd $val)
            if [[ $ret_val == true && $val != "-"* && "x${val}" != "x" ]]; then
                shift
            fi
            fast_on=$ret_val
        fi
    fi

    if [[ "x${ret_val}" == "x" ]]; then
        params="${params} ${param}"
    fi
    shift
done

bin=
if [[ $fast_on == true ]]; then
    bin=$I_MPI_ROOT/intel64/bin/tune/mpitune_app
elif [[ $rank_placement_on == true ]]; then
    bin=$I_MPI_ROOT/intel64/bin/tune/mpitune_rank_placement
else
    bin=$I_MPI_ROOT/intel64/bin/tune/mpitune
fi

$bin $params

