#!/bin/sh
# shellcheck disable=1090,1091
export ENV=~/.profile

XDG_PREFIX=~/.usr
export XDG_CACHE_HOME="$XDG_PREFIX/var/cache"
export XDG_CONFIG_HOME="$XDG_PREFIX/etc"
export XDG_DATA_HOME="$XDG_PREFIX/share"
export XDG_STATE_HOME="$XDG_PREFIX/state"
export XDG_LOG_HOME="$XDG_PREFIX/var/log"

mkdir -p \
	"$XDG_CACHE_HOME"  \
	"$XDG_CONFIG_HOME" \
	"$XDG_DATA_HOME"   \
	"$XDG_STATE_HOME"  \
	"$XDG_LOG_HOME"

GUIX_PROFILE="$XDG_CONFIG_HOME"/guix/current
if [ -e "$GUIX_PROFILE"/etc/profile ]; then
	. "$GUIX_PROFILE"/etc/profile
fi

HISTSIZE=-1
HISTFILE="$XDG_STATE_HOME"/bash-history
HISTCONTROL=ignorespace:ignoredups

export EDITOR='vi'
export VISUAL="$EDITOR"
export PAGER='less -R'
export GUILE_HISTORY="$XDG_STATE_HOME"/guile-history
export RLWRAP_HOME="$XDG_CACHE_HOME"/rlwrap
export LESSHISTFILE="$XDG_STATE_HOME"/lesshst
export EXINIT='
	" set number
	set autoindent
	set ruler
	set showmode
	set showmatch
'

alias l='ls -lahF --color=auto'
alias grep='grep --color=auto'
alias diff='diff --color'
alias less='less -R'
alias mv='mv -i'
alias rm='rm -i'
alias r='reload'

reload() {
	. ~/.profile
}



#
# PS1
#

error_marker() {
	STATUS=$?
	if [ "$STATUS" != 0 ]; then
		printf ' (!! %s !!) ' "$STATUS"
	fi
}

shell_level() {
	# shellcheck disable=3028
	L="${SHLVL:-1}"
	if [ -n "${TMUX:-}" ]; then
		LVL=$((L - 1))
	else
		LVL="$L"
	fi
	if [ "$LVL" != 1 ]; then
		printf '[%s] ' "$LVL"
	fi
}

guix_env() {
	if [ -n "${GUIX_ENVIRONMENT:-}" ]; then
		printf '\n~> guix environment (%s)' "$GUIX_ENVIRONMENT"
	fi
}

PS1='`error_marker`\T \w/ `shell_level``guix_env`
# '
