# 2015-09-28 11:50 CEST


[ -f "$HOME/.profile_debug" ] && debug=1

[ -n "$debug" ] && logger "$$: .profile was triggered"
[ -n "$I_CAN_HAS_PROFILE" ] && {
    [ -n "$debug" ] && logger "$$: .profile already triggered, aborting"
    return
} || {
    [ -n "$debug" ] && logger "$$: No signs of earlier .profile trigger, so doing it now."
}


export XDG_CONFIG_HOME="$HOME/.config"
export XDG_DATA_HOME="$HOME/.local/share"
export XDG_CACHE_HOME="$HOME/.cache"
export XDG_STATE_HOME="$HOME/.local/state"

for d in "$XDG_CONFIG_HOME" "$XDG_DATA_HOME" "$XDG_STATE_HOME"; do
    mkdir -p "$d"
done

export DVDCSS_METHOD=title # in case of emergency try 'disc' or 'key'
export gnuterm=x11
export grep_colors=fn=34
export HISTCONTROL=ignoreboth
export LANG=en_US.UTF-8
export LC_COLLATE=C
export LS_COLORS="fi=0:di=34:ow=34:or=41:ex=32:bd=33:cd=33:pi=36:so=36:ln=31:*.avi=33:*.mpg=33"
export LSCOLORS=exbxgxgxcxdxdxCxCxExEx # non-GNU first order approximation
export PAGER="less -iX"
export EDITOR="nvim"
export MTR_OPTIONS=-t
export PARALLEL_HOME="${XDG_CONFIG_HOME}/parallel"
export KERAS_HOME="${XDG_STATE_HOME}/keras"
export SINGULARITY_CONFIGDIR="${XDG_CONFIG_HOME}/singularity"
export SINGULARITY_CACHEDIR="${XDG_CACHE_HOME}/singularity"
export SQLITE_HISTORY="${XDG_CACHE_HOME}/sqlite_history"


_xdg_dir() {
    k="$1"
    v="$2"
    mode="$3"

    mkdir -p "$v"
    [ -n "$mode" ] && chmod "$k" "$mode"
    eval "export XDG_$k=$v"
}


_xdg_dir DATA_HOME $HOME/.local/share
_xdg_dir CONFIG_HOME $HOME/.config
case $(uname) in
	FreeBSD)
		_xdg_dir RUNTIME_DIR /tmp/xdg-$UID
		mkdir -p "$XDG_RUNTIME_DIR"
		chmod 700 "$XDG_RUNTIME_DIR"
		;;
	*)
		_xdg_dir RUNTIME_DIR /run/user/`id -u`
		;;
esac

perl -Mlocal::lib &>/dev/null && eval $(perl -Mlocal::lib) >/dev/null

export LESS_TERMCAP_mb=$'\e[01;31m'       # begin blinking
export LESS_TERMCAP_md=$'\e[1;31m'        # begin bold
export LESS_TERMCAP_me=$'\e[0m'           # end mode
export LESS_TERMCAP_se=$'\e[0m'           # end standout-mode
export LESS_TERMCAP_so=$'\e[38;5;246m'    # begin standout-mode - info box
export LESS_TERMCAP_ue=$'\e[0m'           # end underline
export LESS_TERMCAP_us=$'\e[04;38;5;146m' # begin underline
export LESSHISTFILE="${XDG_STATE_HOME}/lesshst"

# Not a fan of the new GTK3 scroll bar style
export GTK_OVERLAY_SCROLLING=0
export HOME_AWAY_FROM_HOME="$(readlink -f \"$HOME\")"

grc="$HOME/.gtkrc-2.0"
export GTK2_RC_FILES="$grc"
[ -d "$grc" ] || {
    cat <<EOF >"$grc"
gtk-key-theme-name = "Emacs"
include "${grc}.local"
# Auto-generated by login .profile
EOF
    for gct in $(echo gconftool{,-2}); do
        which $gct &>/dev/null  \
            && $gct -t string --set /desktop/gnome/interface/gtk_key_theme Emacs
    done
}
which gsettings 2>/dev/null >/dev/null && gsettings set org.gnome.desktop.interface gtk-key-theme "Emacs"


lesskey -o ~/.less - <<EOF 2>/dev/null
j	left-scroll
k	forw-line
l	back-line
;	right-scroll
EOF


# Fix for Libreoffice missing menu bar
export SAL_USE_VCLPLUGIN=gen


umask 0022


## Managed by systemd, skip this
##if [ -n "${WAYLAND_DISPLAY}${DISPLAY}" ]; then
#    tehfile=~/.ssh/run/agent_vars.$(hostname -f)
#    #tehtimeout=900
#    tehtimeout=3600
##
#    # We can has ssh-agent?
#    if which ssh-agent >/dev/null 2>/dev/null; then
#        # ssh-agent is running?
#        if ! pgrep -x ssh-agent >/dev/null 2>/dev/null; then
#            dir=$(dirname -- "$tehfile")
#            [ -d "$dir" ] || mkdir -p -- "$dir"
#            ssh-agent -t "$tehtimeout" >"$tehfile"
#            chmod 600 "$tehfile"
#            . "$tehfile"
#        fi
#    fi
##fi
export SSH_AUTH_SOCK=$XDG_RUNTIME_DIR/ssh-agent.socket

## Userland fuckery
BOBS_LS_OPTS=""
while read o; do
    ls $(echo $o) >/dev/null 2>/dev/null || continue
    BOBS_LS_OPTS="$o"
    break
done <<EOF
--color=auto --group-directories-first
-G
EOF
export BOBS_LS_OPTS


BOBS_GREP_OPTS='--color=auto'
grep $BOBS_GREP_OPTS grep ~/.profile >/dev/null 2>/dev/null || BOBS_GREP_OPTS=''
export BOBS_GREP_OPTS



export DO_NOT_TRACK=1

# Make sure that poweroff's are purged from history
which sponge >/dev/null 2>/dev/null \
    && grep -v poweroff ~/.bash_history |sponge ~/.bash_history \
    || echo "No sponge! Install moreutils!" >&2

#which pulseaudio >/dev/null &>/dev/null \
#    && pgrep -u $(whoami) -x pulseaudio >/dev/null 2>/dev/null \
#    || pulseaudio -D

#[ -e ~/.profile.local ] && source ~/.profile.local
for f in $HOME/.profile.d/*.sh; do
    [ -f "$f" ] && . "$f"
done

export I_CAN_HAS_PROFILE=indeed
