# ~/.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.
# remove w permissions for g+o
#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
# see: https://wiki.archlinux.org/index.php/Locale
# no LC_ALL as its used for debugging purposes
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

# variables {{{1
# fix systems missing $USER
# [ -z "${USER-}" ] && export USER="$(whoami)"
# [ -z "${HOSTNAME-}" ] && HOSTNAME="$({ hostname 2>/dev/null || uname -n; } | head -n 1)"
# : "${HOST:=${HOSTNAME:-$(hostname)}}"
# : "${HOSTNAME:=${HOST}}"

# zsh sets USERNAME, GROUPS, and E?[UG]ID
# bash only sets E?UID
# USER=${USERNAME:-$(id -un)}
# : "${USERNAME:=${USER}}"
# export USER USERNAME

# 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 -p -- 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'

# path {{{1

test -d "${HOME%/}"'/bin/util' \
  && PATH="${HOME%/}/bin/util:${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-}}"

# homebrew-keg {{{1

if test -x /opt/homebrew/bin/brew; then
  #export HOMEBREW_BUNDLE_INSTALL_CLEANUP=1
  export HOMEBREW_BUNDLE_FILE="${HOME%/}/.config/homebrew/Brewfile"
  # export HOMEBREW_BUNDLE_FILE_GLOBAL="${HOME%/}/.config/homebrew/Brewfile"
  # export HOMEBREW_BUNDLE_USER_CACHE="${XDG_CACHE_HOME-}"'/bundle'
  export HOMEBREW_CASK_OPTS="--require-sha, --appdir=/Applications"
  export HOMEBREW_CASK_OPTS_REQUIRE_SHA=1
  export HOMEBREW_NO_ANALYTICS=1
  export HOMEBREW_NO_AUTO_UPDATE=1
  export HOMEBREW_NO_INSECURE_REDIRECT=1
  # export HOMEBREW_NO_ENV_HINTS=1
  export HOMEBREW_NO_EMOJI=1
  export HOMEBREW_NO_UPDATE_REPORT_NEW=1
  # test -d "${HOMEBREW_PREFIX-}"'/opt/findutils/libexec/gnubin' &&
  #   PATH="${HOMEBREW_PREFIX-}"'/opt/findutils/libexec/gnubin'"${PATH:+:${PATH-}}"
  # export PATH="/opt/homebrew/opt/util-linux/sbin:${PATH}"
  # export PATH="/opt/homebrew/opt/util-linux/bin:${PATH}"
  export PATH="/opt/homebrew/opt/binutils/bin:${PATH}"
  # LLVM
  export PATH="/opt/homebrew/opt/llvm/bin:${PATH}"
  # PATH="/opt/homebrew/opt/make/libexec/gnubin:${PATH}"
  PATH="/opt/homebrew/opt/ed/libexec/gnubin:${PATH}"
  # PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:${PATH}"
  export PATH="/opt/homebrew/opt/trash-cli/bin:${PATH}"
  export PATH="/opt/homebrew/opt/sqlite/bin:${PATH}"
  export PATH="/opt/homebrew/opt/ruby/bin:${PATH}"
  export PATH="/opt/homebrew/opt/rustup/bin:${PATH}"
  export PATH="/opt/homebrew/opt/python/libexec/bin:${PATH}"
  export PATH="/opt/homebrew/opt/openssl/bin:${PATH}"
  export PATH="/opt/homebrew/opt/ncurses/bin:${PATH}"
  export PATH="/opt/homebrew/opt/make/libexec/gnubin:${PATH}"
  export PATH="/opt/homebrew/opt/man-db/libexec/bin:${PATH}"
  export PATH="/opt/homebrew/opt/libtool/libexec/gnubin:${PATH}"
  export PATH="/opt/homebrew/opt/libarchive/bin:${PATH}"
  export PATH="/opt/homebrew/opt/grep/libexec/gnubin:${PATH}"
  export PATH="/opt/homebrew/opt/gpatch/libexec/gnubin:${PATH}"
  # export PATH="/opt/homebrew/opt/go/libexec/bin:${PATH}"
  export PATH="/opt/homebrew/opt/gnu-which/libexec/gnubin:${PATH}"
  export PATH="/opt/homebrew/opt/gnu-tar/libexec/gnubin:${PATH}"
  export PATH="/opt/homebrew/opt/gnu-sed/libexec/gnubin:${PATH}"
  export PATH="/opt/homebrew/opt/gnu-indent/libexec/gnubin:${PATH}"
  export PATH="/opt/homebrew/opt/gnu-getopt/bin:${PATH}"
  export PATH="/opt/homebrew/opt/gawk/libexec/gnubin:${PATH}"
  # export PATH="/opt/homebrew/opt/findutils/libexec/gnubin:${PATH}"
  export PATH="/opt/homebrew/opt/file-formula/bin:${PATH}"
  export PATH="/opt/homebrew/opt/ed/bin:${PATH}"
  export PATH="/opt/homebrew/opt/curl/bin:${PATH}"
  # test -d "$(brew --prefix python)"/libexec/bin \
  #   && export PATH="$(brew --prefix python)/libexec/bin:${PATH}"
  # $(brew --prefix)/lib/pythonX.Y/site-packages
