#!/bin/bash
#
# This wrapper try to remove system's unmodified environment variables
# and propagate user environments on the nodes.
#
# This script is licence under the same licence as OAR program
#  GNU GENERAL PUBLIC LICENSE
#  http://oar.imag.fr/
#
# 1.0 Nicolas Capit  (initial version for PATH and LD_LIBRARY_PATH)
# 1.1 Patrick Begou  (all the environment excepted... that could make problems!)
# 1.2 Gabriel Moreau (format, small bug and licence)
# 1.3 Gabriel Moreau (add initial support for ulimit)
#

HOST=$1
shift

MYLIMIT=""
for option in 's' 'l' 'n'
do
   myvalue=$(ulimit "-$option")
   [ -n "$myvalue" ] && MYLIMIT="${MYLIMIT} ulimit -$option $myvalue;"
done

MYENV=$( env \
   | grep "^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]" \
   | egrep -v "^USER=|^TERM=|^OAR|^SGE_|^LS_COLORS=\
      |^ENV=|^BASH_ENV=|^HOSTNAME=|^LOGNAME=|^MAIL=\
      |^MANPATH=|^OMPI_MCA_plm_rsh_agent=|^PWD=\
      |^SHELL=|^SSH_|^SUDO_COMMAND=|^HOME=|^DISPLAY=\
      |^SHLVL=" \
   | sort \
   | sed -e 's/^/export /; s/=/="/; s/$/"/' \
   | tr [\\\n] [\;] )

exec /opt/oar/current/bin/oarsh $HOST "$MYLIMIT $MYENV $@"

exit

################################################################

Documentation in Perl POD format (man perlpod)

=head1 NAME

 oar-envsh - oarsh with env variable transmit

=head1 SYNOPSIS

 oar-envsh node command

=head1 DESCRIPTION

C<oar-envsh> need to be executed inside an OAR cluster.
It's a simple wrapper around C<oarsh> command
which is a wrapper around C<ssh> command!

With C<oar-envsh>, almost all env variable are export
in the new shell except the following:

 USER TERM OAR* SGE_* LS_COLORS
 ENV BASH_ENV HOSTNAME LOGNAME MAIL
 MANPATH OMPI_MCA_plm_rsh_agent PWD
 SHELL SSH_* SUDO_COMMAND= HOME DISPLAY
 SHLVL

With C<oar-envsh>, some ulimit are also transfert in the new shell.
Actually, we only transfert:

 -l locked memory
 -n open files
 -s stack size

Alway use C<oarsh>,
only use C<oar-envsh> if really needed!


=head1 SEE ALSO

oarsh


=head1 AUTHORS

Written by :

 Nicolas Capit, Grenoble - France
 Patrick Begou - Gabriel Moreau, Grenoble - France


=head1 LICENSE AND COPYRIGHT

GPL version 2 or later

Copyright (C) 2011-2014 Patrick Begou / LEGI - CNRS UMR 5519 - France

=cut
