# ~/.profile: executed by the command interpreter for login shells.
# vim: ft=sh:fdm=marker:fdl=0:noml:
#
# see also sh(1), environ(7).
#
# this file is not read by bash(1), if ~/.bash_profile or ~/.bash_login exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# note: verify with `eval printf '%s\\n' "${ENV}"`
export ENV="${HOME%/}"'/.rc'

# skel {{{
# # if running bash
# if [ -n "$BASH_VERSION" ]; then
#   # include .bashrc if it exists
#   if [ -f "$HOME/.bashrc" ]; then
#     . "$HOME/.bashrc"
#   fi
# fi

# # set PATH so it includes user's private bin if it exists
# if [ -d "$HOME/bin" ] ; then
#   PATH="$HOME/bin:$PATH"
# fi

# # set PATH so it includes user's private bin if it exists
# if [ -d "$HOME/.local/bin" ] ; then
#   PATH="$HOME/.local/bin:$PATH"
# fi
# }}}
# encoding {{{1
# no LC_ALL as its used for debugging purposes
# see: https://wiki.archlinux.org/index.php/Locale
export LANG=en_US.UTF-8
# readline library initializes according to LC_CTYPE at startup
# get 8bit characters printed
export LC_CTYPE=en_US.UTF-8
# sorting order for characters | unix-style sorting
export LC_COLLATE=C
# 24 hour
export LC_TIME=C

# timezone rule '/usr/share/zoneinfo/Europe/Zurich'
export TZ='Europe/Zurich'

# # tmpfs {{{1
# if test -z "${TMPDIR}"; then
#   case "$(uname)" in
#     Darwin)
#       _CS_DARWIN_USER_TEMP_DIR=$(command -p -- getconf -- DARWIN_USER_TEMP_DIR)
#       TMPDIR=${_CS_DARWIN_USER_TEMP_DIR}
#       ;;
#     *)
#       TMPDIR="${TMPDIR:-/tmp}"
#       ;;
#   esac
# fi
# export TMPDIR

# xdg {{{1

# https://specifications.freedesktop.org/basedir-spec/0.7/ar01s03.html
# https://wiki.archlinux.org/title/XDG_Base_Directory
# command -p -- mkdir -p -- "${HOME%/}"'/.local/share' \
#   && export XDG_DATA_HOME="${HOME%/}"'/.local/share'
# command -p -- mkdir -p -- "${HOME%/}"'/.config' \
#   && export XDG_CONFIG_HOME="${HOME%/}"'/.config'
# command -p -- mkdir -p -- "${HOME%/}"'/.local/state' \
#   && export XDG_STATE_HOME="${HOME%/}"'/.local/state'
# command mkdir -p -- "${HOME%/}"'/.cache' \
#   && export XDG_CACHE_HOME="${HOME%/}"'/.cache'

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

# xdg_runtime_dir
if [ -z "${XDG_RUNTIME_DIR-}" ]; then
  # euid/uid | ksh workaround
  if [ -x /usr/bin/id ]; then
    if [ -z "${EUID-}" ]; then
      # ksh workaround
      EUID=$(/usr/bin/id -u)
      UID=$(/usr/bin/id -ru)
    fi
    # USER="$(/usr/bin/id -un)"
  fi
  if test -d '/run/user'; then
    command -p -- mkdir -p -- /run/user/"${EUID-}" \
      && command -p -- chmod -- 700 /run/user/"${EUID-}" \
      && XDG_RUNTIME_DIR=/run/user/"${EUID-}"
  else
    # `XDG_RUNTIME_DIR` has no default and requires `700` permissions
    command -p -- mkdir -p -- "${TMPDIR%/}"'/runtime-'"${EUID-}" \
      && command -p -- chmod -- 700 "${TMPDIR%/}"'/runtime-'"${EUID-}" \
      && XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:=${TMPDIR%/}/runtime-${EUID-}}"
  fi
fi
export XDG_RUNTIME_DIR

# xdg-osdirs
export XDG_DESKTOP_DIR="${HOME%/}"'/Desktop'
export XDG_DOCUMENTS_DIR="${HOME%/}"'/Documents'
export XDG_DOWNLOAD_DIR="${HOME%/}"'/Downloads'

