#!/usr/bin/env perl
########################################################################
#
#  Name: g5_modules_perl_wrapper
#
#  Purposes - This is a perl wrapper for the g5_modules csh script.
#
#  Notes:
#
#  1. g5_modules must be in the same directory with this script.
#
#  2. Use the following commands to "source" g5_modules from a perl script:
#     #--------------------------------
#     {
#          local @ARGV = ("$BinDir");
#          do "$BinDir/g5_modules_perl_wrapper";
#     }
#     #--------------------------------
#     where $BinDir is dir location of g5_modules and g5_modules_perl_wrapper
#
#  3. If your perl script resides in the same directory with g5_modules and
#     g5_modules_perl_wrapper, then you can "source" g5_modules with the
#     following commands:
#     #--------------------------------
#     use FindBin qw($Bin);
#     use lib "$Bin";
#     {
#          local @ARGV = ();
#          do "g5_modules_perl_wrapper";
#     }
#     #--------------------------------
#
#  REVISION HISTORY
#  17Dec2007  Stassi  Initial version of code
#  23Apr2009  Stassi  Broke scripts into subprograms 
#  04Oct2016  Stassi  Added "g5_mpw_" to front of sub names to avoid
#                     conflict with calling script
#  05Feb2018  Stassi  Get $bindir from @ARGV, if calling script does not
#                     reside is same directory as this script
########################################################################
use strict;
use warnings;

my ($scriptname, $g5_modules);
my ($BASEDIR, @libmods, $modinit, $loadmodules, @usemods);
my (@fields, $LD_LIBRARY_PATH);
my ($arch, $node);
my ($baselib, $status);
my $name = "g5_modules_perl_wrapper";

# main program
#-------------
{
    g5_mpw_init();
    g5_mpw_get_expected_values();
    g5_mpw_set_env_variables();
}

#=======================================================================
# name - init
#=======================================================================
sub g5_mpw_init {
    use FindBin qw($Bin);
    my ($bindir, $msg);

    $bindir = $ARGV[0];
    $bindir = $Bin unless $bindir and -e "$bindir/g5_modules";

    # NOTE: Spell out scriptname--DO NOT USE $0 here!
    #------------------------------------------------
    $scriptname = "g5_modules_perl_wrapper";

    # get architecture and node
    #--------------------------
    chomp($arch = `uname -s`);
    chomp($node = `uname -n`);

    # find g5_modules csh script
    #---------------------------
    $g5_modules = "$bindir/g5_modules";
    unless (-e $g5_modules) {
        $msg = "[$name] >> Error << Cannot find file: $g5_modules";
        print "$msg\n";
        die "$msg;";
    }
}

#=======================================================================
# name - g5_mpw_get_expected_values
#=======================================================================
sub g5_mpw_get_expected_values {

    # get expected values from g5_modules
    #------------------------------------
    chomp( $BASEDIR     = `$g5_modules basedir` );
    chomp( @libmods     = (`$g5_modules modules`) );
    chomp( $loadmodules = `$g5_modules loadmodules` );
    chomp( @usemods     = (`$g5_modules usemodules`) );
    chomp( $modinit     = `$g5_modules modinit` );

    @usemods = () if scalar(@usemods) == 1 and $usemods[0] eq "0";

    # convert modinit script to perl
    #-------------------------------
    @fields = split "/", $modinit;
    pop @fields;
    push @fields, "perl";
    $modinit = join "/", @fields;
}

#=======================================================================
# name - g5_mpw_set_env_variables
#=======================================================================
sub g5_mpw_set_env_variables {

    # setenv BASEDIR
    #---------------
    print "$scriptname: Setting BASEDIR";
    $ENV{"BASEDIR"} = $BASEDIR;

    # add BASEDIR lib to LD_LIBRARY_PATH, if not already there
    #---------------------------------------------------------
    $baselib = "$BASEDIR/$arch/lib";
    $LD_LIBRARY_PATH = $ENV{"LD_LIBRARY_PATH"};
    if ($LD_LIBRARY_PATH) {
        $status = index($LD_LIBRARY_PATH, $baselib);
        if ($status < 0) { $ENV{"LD_LIBRARY_PATH"} .= ":$baselib" };
    }
    else {
        $ENV{"LD_LIBRARY_PATH"} = $baselib;
    }
 
    # load library modules
    #---------------------
    if (-e $modinit) {
        print " and modules";
        do $modinit;
        module ("purge");

        if ($loadmodules) { module ("load modules") };
        if (@usemods) { foreach ( @usemods ) { module ("use -a $_") } }
        foreach ( @libmods ) { module ("load $_") };
        print " for $node\n";
    }
}
