#!/bin/csh -f
########################################################################
#
#  Name: g5_modules
#
#  Purposes -
#    * provide single location for BASEDIR and module values
#    * initialize the following:
#      - set BASEDIR
#      - update LD_LIBRARY_PATH with BASEDIR lib
#      - load library modules
#    * echo expected BASEDIR and library module values when queried
#
#  Notes:
#  1. The script will not function correctly if the scriptname
#     definition within the script is incorrect.
#  2. Type "g5_modules help" for usage information.
#
#  Perl Notes:
#  ==========
#  1. You cannot "source" this file from a perl script.
#  2. Instead use the following in your perl script:
#     #--------------------------------
#        use Env;
#        do "g5_modules_perl_wrapper";
#     #--------------------------------
#  3. Use the g5_modules_perl_wrapper file located in the build's bin
#     directory, where a copy of g5_modules also resides. This is important
#     since g5_modules_perl_wrapper uses g5_modules and expects it to be in
#     the same directory.
#
#  Bash Notes:
#  ==========
#  If you are in a bash shell, then source the g5_modules.sh script.
#  > source g5_modules.sh
#
#  REVISION HISTORY
#  31Aug2007  Stassi   Initial version of code
#  15Oct2007  Stassi   Expand list of machines and include defaults
#  15Dec2007  Stassi   Added LD_LIBRARY_PATH functionality
#  21Jul2008  Takacs   New modules and BASEDIR on discover after OS upgrade
#  13Apr2009  Stassi   Updated for pleiades
#  22Apr2010  Kokron   Updated for Fortuna-2.1 on pleiades
#  21Jul2011  Kokron   Overlay older MKL module as on discover to gain reproducible results from dgeev in GSI 
#  24Aug2012  Stassi   Added sh option to write bash source-able file
########################################################################

# NOTE: Spell out scriptname--DO NOT SET EQUAL TO $0!
#----------------------------------------------------
set scriptname = g5_modules
if ( $scriptname == "$0:t" ) then
   set sourced = 0
else
   set sourced = 1
endif

