#!/bin/csh
#
# Copyright 2017 Intel Corporation.
#
# The source code, information and material ("Material") contained herein is owned by
# Intel Corporation or its suppliers or licensors, and title to such Material remains
# with Intel Corporation or its suppliers or licensors. The Material contains
# proprietary information of Intel or its suppliers and licensors. The Material is
# protected by worldwide copyright 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 or other intellectual property rights in the
# Material is granted to or conferred upon you, either expressly, by implication,
# inducement, estoppel or otherwise. Any license under such intellectual property
# rights must be express and approved by Intel in writing.
#
# Unless otherwise agreed by Intel in writing, you may not remove or alter this notice
# or any other notice embedded in Materials by Intel or Intel's suppliers or licensors
# in any way.

# Parsing script arguments
# Arg1 represents target architecture. Its possible values are 'ia32' or 'intel64',
# default value equals to the value of $COMPILERVARS_ARCHITECTURE environment variable.

set PSTL_TARGET_ARCH=""

if ($?COMPILERVARS_ARCHITECTURE) then
    set PSTL_TARGET_ARCH="$COMPILERVARS_ARCHITECTURE"
endif

if ("$1" != "") then
    set PSTL_TARGET_ARCH="$1"
endif

if ("$PSTL_TARGET_ARCH" != "") then
    if ("$PSTL_TARGET_ARCH" != "ia32" && "$PSTL_TARGET_ARCH" != "intel64") then
        echo "ERROR: Unknown switch '$PSTL_TARGET_ARCH'. Accepted values: ia32, intel64"
        set PSTL_TARGET_ARCH=""
        exit 1
    endif
else
    echo "ERROR: Architecture is not defined. Accepted values: ia32, intel64"
    exit 1
endif


# Arg2 represents PSTLROOT detection method. Its possible value is 'auto_pstlroot'. In which case
# the environment variable PSTLROOT is detected automatically by using the script directory path.
if ("$2" == "auto_pstlroot") then
    set sourced=($_)
    if ("$sourced" != '') then # if the script was sourced
        set script_name=`readlink -f $sourced[2]`
    else # if the script was run => "$_" is empty
        set script_name=`readlink -f $0`
    endif
    set script_dir=`dirname $script_name`
    setenv PSTLROOT "$script_dir/.."
else
    setenv PSTLROOT "/s/intelcompilers-2018/amd64_rhel6/compilers_and_libraries_2018.1.163/linux/pstl"
endif

if ( -e $PSTLROOT/../tbb/bin/tbbvars.csh ) then
   source $PSTLROOT/../tbb/bin/tbbvars.csh $PSTL_TARGET_ARCH;
endif

if (! $?CPATH) then
    setenv CPATH "${PSTLROOT}/include"
else
    setenv CPATH "${PSTLROOT}/include:$CPATH"
endif
