#!/bin/sh

# Default programs:
export BROWSER=firefox
export EDITOR=vim
export FILE=ranger
export IMAGES=feh
export PAGER=less
export TERMINAL=kitty
export VISUAL=code

# Misc vars
export CHROME_EXECUTABLE=chromium # For Flutter

# Set XDG directories:
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
export XDG_DATA_DIRS="${XDG_DATA_DIRS:-/usr/share}"

# Clean home:
export ANDROID_HOME="$XDG_DATA_HOME/Android/Sdk"
export CARGO_HOME="$XDG_DATA_HOME/cargo"
export KREW_ROOT="$XDG_DATA_HOME/krew"
export FLUTTER_HOME="$XDG_DATA_HOME/flutter"
export GOPATH="$XDG_DATA_HOME/go"
export HISTFILE="$XDG_DATA_HOME/history"
export XINITRC="$XDG_CONFIG_HOME/x11/xinitrc"
export ZDOTDIR="$XDG_CONFIG_HOME/zsh"
export NVM_DIR="$XDG_CONFIG_HOME/nvm"
export NUGET_PACKAGES="$XDG_DATA_HOME/nuget"

# Color man pages:
export LESS='-r'
export LESS_TERMCAP_mb
export LESS_TERMCAP_md
export LESS_TERMCAP_me
export LESS_TERMCAP_se
export LESS_TERMCAP_so
export LESS_TERMCAP_ue
export LESS_TERMCAP_us
LESS_TERMCAP_mb="$(printf '\033[01;32m')"
LESS_TERMCAP_md="$(printf '\033[01;32m')"
LESS_TERMCAP_me="$(printf '\033[0m')"
LESS_TERMCAP_se="$(printf '\033[0m')"
LESS_TERMCAP_so="$(printf '\033[01;47;34m')"
LESS_TERMCAP_ue="$(printf '\033[0m')"
LESS_TERMCAP_us="$(printf '\033[01;36m')"

# Other program settings:
export DISPLAY_DPI=96

_tr="$(which tr 2>/dev/null || echo /usr/bin/tr)"
_sed="$(which sed 2>/dev/null || echo /usr/bin/sed)"

# Trims the specified leading and trailing characters from the string
trim() {
    string="${1:?Missing string}"
    char="${2:?Missing trim character}"
    echo "$string" | $_sed "s/^$char*//; s/$char*$//"
}

rm_env_path_entry() {
    src_entries="${1:?Missing source entries}"
    del_entry="${2:?Missing entry to delete}"
    new_entries=""

    for entry in $(echo "$src_entries" | $_tr ':' ' '); do
        if [ "$entry" != "$del_entry" ]; then
            new_entries="$new_entries:$entry"
        fi
    done

    [ -n "$new_entries" ] && trim "$new_entries" ":"
    echo
}

modify_env_path() {
    mode="${1:?Missing mode}"
    entries="$2"
    change="${3:?Missing change}"

    [ -n "$entries" ] && entries="$(trim "$entries" ":")"

    case "$mode" in
    prepend)
        temp_entries="" # Will hold the new entries in the correct order
        for entry in $(echo "$change" | $_tr ':' ' '); do
            [ -z "$entry" ] && continue # Skip empty entries
            [ -d "$entry" ] || continue # Skip non-existent directories
            temp_entries="$temp_entries:$entry"
        done

        # Merge the added entries to the existing entries
        # Trim the leading colon and append the new entries
        entries="${temp_entries#:}:$entries"
        ;;
    append)
        for entry in $(echo "$change" | $_tr ':' ' '); do
            [ -z "$entry" ] && continue # Skip empty entries
            [ -d "$entry" ] || continue # Skip non-existent directories
            entries="$entries:$entry"
        done
        ;;
    delete)
        for entry in $(echo "$change" | $_tr ':' ' '); do
            [ -z "$entry" ] && continue # Skip empty entries
            [ -z "$entries" ] && break  # nothing left to remove
            entries=$(rm_env_path_entry "$entries" "$entry")
        done
        ;;
    *)
        echo "Invalid mode: $mode (choose 'append', 'prepend', 'delete')" >&2
        return 1
        ;;
    esac

    [ -n "$entries" ] && trim "$entries" ":"
    echo
}

prepend_paths="$HOME/.local/bin:$HOME/.cargo/bin:$FLUTTER_HOME/bin:$GOPATH/bin:$KREW_ROOT/bin"

HOMEBREW_PREFIX="$XDG_DATA_HOME/homebrew"
if [ -x "$HOMEBREW_PREFIX/bin/brew" ]; then
    eval "$("$HOMEBREW_PREFIX/bin/brew" shellenv)"
    prepend_paths="$HOMEBREW_PREFIX/bin:$HOMEBREW_PREFIX/sbin:$prepend_paths"
fi

PATH="$(modify_env_path delete "$PATH" "$prepend_paths")"
PATH="$(modify_env_path prepend "$PATH" "$prepend_paths")"

prepend_data_dirs="/var/lib/flatpak/exports/share:$XDG_DATA_HOME/flatpak/exports/share:/var/lib/snapd/desktop:/usr/share:/usr/local/share"

XDG_DATA_DIRS="$(modify_env_path delete "$XDG_DATA_DIRS" "$prepend_data_dirs")"
XDG_DATA_DIRS="$(modify_env_path prepend "$XDG_DATA_DIRS" "$prepend_data_dirs")"

# Add the profile-extras file yourself if you wish to override anything:
PROFILE_EXTRAS="$XDG_CONFIG_HOME/shell/profile-extras"
[ -f "$PROFILE_EXTRAS" ] && . "$PROFILE_EXTRAS" || true
