#!/bin/tcsh
#===============================================================================
# Copyright 2003-2016 Intel Corporation All Rights Reserved.
#
# 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.
#===============================================================================

set __mkl_tmp_cpro_path=/s/intelcompilers-2017/amd64_rhel6/compilers_and_libraries_2017.0.098/linux
setenv MKLROOT ${__mkl_tmp_cpro_path}/mkl

set __mkl_tmp_script_name="mklvars.csh"
set __mkl_tmp_mod_name="mod"

set __mkl_tmp_target_arch=""
set __mkl_tmp_lp64_ilp64=""
set __mkl_tmp_mod=""
set __mkl_tmp_mklvars_verbose=""
set __mkl_tmp_mic_arch=""

if ( $#argv == 0 ) then
    if ( $?COMPILERVARS_ARCHITECTURE ) then
        set __mkl_tmp_target_arch="$COMPILERVARS_ARCHITECTURE"
    endif
    if ( $?MKLVARS_ARCHITECTURE ) then
        set __mkl_tmp_target_arch="$MKLVARS_ARCHITECTURE"
    endif
    if ( $?MKLVARS_INTERFACE ) then
        set __mkl_tmp_lp64_ilp64="$MKLVARS_INTERFACE"
    endif
    if ( $?MKLVARS_MOD ) then
        set __mkl_tmp_mod="$MKLVARS_MOD"
    endif
    if ( $?MKLVARS_VERBOSE ) then
        set __mkl_tmp_mklvars_verbose="$MKLVARS_VERBOSE"
    endif
else
    while ( "$1" != "" )
        if ( "$1" == "ia32" || "$1" == "intel64" || "$1" == "mic" ) then
            set __mkl_tmp_target_arch="$1"
        else if ( "$1" == "lp64" || "$1" == "ilp64" ) then
            set __mkl_tmp_lp64_ilp64="$1"
        else if ( "$1" == ${__mkl_tmp_mod_name} ) then
            set __mkl_tmp_mod="${__mkl_tmp_mod_name}"
        else if ( "$1" == "verbose"   ) then
            set __mkl_tmp_mklvars_verbose="verbose"
        else
            echo
            echo "ERROR: Unknown option '$1'"
            goto Help
        endif
        shift
    end
endif

if ( "${__mkl_tmp_target_arch}" == "intel64" ) then
    set __mkl_tmp_target_arch="intel64"
    set __mkl_tmp_mic_arch="mic"
else if ( "${__mkl_tmp_target_arch}" == "mic" ) then
    set __mkl_tmp_target_arch="mic"
    set __mkl_tmp_mic_arch="mic"
else if ( "${__mkl_tmp_target_arch}" != "ia32" ) then
   echo
   echo "ERROR: architecture is not defined. Accepted values: ia32, intel64, mic"
   goto Help
endif

set __mkl_ld_arch="${__mkl_tmp_cpro_path}/compiler/lib/${__mkl_tmp_target_arch}:${MKLROOT}/lib/${__mkl_tmp_target_arch}"
set __mkl_ld_intel64="${__mkl_tmp_cpro_path}/compiler/lib/intel64:${MKLROOT}/lib/intel64"
set __mkl_ld_mic=""

if ( -d "${__mkl_tmp_cpro_path}/compiler/lib/${__mkl_tmp_mic_arch}" ) then
  set __mkl_ld_mic="${__mkl_tmp_cpro_path}/compiler/lib/${__mkl_tmp_mic_arch}"
endif

if ( -d "${MKLROOT}/lib/${__mkl_tmp_mic_arch}" ) then
  set __mkl_ld_mic="${__mkl_ld_mic}:${MKLROOT}/lib/${__mkl_tmp_mic_arch}"
endif

if ( ! ${?TBBROOT} ) then
    set __mkl_tmp_tbb_subdir="/gcc4.4"
    if ( "${__mkl_tmp_target_arch}" == "mic" ) then
        set __mkl_tmp_tbb_subdir=""
    endif

    set __mkl_ld_arch="${__mkl_tmp_cpro_path}/tbb/lib/${__mkl_tmp_target_arch}${__mkl_tmp_tbb_subdir}:${__mkl_ld_arch}"
    set __mkl_ld_intel64="${__mkl_tmp_cpro_path}/tbb/lib/intel64/gcc4.4:${__mkl_ld_intel64}"
    set __mkl_ld_mic="${__mkl_tmp_cpro_path}/tbb/lib/${__mkl_tmp_mic_arch}:${__mkl_ld_mic}"
endif

if ( ${?LD_LIBRARY_PATH} ) then
    setenv LD_LIBRARY_PATH "${__mkl_ld_arch}:${LD_LIBRARY_PATH}"
else
    setenv LD_LIBRARY_PATH "${__mkl_ld_arch}"
endif
if ( -d "/opt/intel/mic" ) then
if ( "${__mkl_tmp_mic_arch}" != "" ) then
    setenv LD_LIBRARY_PATH "/opt/intel/mic/coi/host-linux-release/lib:/opt/intel/mic/myo/lib:${LD_LIBRARY_PATH}"
endif
endif
if ( "${__mkl_tmp_target_arch}" == "mic" ) then
    setenv LD_LIBRARY_PATH "${__mkl_ld_intel64}:${LD_LIBRARY_PATH}"
endif

if ( ${?LIBRARY_PATH} ) then
    if ( "${__mkl_tmp_target_arch}" == "mic" ) then
        setenv LIBRARY_PATH "${__mkl_ld_intel64}:${LIBRARY_PATH}"
    else
        setenv LIBRARY_PATH "${__mkl_ld_arch}:${LIBRARY_PATH}"
    endif
else
    if ( "${__mkl_tmp_target_arch}" == "mic" ) then
        setenv LIBRARY_PATH "${__mkl_ld_intel64}"
    else
        setenv LIBRARY_PATH "${__mkl_ld_arch}"
    endif
endif

if ( "${__mkl_tmp_mic_arch}" != "" ) then
    if ( ${?MIC_LD_LIBRARY_PATH} ) then
        setenv MIC_LD_LIBRARY_PATH "${__mkl_ld_mic}:${MIC_LD_LIBRARY_PATH}"
    else
        setenv MIC_LD_LIBRARY_PATH "${__mkl_ld_mic}"
    endif
    if ( -d "/opt/intel/mic" ) then
        setenv MIC_LD_LIBRARY_PATH "${__mkl_ld_mic}:/opt/intel/mic/coi/device-linux-release/lib:/opt/intel/mic/myo/lib:${MIC_LD_LIBRARY_PATH}"
    endif
    if ( ${?MIC_LIBRARY_PATH} ) then
        setenv MIC_LIBRARY_PATH "${__mkl_ld_mic}:${MIC_LIBRARY_PATH}"
    else
        setenv MIC_LIBRARY_PATH "${__mkl_ld_mic}"
    endif
endif

if ( ${?NLSPATH} ) then
    setenv NLSPATH "${MKLROOT}/lib/${__mkl_tmp_target_arch}/locale/%l_%t/%N:${NLSPATH}"
else
    setenv NLSPATH "${MKLROOT}/lib/${__mkl_tmp_target_arch}/locale/%l_%t/%N"
endif
if ( "${__mkl_tmp_target_arch}" == "mic" ) then
    setenv NLSPATH "${MKLROOT}/lib/intel64/locale/%l_%t/%N:${NLSPATH}"
endif

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

if ( "${__mkl_tmp_mod}" == "${__mkl_tmp_mod_name}" )  then
    if ( "${__mkl_tmp_target_arch}" == "ia32" ) then
        set __mkl_tmp_lp64_ilp64=""
    else
        if ( "$__mkl_tmp_lp64_ilp64" == "" ) then
            set __mkl_tmp_lp64_ilp64="lp64"
        endif
    endif
    setenv CPATH "${CPATH}:${MKLROOT}/include/${__mkl_tmp_target_arch}/${__mkl_tmp_lp64_ilp64}"
    if ( "${__mkl_tmp_target_arch}" == "mic" ) then
        setenv CPATH "${CPATH}:${MKLROOT}/include/intel64/${__mkl_tmp_lp64_ilp64}"
    endif
endif

if ( "${__mkl_tmp_mklvars_verbose}" == "verbose" ) then
    echo LD_LIBRARY_PATH=${LD_LIBRARY_PATH}
    echo LIBRARY_PATH=${LIBRARY_PATH}
    echo MIC_LD_LIBRARY_PATH=${MIC_LD_LIBRARY_PATH}
    echo NLSPATH=${NLSPATH}
    echo CPATH=${CPATH}
endif

goto End

Help:
    echo ""
    echo "Syntax:"
    echo "  source $__mkl_tmp_script_name <arch> [MKL_interface] [${__mkl_tmp_mod_name}]"
    echo ""
    echo "   <arch> must be one of the following"
    echo "       ia32         : Setup for IA-32 architecture"
    echo "       intel64      : Setup for Intel(R) 64 architecture"
    echo "       mic          : Setup for Intel(R) Many Integrated Core Architecture"
    echo ""
    echo "   ${__mkl_tmp_mod_name} (optional) - set path to MKL F95 modules"
    echo ""
    echo "   MKL_interface (optional) - MKL programming interface for intel64"
    echo "                              Not applicable without ${__mkl_tmp_mod_name}"
    echo "       lp64         : 4 bytes integer (default)"
    echo "       ilp64        : 8 bytes integer"
    echo ""
    echo "If the arguments to the sourced script are ignored (consult docs for"
    echo "your shell) the alternative way to specify target is environment"
    echo "variables COMPILERVARS_ARCHITECTURE or MKLVARS_ARCHITECTURE to pass"
    echo "<arch> to the script, MKLVARS_INTERFACE to pass <MKL_interface> and"
    echo "MKLVARS_MOD to pass <${__mkl_tmp_mod_name}>"
    echo ""
    exit 1;

End: # Clean up of internal settings
    unset __mkl_tmp_cpro_path
    unset __mkl_tmp_script_name
    unset __mkl_tmp_mod_name
    unset __mkl_tmp_target_arch
    unset __mkl_tmp_lp64_ilp64
    unset __mkl_tmp_mod
    unset __mkl_tmp_mklvars_verbose
    unset __mkl_tmp_mic_arch
    unset __mkl_tmp_tbb_subdir
    unset __mkl_ld_intel64
    unset __mkl_ld_arch
    unset __mkl_ld_mic