# check for improper usage
#-------------------------
if (! $sourced && ! $#argv) goto usage   # not sourced and no args
if (  $sourced &&   $#argv) goto usage   # sourced with args

# get architecture, machine, and node information
#------------------------------------------------
set arch = `uname -s`
set mach = `uname -m`
set node = `uname -n`

# Find site
# ---------

if (($node =~ discover*) || ($node =~ borg*)  || \
    ($node =~ dali*)     || ($node =~ warp*)) then

   set site = "NCCS"

else if ( ($node =~ jibb*) || ($node =~ jcc01*) ) then

   set site = "JIBB"

else if (($node =~ pfe*)  || ($node =~ p4fe*) || \
         ($node =~ r[0-9]*i[0-9]*n[0-9]*)     || \
         ($node =~ bridge*)) then
   set site = "NAS"

else if ($node =~ janus*) then
   set site = "GMAO.janus"

else if ( -d /ford1/share/gmao_SIteam/ && -d /ford1/local/ && $arch == Linux ) then
   set site = "GMAO.desktop"

endif

alias echo2  "echo \!* > /dev/stderr"

# get values
#-----------
if ($arch == Linux) then
   if ($mach == x86_64) goto X86_64
   if ($mach == i686)   goto I686
endif

# ERROR -- unknown architecture/machine
#--------------------------------------
echo2 "${scriptname}: >>>WARNING<<< unrecognizable arch/machine = $arch/$mach"
echo 0   # indicating error condition
exit 8

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#                     X86_64 values (Linux)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
X86_64:

#==================#
#  X86_64 default  #
#==================#

set modinit = DUMMY

set loadmodules = 0
set usemodules = 0

#========#
#  NCCS  #
#========#
if ( $site == NCCS ) then

   set mod1 = comp/intel-13.1.2.183

   # For MVAPICH2 (recommended)
   set basedir = /discover/swdev/mathomp4/Baselibs/TmpBaselibs/GMAO-Baselibs-4_0_5-ESMF-6_3_0rp1_beta_snapshot_07/x86_64-unknown-linux-gnu/ifort_13.1.2.183-mvapich2_1.8.1-ESMF_NetCDF
   set mod2 = other/mpi/mvapich2-1.8.1/intel-13.1.2.183

   # For IMPI 4.0 (caveat)
   # set basedir = /discover/swdev/mathomp4/Baselibs/GMAO-Baselibs-4_0_3/x86_64-unknown-linux-gnu/ifort_13.1.2.183-impi_4.0.3.008
   # set mod2 = mpi/impi-4.0.3.008

   set mod3 = lib/mkl-13.0.1.117
   set mod4 = other/comp/gcc-4.6.3-sp1
   set mod5 = other/SIVO-PyD/spd_1.10.0_gcc-4.6.3-sp1

   set mods = ( $mod1 $mod2 $mod3 $mod4 $mod5 )
   set modinit = /usr/share/modules/init/csh
   set loadmodules = 0

#========#
#  JIBB  #
#========#
else if ( $site == JIBB ) then

   set basedir = /jibb/nobackup/gmaosi/Baselibs/GMAO-Baselibs-4_0_3/x86_64-unknown-linux-gnu/ifort_13.1.3.192-mvapich2_1.8.1
   set mod1 = comp/intel-13.1.3.192
   set mod2 = other/mpi/mvapich2-1.8.1/intel-13.1.3.192
   set mod3 = other/comp/gcc-4.6.3
   #set mod4 = PYTHON GOES HERE

   #set mods = ( $mod1 $mod2 $mod3 $mod4 )
   set mods = ( $mod1 $mod2 $mod3 )
   set modinit = /usr/share/modules/init/tcsh
   set loadmodules = 0

#=======#
#  NAS  #
#=======#
else if ( $site == NAS ) then

   set basedir = /nobackup/gmao_SIteam/Baselibs/GMAO-Baselibs-4_0_3/x86_64-unknown-linux-gnu/ifort_2013.5.192-mpt_2.06rp16
   set mod1 = comp-intel/2013.5.192
   set mod2 = mpi-sgi/mpt.2.06rp16
   set mod3 = x11-libs/32bit
   set mod4 = imagemagick/6.4.0-3
   set mod5 = python/2.7.3

   set mods = ( $mod1 $mod2 $mod3 $mod4 $mod5 )
   set modinit = /usr/share/modules/init/tcsh
   set loadmodules = 0
   set usemodules = 1

#=========#
#  JANUS  #
#=========#
else if ( $site == GMAO.janus ) then
   set basedir=/ford1/share/gmao_SIteam/Baselibs/GMAO-Baselibs-4_0_4/x86_64-unknown-linux-gnu/pgfortran_13.10.0-openmpi_1.7.3
   set mod1 = comp/pgi/13.10.0
   set mod2 = mpi/openmpi/1.7.3-pgi-13.10.0
   set mod3 = other/python/canopy-1.2.0.1610.rh5-x86-64

   set mods = ( $mod1 $mod2 $mod3 )
   set modinit = /usr/share/Modules/init/tcsh
   set loadmodules = 0
   set usemodules = 1

#=================#
#  GMAO DESKTOP   #
#=================#
else if ( $site == GMAO.desktop ) then

   set basedir=/ford1/share/gmao_SIteam/Baselibs/GMAO-Baselibs-4_0_4/x86_64-unknown-linux-gnu/ifort_13.1.1.163-openmpi_1.6.5-canopy
   set mod1 = comp/intel/13.1.1.163
   set mod2 = mpi/openmpi/1.6.5-intel-13.1.1.163
   set mod3 = other/python/canopy-1.2.0.1610.rh5-x86-64

   set mods = ( $mod1 $mod2 $mod3 )
   set modinit = /usr/share/Modules/init/tcsh
   set loadmodules = 0
   set usemodules = 1

endif

goto ACTION

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#                      I686 values (Linux)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
I686:

#================#
#  I686 default  #
#================#
set basedir = $HOME/baselibs/v2_2_0rp2
set modinit = DUMMY

#===========================#
#  LAKE (Atanas's machine)  #
#===========================#
if ($node == lake) then
   set basedir = /home/trayanov/baselibs/v3_1_0rp1

#============#
#  CALCULON  #
#============#
else if ($node == calculon) then
   set basedir = /home/dasilva/baselibs/v2_2rp2_nb2

#===========#
#  CEREBUS  #
#===========#
else if ($node == cerebus) then
   set basedir = /home/trayanov/baselibs/v2_2_0rp2

endif
goto ACTION

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#              perform requested action
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
ACTION:

#===================#
#   ECHO VALUE(s)   #
#===================#
set wrapper = 0
if ( $#argv > 0 ) then

   if ( $1 == basedir ) then
      echo $basedir

   else if ( $1 == modules ) then
      echo $mods

   else if ( $1 == modinit ) then
      echo $modinit

   else if ( $1 == loadmodules ) then
      echo $loadmodules

   else if ( $1 == usemodules ) then
      echo $usemodules

   else if ( $1 == ESMA_FC ) then
      echo $ESMA_FC

   else if ( $1 == site ) then
      echo $site

   else if ( $1 == sh ) then
      set wrapper = 1
      goto SETVALUES

   else if ( $1 == help ) then
      goto usage

   else
      echo2 "${scriptname}: >>>ERROR<<< unrecognizable option -- $1"
      echo2 Type "$scriptname help" for usage information.
      echo 0   # indicating error condition
      exit 9
   endif

   exit
endif

SETVALUES:
#================#
#   SET VALUES   #
#================#
#-------------------------------------------------------------------------
# set environment variables, BASEDIR and LD_LIBRARY_PATH; and load modules
#-------------------------------------------------------------------------

# setenv BASEDIR
#---------------
if ($?basedir) then
   if (! $wrapper) echo -n "${scriptname}: Setting BASEDIR"
   setenv BASEDIR $basedir
else if ($?BASEDIR) then
   if (! $wrapper) echo -n "${scriptname}: BASEDIR found in environment"
else
   echo
   echo "BASEDIR not found in environment or set by ${scriptname}"
   exit 3
endif

# add BASEDIR lib to LD_LIBRARY_PATH, if not already there
#---------------------------------------------------------
if ($?LD_LIBRARY_PATH) then
   echo $LD_LIBRARY_PATH | grep $BASEDIR/$arch/lib > /dev/null
   if ($status) then  #  == 1, if not found
      setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:$BASEDIR/$arch/lib
   endif
else
   setenv LD_LIBRARY_PATH $BASEDIR/$arch/lib
endif

# add individual $ld_libraries to LD_LIBRARY_PATH, if not already there
#----------------------------------------------------------------------
if ($?ld_libraries) then
   foreach lib ( $ld_libraries )
      if ($LD_LIBRARY_PATH !~ *$lib*) then
         setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:$lib
      endif
   end
endif

if ($?LD_LIBRARY64_PATH) then
   echo $LD_LIBRARY64_PATH | grep $BASEDIR/$arch/lib > /dev/null
   if ($status) then  #  == 1, if not found
      setenv LD_LIBRARY64_PATH ${LD_LIBRARY64_PATH}:$BASEDIR/$arch/lib
   endif
endif

# load library modules
#---------------------
if (-e $modinit) then

   if (! $wrapper) echo -n " and modules"
   source $modinit
   module purge

   if ($loadmodules) module load modules

   if ($usemodules) then
      if ($site == NAS) then
         module use -a /u/scicon/tools/modulefiles
         module use -a /nobackup/gmao_SIteam/modulefiles
      endif

      if ($site =~ "GMAO.*" ) then
         module use -a /ford1/share/gmao_SIteam/modulefiles
      endif
   endif

   foreach mod ( $mods )
      module load $mod
   end

endif
if (! $wrapper) echo " for $node"

# write sh commands to a .g5_modules.sh file
#-------------------------------------------
if ($wrapper) then
   set outfil = ".g5_modules.sh"

   if ($?BASEDIR) then
      echo "export BASEDIR=$BASEDIR"                     >! $outfil
   endif
   if ($?LD_LIBRARY_PATH) then
      echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH"     >> $outfil
   endif
   if ($?LD_LIBRARY64_PATH) then
      echo "export LD_LIBRARY64_PATH=$LD_LIBRARY64_PATH" >> $outfil
   endif

   set modulecmd = `which modulecmd`
   if ($status) set modulecmd = "/usr/bin/modulecmd"
   set modinit_sh = `echo $modinit | sed "s|/csh|/sh|"`

   if (-e $modulecmd) then
      if (-e $modinit_sh) then
         echo 'source '$modinit_sh                       >> $outfil
      endif
      echo 'eval `'$modulecmd sh purge'`'                >> $outfil
      if ($loadmodules) then
         echo 'eval `'$modulecmd sh load modules'`'      >> $outfil
      endif
      foreach mod ($mods)
         echo 'eval `'$modulecmd sh load $mod'`'         >> $outfil
      end
   endif

endif
exit

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#                    usage information                    #
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
usage:

more <<EOF

NAME
     $scriptname - Script to handle BASEDIR and library module definitions

DESCRIPTION

     This script provides a single location for storing the BASEDIR and
     library module name information.  Previous to this script, this
     information was coded in multiple files.

     This script will set the BASEDIR environment variable to its proper value,
     add the BASEDIR lib directory to LD_LIBRARY_PATH (if necessary), and will
     load library modules when sourced.

     If the script is called with "basedir", "modules", "modinit", or 
     "loadmodules", then it will echo the values to standard output without
     modifying the environment.

     The "modinit" and "loadmodules" options are primarily for use with
     the g5_modules_perl_wrapper script.

SYNOPSIS

     source $scriptname

     or

     $scriptname <option>

OPTIONS

     help                echo usage
     basedir             echo expected value for BASEDIR environment variable
     modules             echo expected list of modules
     modinit             echo location of csh module initialization script
     loadmodules         echo logical indicating whether "module load modules"
                              is needed prior to loading other modules
     usemodules          echo logical indicating whether "module use directory(s)"
                              is needed prior to loading other modules
     ESMA_FC             echo value of ESMA_FC if set
     site                echo site as detected by $scriptname
     sh                  write sh version of g5_modules to file named, ".g5_modules.sh"

EOF
exit 1
