#!/bin/bash
# 
# SCRIPT NAME:
# .env
#
# AUTHOR:
# Severin Kaderli <severin@kaderli.dev>
#
# DESCRIPTION:
# This file contains declarations of environment variables.
#
# USAGE:
# This script is sourced by .bashrc
# XDG_Base_Directories
if [ -f "${XDG_BIN_HOME}/utils" ]; then
    . "${XDG_BIN_HOME}/utils"
fi

export XDG_CONFIG_HOME="${HOME}/.config"
export XDG_CACHE_HOME="${HOME}/.cache"
export XDG_DATA_HOME="${HOME}/.local/share"
export XDG_BIN_HOME="${HOME}/.local/bin"
export XDG_LOG_HOME="${HOME}/.local/log"
export DOTFILES="${HOME}/dotfiles"

export HOST="$(hostname)"
if [ "${HOST}" = "odin" ]; then
    export IS_WORK="1"
else
    export IS_WORK="0"
fi

# XDG Overrides for unsupported programs 
export HTTPIE_CONFIG_DIR="${XDG_CONFIG_HOME}/httpie"
export WGETRC="${XDG_CONFIG_HOME}/wget/wgetrc"
export XAUTHORITY="${HOME}/.Xauthority"
export VIMINIT=":source ${XDG_CONFIG_HOME}/vim/vimrc"
export CARGO_HOME="${XDG_DATA_HOME}/cargo"
export RUSTUP_HOME="${XDG_DATA_HOME}/rustup"
export PYLINTHOME="${XDG_CACHE_HOME}/pylint"
export DOCKER_CONFIG="${XDG_CONFIG_HOME}/docker"
export GTK_RC_FILES="${XDG_CONFIG_HOME}/gtk-1.0/gtkrc"
export GTK2_RC_FILES="${XDG_CONFIG_HOME}/gtk-2.0/gtkrc"
export XINITRC="${XDG_CONFIG_HOME}/X11/xinitrc"
export XSERVERRC="${XDG_CONFIG_HOME}/X11/xserverrc"
export NPM_CONFIG_USERCONFIG="${XDG_CONFIG_HOME}/npm/config"
export NODE_REPL_HISTORY="${XDG_DATA_HOME}/node/repl_history"
export GOPATH="${XDG_DATA_HOME}/go"
export GNUPGHOME="${XDG_DATA_HOME}/gnupg"
export GEM_HOME="${XDG_DATA_HOME}/gem"
export GEM_SPEC_CACHE="${XDG_CACHE_HOME}/gem/specs"
mkdir -p "${XDG_DATA_HOME}/wine/prefixes"
export WINEPREFIX="${XDG_DATA_HOME}/wine/prefixes/default"
export SQLITE_HISTORY="${XDG_DATA_HOME}/sqlite_history"
export TASKDATA="${HOME}/Tasks"
export TASKRC="${XDG_CONFIG_HOME}/task/taskrc"

# Python
mkdir -p "${XDG_DATA_HOME}/python"
export PYTHONSTARTUP="${XDG_CONFIG_HOME}/python/startup.py"

# zsh
export ZDOTDIR="${XDG_CONFIG_HOME}/zsh"
export HISTFILE="${XDG_DATA_HOME}/zsh/history"
export HISTCONTROL=ignoreboth
export SAVEHIST=1000
export HISTSIZE=1000
export HISTFILESIZE=2000
mkdir -p "${XDG_DATA_HOME}/zsh/zcompdump-${ZSH_VERSION}"

if [ -n "$(command -v compinit)" ]; then
    compinit -d "${XDG_DATA_HOME}/zsh/zcompdump-${ZSH_VERSION}"
fi

# Setting applications
export VISUAL=vim
export EDITOR="${VISUAL}"
export TERMINAL="termite"
export BROWSER="google-chrome-beta"
export PAGER="less"
export MANPAGER="${PAGER}"

# QT
export QT_QPA_PLATFORMTHEME=gtk2
export QT_STYLE_OVERRIDE=gtk2

# Ibus
export GTK_IM_MODULE=xim
export XMODIFIERS=@im=ibus
export QT_IM_MODULE=xim

# Adding folders to the PATH variable
export PATH="${PATH}:${HOME}/.local/bin"
export PATH="${PATH}:${CARGO_HOME}/bin"
export PATH="${PATH}:${XDG_DATA_HOME}/npm/bin"
export PATH="${PATH}:${GOPATH}/bin"
export PATH="${PATH}:${GEM_HOME}/bin"
export PATH="${PATH}:${XDG_CONFIG_HOME}/composer/vendor/bin"

# Load version control information
function current_branch() {
    branch="$(git rev-parse --abbrev-ref HEAD)" > /dev/null 2>&1
    if [ -z "${branch}" ]
    then
        echo ""
    else
        echo "[${branch}]"
    fi
}

function file_count() {
    count="$(ls -a -1 | wc -l)"
    echo "[${count} files]"
}

function folder_size() {
    size="$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')"
    echo "[${size}B]"
}

function prompt_width() {
    hostname="$(hostname)"
    branch="$(current_branch)"
    pwd="$(print -P %~)"
    file_count="$(file_count)"
    folder_size="$(folder_size)"
    echo "$((COLUMNS - 19 - ${#hostname} - ${#branch} - ${#pwd} - ${#file_count} - ${#folder_size}))"
}

function precmd() {
    # Print a newline before the prompt, unless it's the
    # first prompt in the process.
    if [ -z "$NEW_LINE_BEFORE_PROMPT" ]; then
        NEW_LINE_BEFORE_PROMPT=1
    elif [ "$NEW_LINE_BEFORE_PROMPT" -eq 1 ]; then
        echo ""
    fi
}

# Custom prompt
NEWLINE=$'\n'
export PS1="┌─"
export PS1="${PS1}%F{red}[%*]%f"
export PS1="${PS1}%F{blue}[%m]%f"
export PS1="${PS1}%F{yellow}[%~]%f"
export PS1="${PS1}"'${(r:$(prompt_width)::─:)}'
export PS1="${PS1}%F{yellow}"'$(file_count)'"%f"
export PS1="${PS1}%F{blue}"'$(folder_size)'"%f"
export PS1="${PS1}%F{red}"'$(current_branch)'"%f"
export PS1="${PS1}─┐"
export PS1="${PS1}${NEWLINE}└─╼ "
export RPROMPT="╾─┘"