#!/bin/csh
#
# Copyright 2013-2019 Intel Corporation.
#
# This software and the related documents are Intel copyrighted materials, and
# your use of them is governed by the express license under which they were
# provided to you (License). Unless the License provides otherwise, you may
# not use, modify, copy, publish, distribute, disclose or transmit this
# software or the related documents without Intel's prior written permission.
#
# This software and the related documents are provided as is, with no express
# or implied warranties, other than those that are expressly stated in the
# License.
#

set INST="/s/intelcompilers-2019/amd64_rhel6"

set DIR_DEBUGGER="${INST}/debugger_2019"
set DIR_DOCUMENTATION="${INST}/documentation_2019"

if ( ! -d "${INST}" ) then
	echo "installation directory not found"
	exit
endif



# Select architecture.
switch ("`uname -sm`")
case "Darwin x86_64":
	set DEBUGGERARCH="intel64"
	breaksw
case "Linux x86_64":
	set DEBUGGERARCH="intel64"
	breaksw
case "Linux *":
	set DEBUGGERARCH="ia32"
	breaksw
default:
	echo "unsupported architecture"
	unset INST DIR_DEBUGGER DIR_DOCUMENTATION
	exit
	breaksw
endsw



# Test to check if already sourced.
if (${?PATH}) then
	switch("${PATH}")
	case "${DIR_DEBUGGER}/gdb/${DEBUGGERARCH}/bin":
		unset INST DIR_DEBUGGER DIR_DOCUMENTATION DEBUGGERARCH
		exit
		breaksw
	case "${DIR_DEBUGGER}/gdb/${DEBUGGERARCH}/bin:"*:
		unset INST DIR_DEBUGGER DIR_DOCUMENTATION DEBUGGERARCH
		exit
		breaksw
	case *":${DIR_DEBUGGER}/gdb/${DEBUGGERARCH}/bin:"*:
		unset INST DIR_DEBUGGER DIR_DOCUMENTATION DEBUGGERARCH
		exit
		breaksw
	case *":${DIR_DEBUGGER}/gdb/${DEBUGGERARCH}/bin":
		unset INST DIR_DEBUGGER DIR_DOCUMENTATION DEBUGGERARCH
		exit
		breaksw
	endsw
endif



# Precedence: LC_ALL > LC_* > LANG > "en".
if (${?LC_ALL}) then
	set LANGUAGE_TERRITORY="${LC_ALL}"
else if (${?LC_MESSAGES}) then
	set LANGUAGE_TERRITORY="${LC_MESSAGES}"
else if (${?LANG}) then
	set LANGUAGE_TERRITORY="${LANG}"
else
	set LANGUAGE_TERRITORY="en"
endif



# Map to available languages.
switch("${LANGUAGE_TERRITORY}")
case "ja_*":
	set LANGUAGE_TERRITORY="jp"
	breaksw
default:
	set LANGUAGE_TERRITORY="en"
	breaksw
endsw

if (! -d "${DIR_DOCUMENTATION}/${LANGUAGE_TERRITORY}/debugger") then
    set LANGUAGE_TERRITORY="en"
endif



# Collect documentation directories.
if ( -d "${DIR_DOCUMENTATION}/${LANGUAGE_TERRITORY}/debugger/gdb-ia" ) then
	if ($?GDBINFO) then
		set GDBINFO="${DIR_DOCUMENTATION}/${LANGUAGE_TERRITORY}/debugger/gdb-ia/info/:${GDBINFO}"
	else
		set GDBINFO="${DIR_DOCUMENTATION}/${LANGUAGE_TERRITORY}/debugger/gdb-ia/info/"
	endif

	if ($?GDBMAN) then
		set GDBMAN="${DIR_DOCUMENTATION}/${LANGUAGE_TERRITORY}/debugger/gdb-ia/man/:${GDBMAN}"
	else
		set GDBMAN="${DIR_DOCUMENTATION}/${LANGUAGE_TERRITORY}/debugger/gdb-ia/man/"
	endif
endif



# Export documentation directories.
if ($?NLSPATH) then
    setenv NLSPATH "${DIR_DEBUGGER}/gdb/${DEBUGGERARCH}/share/locale/%l_%t/%N:${NLSPATH}"
else
    setenv NLSPATH "${DIR_DEBUGGER}/gdb/${DEBUGGERARCH}/share/locale/%l_%t/%N"
endif

if ($?GDBINFO) then
	if ($?INFOPATH) then
	    setenv INFOPATH "${GDBINFO}:${INFOPATH}"
	else
	    setenv INFOPATH "${GDBINFO}"
	endif
endif

if ($?GDBMAN) then
	if ($?MANPATH) then
		setenv MANPATH "${GDBMAN}:${MANPATH}"
	else if ( ! `which manpath >& /dev/null; echo $status` ) then
		setenv MANPATH "${GDBMAN}:`manpath`"
	else
		setenv MANPATH "${GDBMAN}"
	endif
endif



# Export paths.
setenv INTEL_PYTHONHOME "${DIR_DEBUGGER}/python/${DEBUGGERARCH}/"

if ($?PATH) then
    setenv PATH "${DIR_DEBUGGER}/gdb/${DEBUGGERARCH}/bin:${PATH}"
else
    setenv PATH "${DIR_DEBUGGER}/gdb/${DEBUGGERARCH}/bin"
endif

if ( "`uname -s`" != Darwin ) then
	if ( -d "${DIR_DEBUGGER}/libipt/${DEBUGGERARCH}/lib" ) then
	    if ($?LD_LIBRARY_PATH) then
		setenv LD_LIBRARY_PATH "${DIR_DEBUGGER}/libipt/${DEBUGGERARCH}/lib:${LD_LIBRARY_PATH}"
	    else
		setenv LD_LIBRARY_PATH "${DIR_DEBUGGER}/libipt/${DEBUGGERARCH}/lib"
	    endif
	endif
endif



# Cleanup.
unset INST DIR_DEBUGGER DIR_DOCUMENTATION DEBUGGERARCH LANGUAGE_TERRITORY GDBINFO GDBMAN
