#!/bin/csh
# utility script for setting environment for Jpp software.

# Set environment variables for Jpp software

if ( $?prompt != 0 ) then
    echo "# Setting environment variables for Jpp software."
endif


if ( $#argv == 1 ) then
    setenv JPP_DIR $1
else
    setenv JPP_DIR `pwd -L`
endif


if ( ! -r $JPP_DIR/setenv.csh ) then
    echo "ERROR: This script must be run with the correct Jpp directory."
else
    if ( $?ROOTSYS == 0 ) then
	echo Environment variable ROOTSYS undefined.
    endif

    source $JPP_DIR/externals/ulib/ulib.csh


    define_variable PATH
    define_variable LD_LIBRARY_PATH


    # remove history

    remove_variable  PATH            JPP_BIN
    remove_directory PATH            $JPP_DIR/externals/ulib

    remove_variable LD_LIBRARY_PATH  JPP_LIB
    remove_variable LD_LIBRARY_PATH  DATABASE_LIB
    remove_variable LD_LIBRARY_PATH  ANTARES_LIB
    remove_variable LD_LIBRARY_PATH  KM3NET_LIB
    remove_variable LD_LIBRARY_PATH  FLUX_LIB


    # set up environment

    set_variable SYSTEM              `uname -s`
    set_variable MACHINE             `uname -m`
    set_variable PROCESSOR           `uname -p`

    set_variable JPP_EXTERNALS        $JPP_DIR/externals

    set_variable DATABASE_LIB         $JPP_EXTERNALS/dbclient/lib/
    set_variable ANTARES_LIB          $JPP_EXTERNALS/antares-dataformat/
    set_variable KM3NET_DATAFORMAT    $JPP_EXTERNALS/km3net-dataformat/    
    set_variable KM3NET_LIB           $JPP_EXTERNALS/km3net-dataformat/lib/
    set_variable FLUX_LIB             $JPP_EXTERNALS/flux/
    set_variable CHSM_DIR             $JPP_EXTERNALS/chsm/

    set_variable JPP_OUT              $JPP_DIR/out/
    set_variable JPP_BIN              $JPP_OUT/$SYSTEM/bin/
    set_variable JPP_LIB              $JPP_OUT/$SYSTEM/lib/
    set_variable JPP_DOC              $JPP_DIR/doc/
    set_variable JPP_DATA             $JPP_DIR/data/
    set_variable JPP_ARCHIVE          $JPP_DIR/inputs/

    set_variable JPP_PAGES            https://common.pages.km3net.de/jpp


    insert_directory PATH             $ROOTSYS/bin
    insert_directory PATH             $JPP_BIN
    insert_directory PATH             $JPP_DIR/externals/ulib
    
    if ( `count_directory PATH \.` != 0 ) then
	remove_directory PATH  \.
	insert_directory PATH  \.
    endif
    
    if ( $?BOOST_ROOT != 0 ) then
	if ( `count_directory LD_LIBRARY_PATH $BOOST_ROOT/lib` == 0 ) then
	    insert_directory LD_LIBRARY_PATH  $BOOST_ROOT/lib
	endif
    endif

    insert_directory LD_LIBRARY_PATH  $JPP_LIB
    insert_directory LD_LIBRARY_PATH  $DATABASE_LIB
    insert_directory LD_LIBRARY_PATH  $ANTARES_LIB
    insert_directory LD_LIBRARY_PATH  $KM3NET_LIB
    insert_directory LD_LIBRARY_PATH  $FLUX_LIB
    
    insert_directory LD_LIBRARY_PATH  $ROOTSYS/lib

    set_variable ROOT_INCLUDE_PATH `egrep -lR --include \*.hh ClassDef $JPP_DIR | xargs -d '\n' dirname | sort -u | tr '\n' ':'`

    set_variable ROOT_INCLUDE_PATH    ${ROOT_INCLUDE_PATH}$JPP_DIR/software:$JPP_DIR/externals

    # Necessary to set the correct environment in containers
    set_variable JPP_PATH             $PATH
    set_variable JPP_LD_LIBRARY_PATH  $LD_LIBRARY_PATH

    # Set time axis to UTC
    set_variable TIMESTAMP utc
    
endif