fi

# homebrew {{{1

if test -x /opt/homebrew/bin/brew; then
  PATH="/opt/homebrew/bin:/opt/homebrew/sbin${PATH+:${PATH-}}"
  export HOMEBREW_PREFIX="/opt/homebrew"
  export HOMEBREW_CELLAR="/opt/homebrew/Cellar"
  export HOMEBREW_REPOSITORY="/opt/homebrew"
  # shellcheck disable=SC2312
  eval "$(/usr/bin/env PATH_HELPER_ROOT="/opt/homebrew" /usr/libexec/path_helper -s)"
  # export PATH="/opt/homebrew/bin:/opt/homebrew/sbin${PATH+:${PATH-}}"
  [ -z "${MANPATH-}" ] || export MANPATH=":${MANPATH#:}"
  # export MANPATH="/opt/homebrew/share/man${MANPATH+:$MANPATH}:"
  export INFOPATH="/opt/homebrew/share/info:${INFOPATH:-}"
  export CPATH="/opt/homebrew/include"
  # curl
  export LDFLAGS="${LDFLAGS:+${LDFLAGS-} }"'-L'"${HOMEBREW_PREFIX-}"'/opt/curl/lib'
  export CPPFLAGS="${CPPFLAGS:+${CPPFLAGS-} }"'-I'"${HOMEBREW_PREFIX-}"'/opt/curl/include'
  # PKG_CONFIG_PATH="${PKG_CONFIG_PATH:+${PKG_CONFIG_PATH-}:}${HOMEBREW_PREFIX-}"'/opt/curl/lib/pkgconfig'

  # openssl
  export LDFLAGS="${LDFLAGS:+${LDFLAGS-} }"'-L'"${HOMEBREW_PREFIX-}"'/opt/openssl/lib'
  export CPPFLAGS="${CPPFLAGS:+${CPPFLAGS-} }"'-I'"${HOMEBREW_PREFIX-}"'/opt/openssl/include'
  PKG_CONFIG_PATH="${PKG_CONFIG_PATH:+${PKG_CONFIG_PATH-}:}${HOMEBREW_PREFIX-}"'/opt/openssl/lib/pkgconfig'
fi

# # 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-}}"

case $(uname) in
  Darwin)
    PATH="${HOME%/}/bin/darwin:${PATH}"
    ;;
  *)
    PATH="${HOME%/}/bin/linux:${PATH}"
    # if [ -z "${DISPLAY:-}" ]; then
    #   export DISPLAY=:0
    # fi
    # test -f '/etc/rpi-issue' \
    #   && export XDG_SESSION_TYPE=wayland \
    #   && 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}"

# ensure good return code
