#!/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

if [ -z "${HOME}" ]; then
    export HOME="/home/severin"
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"

if [ "$(hostname)" = "loki" ]; 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 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"
export PARALLEL_HOME="$XDG_CONFIG_HOME/parallel"
export CCACHE_CONFIGPATH="${XDG_CONFIG_HOME}/ccache/config"
export CCACHE_DIR="${XDG_CACHE_HOME}/ccache"
mkdir -p "${XDG_CONFIG_HOME}/pg"
mkdir -p "${XDG_CACHE_HOME}/pg"
export PSQLRC="${XDG_CONFIG_HOME}/pg/psqlrc"
export PSQL_HISTORY="${XDG_CACHE_HOME}/pg/psql_history"
export PGPASSFILE="${XDG_CONFIG_HOME}/pg/pgpass"
export PGSERVICEFILE="${XDG_CONFIG_HOME}/pg/pg_service.conf"
export STARSHIP_CONFIG="${XDG_CONFIG_HOME}/starship/config.toml"
export OCTAVE_HISTFILE="${XDG_CACHE_HOME}/octave-hist"
export OCTAVE_SITE_INITFILE="${XDG_CONFIG_HOME}/octave/octaverc"
mkdir -p "${XDG_CONFIG_HOME}/bazaar"
export VAGRANT_HOME="${XDG_DATA_HOME}/vagrant"
export VAGRANT_ALIAS_FILE="${XDG_DATA_HOME}/vagrant/aliases"
mkdir -p "${XDG_CONFIG_HOME}/mpd/playlists"
mkdir -p "${XDG_CACHE_HOME}/less"
export LESSKEY="${XDG_CONFIG_HOME}/less/lesskey"
export LESSHISTFILE="${XDG_CACHE_HOME}/less/history"
mkdir -p "${XDG_DATA_HOME}/mysql"
export MYSQL_HISTFILE="${XDG_DATA_HOME}/mysql/history"
export ANDROID_SDK_HOME="${XDG_CONFIG_HOME}/android"
mkdir -p "${XDG_DATA_HOME}/nuget"
export NUGET_PACKAGES="${XDG_CACHE_HOME}/nuget/packages"
export _JAVA_OPTIONS=-Djava.util.prefs.userRoot="${XDG_CONFIG_HOME}/java"
export GRADLE_USER_HOME="${XDG_DATA_HOME}/gradle"

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

# Java
export IDEA_JDK="/usr/lib/jvm/jdk-jetbrains"
export PHPSTORM_JDK="${IDEA_JDK}"
export CLION_JDK="${IDEA_JDK}"

# zsh
export ZDOTDIR="${XDG_CONFIG_HOME}/zsh"
export HISTFILE="${XDG_DATA_HOME}/zsh/history"
export HISTCONTROL=ignoreboth
export HISTSIZE=10000
export SAVEHIST="${HISTSIZE}"
export HISTFILESIZE="${HISTSIZE}"
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

export MTGA_WINE_PREFIX="${HOME}/games/magic-the-gathering-arena"
export MTGA_WINE_BINARY="/usr/bin/wine"

# Setting applications
export VISUAL=vim
export EDITOR="${VISUAL}"
export TERMINAL="alacritty"
export BROWSER="chromium"
export PAGER="less"
export MANPAGER="${PAGER}"

# QT
export QT_QPA_PLATFORMTHEME="qt5ct"

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

export SDL_GAMECONTROLLERCONFIG="050000007e0500003003000001000000,Nintendo Wii Remote Pro Controller,platform:Linux,a:b0,b:b1,x:b3,y:b2,back:b8,guide:b10,start:b9,leftstick:b11,rightstick:b12,leftshoulder:b4,rightshoulder:b5,dpup:b13,dpdown:b14,dpleft:b15,dpright:b16,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,"

# Adding folders to the PATH variable
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"
export PATH="${HOME}/.local/bin:${PATH}"

# 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}[%D{%T}]%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="╾─┘"
eval "$(starship init zsh)"