# Title: .bash_profile
# Author: Frédéric CHEVALIER <f15.chevalier@gmail.com>
# Version: 1.9
# Created in: 2012-05-25
# Modified in: 2015-04-20


#==========#
# Versions #
#==========#

# v1.9 - 2015-04-20: Python env variable added / alias updated
# v1.8 - 2014-12-02: proxy-display added (resolve $DISPLAY problem with screen)
# v1.7 - 2014-10-22: alias added / conditional modification of PATH and Ld_LIBRARY_PATH to be able to use eog and evince on other server (crow, ...)
# v1.6 - 2014-10-15: alias added / LC* added / LS_COLORS removed / PATH updated / LD_LIBRARY_PATH updated
# v1.5 - 2014-09-30: improve screen session detection to print welcome message at server connection even if screen session is running elsewhere
# v1.4 - 2014-08-14: adapt behavior of the start screen regarding the screen program
# v1.3 - 2014-08-06: part for SSH environment setup moved from .bashrc to avoid setting up SSH environment when connection to a node (qrsh)
# v1.2 - 2014-07-09: behaviour modified when login to a node using qrsh (qrsh alias modified / if loop correction for message)
# v1.1 - 2014-07-01: specific aliases needed for GridEngine added
# v1.0 - 2014-06-28: rewriting for better interplay with .bashrc
# v0.0 - 2012-05-25: creation



#===============================#
# Get the aliases and functions #
#===============================#

if [[ -f ~/.bashrc ]]
then
	source ~/.bashrc
fi



#==================#
# Specific aliases #
#==================#

alias pqsub='qsub -V -cwd -o $HOME/status -j y -r y -S /bin/bash'
alias pqsubR='qsub -V -cwd -o $HOME/status -j y -r y -S $(which Rscript)'
alias pqrsh='qrsh -V -cwd -pty y bash -li'
alias plocate='locate -d "$HOME/.mlocate.db"'
alias xterm='xterm -bg black -fg white -rightbar -geometry 200x35'

alias medusa='ssh -Y medusa'
alias medusa2='ssh -Y medusa2'
alias mendel='ssh -Y mendel'
alias crow='ssh -Y crow'
alias bulmer='ssh -Y bulmer'

alias model_script_sh='cp $HOME/Templates/script.sh $1'



#================================================#
# User specific environment and startup programs #
#================================================#

#--------#
# Locale #
#--------#

# Get with locale command
export LANG=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8
export LC_TIME=en_US.UTF-8      # Fix timestamp to english format (french connection problem)
export LC_NUMERIC=en_US.UTF-8   # To always have . as decimal separator
export LC_NAME=en_US.UTF-8
LC_ALL=


#-------#
# Paths #
#-------#

# Path variable
export PATH=:$HOME/local/bin:$HOME/local/share:$HOME/local/usr/local/bin:$PATH     # Personal path

# Perl variables
export PERL5LIB=~/local/lib/perl5/site_perl/5.18.2/x86_64-linux/auto:~/local/lib/perl5/5.18.2:~/local/lib/perl5/site_perl:~/local/lib/perl5/site_perl/5.18.2:~/local/lib/perl5/site_perl/5.18.2/x86_64-linux:~/local/lib/perl5/5.18.2/x86_64-linux:/usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi:/usr/lib/perl5/site_perl/5.8.8:/usr/lib/perl5/site_perl:/usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi:/usr/lib/perl5/vendor_perl/5.8.8:/usr/lib/perl5/vendor_perl:/usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi:/usr/lib/perl5/5.8.8

# Python
export PYTHONPATH=$HOME/local/lib/python2.6/site-packages
export PYTHONIOENCODING=utf-8

# Special paths for medusa and nodes
if [[ $HOSTNAME =~ medusa* || $HOSTNAME =~ compute.* ]]
then
    # Path variable
    export PATH=$HOME/local/bin:$HOME/local/share:$HOME/local/usr/local/bin     # Personal path
    export PATH=$PATH:/opt/openmpi/bin:/opt/bio/ncbi/bin:/opt/bio/mpiblast/bin:/opt/bio/hmmer/bin:/opt/bio/EMBOSS/bin:/opt/bio/clustalw/bin:/opt/bio/tcoffee/bin:/opt/bio/phylip/exe:/opt/bio/mrbayes:/opt/bio/fasta:/opt/bio/glimmer/bin:/opt/bio/glimmer/scripts:/opt/bio/gromacs/bin:/opt/bio/gmap/bin:/opt/bio/tigr/bin:/opt/bio/autodocksuite/bin:/opt/bio/wgs/bin:/opt/eclipse:/opt/ganglia/bin:/opt/ganglia/sbin:/opt/rocks/bin:/opt/rocks/sbin:/opt/condor/bin:/opt/condor/sbin:/opt/gridengine/bin/lx26-amd64:/usr/bin:/usr/local/bin:/bin:/usr/java/latest/bin # System path
    export PATH=$PATH:/opt/gridengine/bin/linux-x64 # System path needed for the new medusa

    # Location of bamtools lib (source: http://www.vcru.wisc.edu/simonlab/bioinformatics/programs/install/bamtools.htm)
    export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOME/local/lib" #:$HOME/local/lib64" #Otherwise pb with heron  #:$LD_LIBRARY_PATH"
fi


#----------#
# Programs #
#----------#

# Vim
export VIMHOME=$HOME/.vim

# R
R_version=$(R --version | head -1 | cut -d " " -f 3)
export R_LIBS_HOME="$HOME/.R-libs"

if [[ ! -d "${R_LIBS_HOME}/${R_version}" ]]
then
    mkdir "${R_LIBS_HOME}/${R_version}"
fi

export R_LIBS="${R_LIBS_HOME}/${R_version}"

# SSH environment setup
if [[ ! $HOSTNAME =~ compute.* ]]
then
    if [[ -n "$SSH_CONNECTION" ]]
    then
        # Source SSH settings, if applicable
        if [[ -f "${SSH_ENV}" ]]
        then
            source "${SSH_ENV}" > /dev/null
            #ps ${SSH_AGENT_PID} doesn't work under cywgin
            ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || start_agent
        else    
            start_agent
        fi
    fi
fi

# Display forwarding
# dependency: proxy-dsiplay
if [[ ! ($HOSTNAME =~ medusa* || $HOSTNAME =~ compute.*) && -n "$DISPLAY" && $(which proxy-display &> /dev/null; echo $?) == 0 ]]
then
    proxy-display :99
fi



#=================#
# Welcome message #
#=================#

name=${HOSTNAME%%.*}
nb=$(printf "%${#name}s\n" "" | sed "s/ /=/g")

if [[ ! $HOSTNAME =~ compute.* &&  -z $STY ]]
then
    nb=$nb=======
    echo -e "\n\n"
    cat .welcome.msg | sed "s/HOSTNAME/$(echo $name Server | sed "s/^[m,h]/\U&/")/" | sed "s/=\*/$nb/g"
    echo -e "\n\n"


    if [[ $(echo $name) == "medusa" || $(echo $name) == "medusa2" ]]
    then
        echo -e "\033[33mSpace available on /master [%/Total/free]:\033[00m $(df -h | grep "/master " | awk '{print $5" / "$2" / "$4}')\n"
        echo -e "\033[33mCores available:\033[00m\n"
        qstat -g c
        echo -e "\n"
    fi
fi

