## Source /etc/profile ##

# Interactive shell check (PS1 is the way Debian scripts check and
# shopt login_shell is the way Slackware does it)
if [ "$PS1" ] || ! shopt -q login_shell; then
    if [ -r /etc/profile ]; then
        . /etc/profile
    fi

    # Source /etc/skel/.bashrc if NOT root user
    if [ -r /etc/skel/.bashrc ] && [ $UID != 0 ]; then
        . /etc/skel/.bashrc
    fi
fi

## Aliases ##

alias sudo='sudo '
alias l='ls -blhF --color=auto'
alias la='ls -balhF --color=auto'
alias grep='grep -P --color=auto'
alias ..='cd ..'
alias p='cat'
alias n='cat -n'
alias digg='dig +nocl +short +noshort'
alias emacs='emacs -nw'
alias gpg='gpg -a'
alias open='xdg-open'
alias cdm='cd $HOME/misc'
alias h='echo $HOME'
alias ip='ip -c'
if command -v netcat > /dev/null 2>&1; then
    alias nc='netcat -v -z'
elif command -v nc > /dev/null 2>&1; then
    alias nc='nc -v -z'
fi
if command -v gcal > /dev/null 2>&1; then
    alias cal='gcal'
fi
if command -v vim > /dev/null 2>&1; then
    alias vi='vim'
fi

## Exports ##

# For Perl/CPAN
if [ -d $HOME/perl5 ]; then
    export PATH="/home/daniel/perl5/bin${PATH:+:${PATH}}"
    export PERL5LIB="/home/daniel/perl5/lib/perl5${PERL5LIB:+:${PERL5LIB}}"
    export PERL_LOCAL_LIB_ROOT="/home/daniel/perl5${PERL_LOCAL_LIB_ROOT:+:${PERL_LOCAL_LIB_ROOT}}"
    export PERL_MB_OPT="--install_base \"/home/daniel/perl5\""
    export PERL_MM_OPT="INSTALL_BASE=/home/daniel/perl5"

    # For perlbrew
    if [ -r $HOME/perl5/perlbrew/etc/bashrc ]; then
        . $HOME/perl5/perlbrew/etc/bashrc
    fi
fi

# For Raku/zef
if [ -d $HOME/.raku/bin ]; then
    export PATH="$HOME/.raku/bin:$PATH"
fi

# For Ruby/rbenv
# If not installed globally, see if it's installed locally
if ! command -v rbenv > /dev/null 2>&1; then
    if [ -d $HOME/.rbenv/bin ]; then
        export PATH="$HOME/.rbenv/bin:$PATH"
    fi

    if [ -d $HOME/.rbenv/plugins/ruby-build/bin ]; then
        export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
    fi
fi
# If we have rbenv now, set up the environment
if command -v rbenv > /dev/null 2>&1; then
    eval "$(rbenv init -)"
fi

# Mine
if [ -d $HOME/bin ]; then
    export PATH="$HOME/bin:$PATH"
fi

# Editor
if command -v emacs > /dev/null 2>&1; then
    export EDITOR='emacs -nw'
fi

## Misc ##

# Prevent line wrapping mess
shopt -s checkwinsize

# Print fortune (unless the profile script exists, first test checks
# to see if STDIN is a terminal to fix scp/rsync)
if [ -t 0 ] && \
       command -v fortune > /dev/null 2>&1 && \
       ! [ -r /etc/profile.d/fortune.sh ]; then
    echo -e "\n`fortune`\n"
fi

# Bash completion for IBM Cloud
if command -v ibmcloud > /dev/null 2>&1 && \
       [ -r /usr/local/ibmcloud/autocomplete/bash_autocomplete ]; then
    . /usr/local/ibmcloud/autocomplete/bash_autocomplete
fi
