profile/home/dotfiles/profile

272 lines
6.2 KiB
Bash

#!/bin/sh
# just for this profile code
PROFILE_DEBUG="${PROFILE_DEBUG:-0}"
##############################################################################
# Profile helper code from here
PROFILE_DEBUG_SECTION_START_PREFIX="\\ "
PROFILE_DEBUG_INDENT="${PROFILE_DEBUG_INDENT:- |}"
PROFILE_DEBUG_SECTION_END_TEXT="/"
DEBUG_INDENT=0
when_binary_available() {
if has_binary "$@"; then
printf '1'
return
fi
printf '0'
return
}
has_binary() {
command -v "$@" >/dev/null 2>&1
}
section() {
if [ $PROFILE_DEBUG -gt 0 ]; then
NO_SPACE_PREFIX=1 log "${PROFILE_DEBUG_SECTION_START_PREFIX}$(tput smso)$*$(tput rmso)"
fi
DEBUG_INDENT=$(expr $DEBUG_INDENT + 1)
}
section_end() {
DEBUG_INDENT=$(expr $DEBUG_INDENT - 1)
if [ $PROFILE_DEBUG -gt 0 ]; then
NO_SPACE_PREFIX=1 log "$PROFILE_DEBUG_SECTION_END_TEXT"
fi
}
log() {
if [ $PROFILE_DEBUG -gt 0 ]; then
indent=""
if [ $DEBUG_INDENT -gt 0 ]; then
for _ in $(seq 0 $(( DEBUG_INDENT-1 )) ); do
indent="${indent}${PROFILE_DEBUG_INDENT}"
done
fi
prefix=" "
if [ ${NO_SPACE_PREFIX:-0} -gt 0 ]; then
prefix=""
fi
echo "$*" | while IFS= read -r line; do
echo "$indent$prefix$line"
done
fi
}
##############################################################################
# home bin
# Including this before doing any checks is crucial for detecting user-local
# installed binaries.
section "home bin"
PROFILE_BIN=${PROFILE_BIN:-1}
if [ $PROFILE_BIN -gt 0 ]; then
PATH="$HOME/bin:$HOME/.local/bin:$PATH"
export PATH
fi
section_end
##############################################################################
# Profile tweaks and features
PROFILE_COMPOSER=${PROFILE_COMPOSER:-$(when_binary_available composer)}
PROFILE_CUSTOM=${PROFILE_CUSTOM:-1}
PROFILE_DEFAULT_TERM=${PROFILE_DEFAULT_TERM:-1}
PROFILE_GO=${PROFILE_GO:-$(when_binary_available go)}
PROFILE_GPG_PINENTRY_FIX=${PROFILE_GPG_PINENTRY_FIX:-1}
PROFILE_IBUS=${PROFILE_IBUS:-$(when_binary_available ibus)}
PROFILE_KEYCHAIN=${PROFILE_KEYCHAIN:-0}
PROFILE_NANO=${PROFILE_NANO:-$(when_binary_available nano)}
PROFILE_NANO_EXPLICIT_WIDE=${PROFILE_NANO_EXPLICIT_WIDE:-0}
PROFILE_NO_BEEPS=${PROFILE_NO_BEEPS:-$([ "$(uname -s)" != "Darwin" ] && when_binary_available xset || echo 0)}
PROFILE_NODE=${PROFILE_NODE:-$(when_binary_available node)}
PROFILE_PRINT_REBOOT_REQUIRED=${PROFILE_PRINT_REBOOT_REQUIRED:-0} # hacky, see below
PROFILE_RUBY=${PROFILE_RUBY:-$(when_binary_available ruby)}
PROFILE_RUST=${PROFILE_RUST:-$(when_binary_available rust)}
PROFILE_SSH_AGENT=${PROFILE_SSH_AGENT:-$(when_binary_available ssh-agent)}
PROFILE_SYSTEMD=${PROFILE_SYSTEMD:-$(when_binary_available systemctl)}
PROFILE_THEFUCK=${PROFILE_THEFUCK:-$(when_binary_available thefuck)}
PROFILE_YARN=${PROFILE_YARN:-$(when_binary_available yarn)}
section "profile variables"
log "$(set | grep '^PROFILE_')"
section_end
##############################################################################
# Actual profile code from here
section "custom profile"
if [ $PROFILE_CUSTOM -gt 0 ]; then
log "Checking if local profile code exists at ~/.local_profile..."
if [ -f ~/.local_profile ]; then
log "Exists, loading."
# shellcheck source=/dev/null
. ~/.local_profile
else
log "Does not exist, skipping."
fi
fi
section_end
section "thefuck"
if [ $PROFILE_THEFUCK -gt 0 ]; then
eval "$(thefuck --alias)"
fi
section_end
# Keychain (GPG/SSH keys)
section "keychain"
if [ $PROFILE_KEYCHAIN -gt 0 ]; then
eval "$(keychain --eval -Q --quiet id_ed25519 id_rsa)"
eval "$(keychain --eval -Q --quiet --agents ssh,gpg)"
fi
section_end
# Default terminal
section "term"
if [ $PROFILE_DEFAULT_TERM -gt 0 ] && [ -z "$TERM" ]; then
export TERM=linux
fi
section_end
# composer
section "composer"
if [ $PROFILE_COMPOSER -gt 0 ]; then
composer_path="$(composer global config --global --absolute bin-dir -q)"
if [ -n "$composer_path" ]
then
log "Detected composer bin path: $composer_path"
PATH="$PATH:$composer_path"
export PATH
else
log "No composer bin path detected."
fi
fi
section_end
# ruby
section "ruby"
if [ $PROFILE_RUBY -gt 0 ]; then
printf "%s:" "$(gem env path)" | while IFS= read -rd: dir; do
PATH="$PATH:$dir/bin"
done
export PATH
fi
section_end
# gpg password via console
section "gpg pinentry fix"
if [ $PROFILE_GPG_PINENTRY_FIX -gt 0 ]; then
log "Changing GPG_TTY to: $(tty)"
GPG_TTY=$(tty)
export GPG_TTY
if [ ! -z "$SSH_CONNECTION" ]; then
log "Detected SSH connection, making pinentry use curses instead of x11."
export PINENTRY_USER_DATA="USE_CURSES=1"
else
log "All good with pinentry itself it seems."
fi
fi
section_end
# rust
section "rust"
if [ $PROFILE_RUST -gt 0 ]; then
PATH="$PATH:$HOME/.cargo/bin"
export PATH
fi
section_end
# go
section "go"
if [ $PROFILE_GO -gt 0 ]; then
GOPATH="$HOME/go"
export GOPATH
PATH="$PATH:$GOPATH/bin"
export PATH
fi
section_end
# nano
section "nano"
if [ $PROFILE_NANO -gt 0 ]; then
EDITOR="nano"
section "explicit -w"
if [ $PROFILE_NANO_EXPLICIT_WIDE -gt 0 ]; then
EDITOR="$EDITOR -w"
fi
section_end
export EDITOR
fi
section_end
# Yarn
section "yarn"
if [ $PROFILE_YARN -gt 0 ]; then
log "Adding yarn to path..."
PATH="$HOME/.yarn/bin:$PATH"
export PATH
fi
section_end
# node
section "node"
if [ $PROFILE_NODE -gt 0 ]; then
# priority over system path (npm, yarn)
npm_config_prefix="$HOME/.node_modules"
PATH="$npm_config_prefix/bin:$PATH"
export npm_config_prefix
export PATH
fi
section_end
# NO BEEPS
section "no beeps"
if [ $PROFILE_NO_BEEPS -gt 0 ]; then
if [ ! -z "$DISPLAY" ]; then
xset -b
fi
fi
section_end
# IBUS
section "ibus"
if [ $PROFILE_IBUS -gt 0 ]; then
export GTK_IM_MODULE=ibus
export XMODIFIERS=@im=ibus
export QT_IM_MODULE=ibus
ibus-daemon -drx
fi
section_end
# SSH agent
section "ssh-agent"
if [ $PROFILE_SSH_AGENT -gt 0 ]; then
if [ -z "$SSH_AUTH_SOCK" ]; then
eval "$(ssh-agent -s)"
fi
fi
section_end
# Reboot required? (hacky code)
section "reboot required?"
if [ $PROFILE_PRINT_REBOOT_REQUIRED -gt 0 ]; then
if [ "$(pacman -Q linux | cut -d " " -f 2)" -gt "$(uname -r | sed 's,-zen,,' | sed 's,-lts,,')" ]; then
echo "$(tput setaf 1)$(tput bold)Reboot required!"
fi
fi
section_end
section "systemd"
if [ $PROFILE_SYSTEMD -gt 0 ]; then
log "Making sure systemctl knows about our newly built PATH..."
log "$(systemctl --user import-environment PATH)"
fi
section_end