# }}}1

test -d "${HOME%/}"'/bin/dev' \
  && PATH="${HOME%/}/bin/dev:${HOME%/}/bin/fuz:${HOME%/}/bin/git:${HOME%/}/bin/vue:${HOME%/}/bin/fukusu:${PATH}"
test -d "${HOME%/}"'/repo/bin' \
  && PATH="${HOME%/}"'/repo/bin'"${PATH:+:${PATH-}}"
test -d "${HOME%/}"'/.local/fzf/bin' \
  && PATH="${HOME%/}"'/.local/fzf/bin'"${PATH:+:${PATH-}}"

# # golang {{{1
# if command -v go >/dev/null 2>&1 || test -x '/usr/local/go/bin/go'; then
#   # export GOPATH="${HOME%/}"'/.local/share/go'
#   # if uname | grep -q Darwin; then
#   #   # GOROOT="$(brew --prefix go)"/libexec
#   #   # export GOROOT
#   #   export GOROOT="${HOMEBREW_PREFIX-}"'/opt/go/libexec'
#   #   export PATH="${GOROOT-}/bin:${PATH}"
#   # else
#   # shellcheck disable=SC2312
#   if [ "$(uname)" != "Darwin" ]; then
#     test -d '/usr/local/go' \
#       && export GOROOT='/usr/local/go' \
#       && export PATH="${GOROOT-}/bin:${PATH}"
#     # export GOROOT="$(go env GOROOT)"
#   fi
#   test "${GOPATH-}" != '' \
#     && test -d "${GOPATH-}"'/bin' \
#     && export PATH="${GOPATH-}/bin:${PATH}"
#   # export PATH="$(go env GOPATH)/bin:${PATH}"
#   # export PATH="${GOPATH-}"'/bin'"${PATH:+:${PATH-}}"
#   # export PATH="${GOROOT-}/bin:${GOPATH-}/bin:${PATH}"
#   # export PATH="${PATH}:${GOROOT}/bin:$GOPATH/bin"
# fi

# # }}}1

test -d "${HOME%/}"'/.local/share/go/bin' \
  && PATH="${HOME%/}"'/.local/share/go/bin'"${PATH:+:${PATH-}}"
test -d "${HOME%/}"'/.local/uv/bin' \
  && PATH="${HOME%/}"'/.local/uv/bin'"${PATH:+:${PATH-}}"
test -d "${HOME%/-}"'/.local/share/gem/bin' \
  && PATH="${HOME%/}"'/.local/share/gem/bin'"${PATH:+:${PATH-}}"
test -d "${HOME%/}"'/.local/share/cargo/bin' \
  && PATH="${HOME%/}"'/.local/share/cargo/bin'"${PATH:+:${PATH-}}"
test -d "${HOME%/}"'/.local/bin' \
  && PATH="${HOME%/}"'/.local/bin'"${PATH:+:${PATH-}}"
test -d "${HOME%/}"'/bin/util' \
  && PATH="${HOME%/}"'/bin/util'"${PATH:+:${PATH-}}"

# case $(uname) in
#   Darwin)
#     PATH="${HOME%/}/bin/darwin:${PATH}"
#     ;;
#   *)
#     PATH="${HOME%/}/bin/linux:${PATH}"
#     # test -f '/etc/rpi-issue' \
#     #   && export XDG_DATA_DIRS="${HOME%/}/.local/share:/usr/local/share:/usr/share/raspi-ui-overrides:/usr/share:/usr/share/gdm:/var/lib/menu-xdg"
#     ;;
# esac
# make sure ~/bin is the most preferred in PATH
# export PATH="$HOME/bin:$PATH"
# export PATH="${HOME%/}/bin:${HOME%/}/bin/util:${PATH}"
export PATH="${HOME%/}/bin:${PATH}"

# if running bash
if [ -n "${BASH_VERSION-}" ]; then
  if [ -f "${HOME}/.bashrc" ]; then
    . "${HOME}/.bashrc"
  fi
fi

# ensure good return code
