#!/bin/sh
# ~/.profile
# Made on kisho, strives to be universal now
# xmpp:[email protected]
# mailto:[email protected]

# Helper functions---------------------------------------------------- #
       has-external(){
               for n; do
                       [ -x "$( 2>/dev/null command -v $n )" ] || return 1
               done
       }

# Freedesktop definitions--------------------------------------------- #
 export XDG_CONFIG_HOME="${HOME}/.config";
 export XDG_CACHE_HOME="${HOME}/.local/cache";
 export XDG_DATA_HOME="${HOME}/.local/share";
 export XDG_STATE_HOME="${HOME}/.local/state";
 if [ -f "${HOME}/.config/user-dirs.dirs" ]
 then
   while read REPLY
   do
     eval "export ${REPLY};"
   done < "${HOME}/.config/user-dirs.dirs"
 fi

# Mail and other utilities-------------------------------------------- #
 export MAILCHECK="-1";            # No new mail notifs
 export MAILPATH="/var/mail/${LOGNAME}";

 export GNUPGHOME="${XDG_CONFIG_HOME:-$HOME/.config}/gnupg";
 export PASSWORD_STORE_DIR="${HOME}/.local/password-store";

 export PAGER="less";
 export LESS="-giq"
 export LESSKEY="${XDG_CONFIG_HOME:-$HOME/.config}/less"
 export LESSHISTFILE="-";

 umask 077;                        # Leave my stuff

# My editor needs are intricate--------------------------------------- #
 if kak -version | grep -Eq 'v202[01]' # old kak curses suck on ssh
 then
         if vi --version >/dev/null 2>&1; then EDITOR=vim;
         elif has-external vi;            then EDITOR=vi;
         elif has-external vim;           then EDITOR=vim;
         elif has-external micro;         then EDITOR=micro;
         elif has-external joe;           then EDITOR=joe;
         elif has-external nano;          then EDITOR=nano;
         fi
 elif has-external kak;             then EDITOR=kak;
 fi
 export EDITOR;
       [ -n "${EDITOR}" ] && export VISUAL="${EDITOR}";

# Path---------------------------------------------------------------- #
 [ -d "${HOME}/.local/bin" ]     && PATH="${HOME}/.local/bin:${PATH}";
 [ -d "${HOME}/bin" ]            && PATH="${HOME}/bin:${PATH}";
 [ -d "${HOME}/scripts" ]        && PATH="${HOME}/scripts:${PATH}";
 export PATH;

# Tor for libcurl----------------------------------------------------- #
 if netstat -lnt | awk '($0=$4) && /9050$/ {i++};END {if (i<1) exit 1}'
       then
         export ALL_PROXY='socks5h://localhost:9050'
 fi

# Some help for fancy stow packages------------------------------------#
 STOW_SH_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/sh";
 if [ -d "${STOW_SH_DIR}" ]
 then
   for f in "${STOW_SH_DIR}"/*.sh
   do
     [ -f "${f}" ] && . "${f}"
   done
 fi

# If running bash, source the bashrc too------------------------------ #
 if [ -n "${BASH_VERSION}" ]
 then
   BASH_ENV="${HOME}/.bashrc"
   if [ -f "${BASH_ENV}" ]; then . "${BASH_ENV}"; export BASH_ENV; fi

 elif [ -n "${KSH_VERSION}" ] || [ "${SHELL##*/}" = ksh ]
 then
   ENV="${HOME}/.config/ksh/kshrc"
   if [ -f "${ENV}" ]; then export ENV; fi

 fi