333 lines
8.8 KiB
Bash
333 lines
8.8 KiB
Bash
# ─< Check if the given command exists silently >─────────────────────────────────────────
|
||
command_exists() {
|
||
command -v "$@" >/dev/null 2>&1
|
||
}
|
||
|
||
#
|
||
# Check if the user is root and set sudo variable if necessary
|
||
check_root() {
|
||
if [[ "${EUID}" -ne 0 ]]; then
|
||
if command_exists sudo; then
|
||
echo_info "User <$(whoami)> is not root. Using sudo for privileged operations."
|
||
_sudo="sudo"
|
||
else
|
||
echo_error "No sudo found and you're not root! Can't install packages."
|
||
fi
|
||
else
|
||
echo_info "Root access confirmed."
|
||
_sudo=""
|
||
fi
|
||
}
|
||
|
||
# ───────────────────────────────────< Message storage >─────────────────────────────────
|
||
typeset -A _MESSAGES
|
||
_MESSAGES=(
|
||
[missing]=""
|
||
[error]=""
|
||
[warn]=""
|
||
[info]=""
|
||
)
|
||
|
||
# Define color variables
|
||
RED='\033[0;31m'
|
||
YELLOW='\033[0;33m'
|
||
CYAN='\033[0;36m'
|
||
GREEN='\033[1;32m'
|
||
NC='\033[0m' # No Color
|
||
BOLD='\033[1m'
|
||
|
||
# Functions to store messages
|
||
echo_error() {
|
||
_MESSAGES[error]+="${RED}❌ $@${NC}\n"
|
||
}
|
||
|
||
echo_missing() {
|
||
_MESSAGES[missing]+="${YELLOW} $@${NC}\n"
|
||
}
|
||
|
||
echo_warning() {
|
||
_MESSAGES[warn]+="${YELLOW}⚠️ $@${NC}\n"
|
||
}
|
||
|
||
echo_info() {
|
||
_MESSAGES[info]+="${CYAN}ℹ️ $@${NC}\n"
|
||
}
|
||
|
||
# Display stored messages
|
||
error_log() {
|
||
[[ -z "${_MESSAGES[error]}${_MESSAGES[warn]}${_MESSAGES[info]}${_MESSAGES[missing]}" ]] && return 0
|
||
|
||
typeset -A headers colors
|
||
headers=(
|
||
missing "⚠️ MISSING ESSENTIALS ⚠️"
|
||
error "❌ Errors"
|
||
warn "⚠️ Warnings"
|
||
info "ℹ️ Info"
|
||
)
|
||
colors=(
|
||
missing "$YELLOW"
|
||
error "$RED"
|
||
warn "$YELLOW"
|
||
info "$CYAN"
|
||
)
|
||
|
||
for type in error warn info missing; do
|
||
[[ -n "${_MESSAGES[$type]}" ]] && {
|
||
printf "\n${BOLD}${colors[$type]}=== ${headers[$type]} ===${NC}\n"
|
||
printf "${_MESSAGES[$type]}"
|
||
}
|
||
done
|
||
}
|
||
|
||
__shell_qol__() {
|
||
if command_exists oh-my-posh; then
|
||
|
||
# local zen=~/.zsh/themes/zen.toml
|
||
local info=~/.zsh/themes/clean-info.toml
|
||
local power=~/.zsh/themes/power10k_edit.toml
|
||
|
||
# if no ssh connection is there, use the $info theme
|
||
# else
|
||
# use the $power theme
|
||
if [ -z "$SSH_CONNECTION" ]; then
|
||
# eval "$(oh-my-posh init zsh --config 'https://git.k4li.de/dotfiles/oh-my-posh/raw/branch/main/amro.toml')"
|
||
# eval "$(oh-my-posh init zsh --config '~/.zsh/themes/power10k_edit.toml')"
|
||
# eval "$(oh-my-posh init zsh --config ${zen})"
|
||
eval "$(oh-my-posh init zsh --config ${info})"
|
||
else
|
||
eval "$(oh-my-posh init zsh --config ${power})"
|
||
fi
|
||
else
|
||
echo_missing "oh-my-posh"
|
||
curl -s https://ohmyposh.dev/install.sh | $_sudo bash --norc -s -- -d /usr/bin/
|
||
fi
|
||
|
||
# ─< init fzf for zsh >───────────────────────────────────────────────────────────────────
|
||
if command_exists fzf; then
|
||
source <(fzf --zsh)
|
||
fi
|
||
|
||
if command_exists zoxide; then
|
||
|
||
local raw="$(zoxide --version | awk '{print $2}')"
|
||
local ver="${raw#v}" # remove leading 'v'
|
||
local ver="${ver%%-*}" # remove anything after dash
|
||
|
||
# local ver="$(zoxide --version | cut -d' ' -f2)"
|
||
if [[ "$ver" =~ ^0\.([0-5]|[0-9]{1,2})\. ]]; then
|
||
# older than 0.5.x
|
||
eval "$(zoxide init zsh)"
|
||
else
|
||
# newer than 0.5.x
|
||
eval "$(zoxide init zsh)"
|
||
eval "$(zoxide init zsh --cmd cd)"
|
||
fi
|
||
|
||
# ─< easier dir up >────────────────────────────────────────────────────────────────────────
|
||
alias ..="z .."
|
||
else
|
||
echo_missing "zoxide"
|
||
# ─< easier dir up >────────────────────────────────────────────────────────────────────────
|
||
alias ..="cd .."
|
||
fi
|
||
}
|
||
|
||
__get_Packager__() {
|
||
# Load OS release information
|
||
[ -f /etc/os-release ] && . /etc/os-release || return 1
|
||
|
||
DISTRO="${ID}:${ID_LIKE}"
|
||
|
||
case "$DISTRO" in
|
||
*debian*)
|
||
pkg_install() {
|
||
if command_exists nala; then
|
||
$_sudo nala install --assume-yes "$@"
|
||
else
|
||
$_sudo apt install --assume-yes "$@"
|
||
fi
|
||
}
|
||
|
||
if command_exists nala; then
|
||
alias search="nala search"
|
||
alias update="$_sudo nala update && $_sudo nala upgrade --full"
|
||
alias remove="$_sudo nala purge"
|
||
else
|
||
alias search="apt-cache search"
|
||
alias update="$_sudo apt update && $_sudo apt upgrade"
|
||
alias remove="$_sudo apt purge"
|
||
fi
|
||
|
||
alias unbreak="$_sudo dpkg --configure -a"
|
||
alias install="pkg_install"
|
||
;;
|
||
*arch* | *cachyos*)
|
||
pkg_install() {
|
||
if command_exists paru; then
|
||
paru -S --color always --noconfirm --needed "$@"
|
||
elif command_exists yay; then
|
||
yay -S --color always --noconfirm --needed "$@"
|
||
else
|
||
$_sudo pacman -S --color always --noconfirm --needed "$@"
|
||
fi
|
||
}
|
||
if command_exists paru; then
|
||
alias search="paru -Ss --color always"
|
||
# alias install="paru -S --color always --noconfirm"
|
||
alias update="paru -Syu --color always"
|
||
alias remove="paru -R --color always"
|
||
elif command_exists yay; then
|
||
alias search="yay -Ss --color always"
|
||
# alias install="yay -S --noconfirm --color always"
|
||
alias update="yay -Syu --color always"
|
||
alias remove="yay -R --color always"
|
||
else
|
||
alias search="$_sudo pacman -Ss --color always"
|
||
# alias install="$_sudo pacman -S --noconfirm --color always"
|
||
alias update="$_sudo pacman -Syu --color always"
|
||
alias remove="$_sudo pacman -R --color always"
|
||
fi
|
||
alias install="pkg_install"
|
||
;;
|
||
*rhel* | *fedora*)
|
||
alias search="dnf search"
|
||
alias install="$_sudo dnf install -y --skip-missing"
|
||
alias update="$_sudo dnf update -y"
|
||
alias remove="$_sudo dnf remove -y"
|
||
;;
|
||
*suse*)
|
||
alias search="zypper search"
|
||
alias install="$_sudo zypper install --no-confirm"
|
||
alias update="$_sudo zypper update"
|
||
alias remove="$_sudo zypper remove"
|
||
;;
|
||
*alpine*)
|
||
alias install="$_sudo apk add"
|
||
alias update="$_sudo apk update && $_sudo apk upgrade"
|
||
alias remove="$_sudo apk del"
|
||
;;
|
||
*nixos*)
|
||
echo_info "Using NIX!!"
|
||
alias update="$_sudo nixos-rebuild switch"
|
||
# alias install="$_sudo nix-env -iA nixos."
|
||
install() {
|
||
"$_sudo nix-end -iA nixos.$@"
|
||
}
|
||
alias edit="$_sudo -E $EDITOR /etc/nixos/configuration.nix"
|
||
;;
|
||
*)
|
||
echo_error "Unsupported distro: $ID"
|
||
;;
|
||
esac
|
||
}
|
||
|
||
__sources__() {
|
||
local sourceDir="$HOME/.zsh"
|
||
local sourceOptions=(
|
||
# "defaults"
|
||
"plugins"
|
||
"installs"
|
||
"aliases"
|
||
)
|
||
|
||
for i in "${sourceOptions[@]}"; do
|
||
if [ -e "${sourceDir}/.$i.zsh" ]; then
|
||
. "${sourceDir}/.$i.zsh"
|
||
fi
|
||
done
|
||
}
|
||
|
||
__defaults__() {
|
||
# Load completions
|
||
autoload -Uz compinit && compinit
|
||
|
||
bindkey -e
|
||
|
||
# # Define custom word style that treats special characters as word boundaries
|
||
autoload -Uz select-word-style
|
||
select-word-style bash
|
||
|
||
setopt appendhistory
|
||
setopt sharehistory
|
||
setopt hist_ignore_space
|
||
setopt hist_ignore_all_dups
|
||
setopt hist_save_no_dups
|
||
setopt hist_ignore_dups
|
||
setopt hist_find_no_dups
|
||
|
||
# Huge history. Doesn't appear to slow things down, so why not?
|
||
HISTSIZE=500000
|
||
HISTFILESIZE=100000
|
||
|
||
HISTFILE=~/.zsh_history
|
||
SAVEHIST=$HISTSIZE
|
||
HISTDUP=erase
|
||
# Completion styling
|
||
zstyle :compinstall filename "$HOME/.zshrc"
|
||
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'
|
||
# zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
|
||
zstyle ':completion:*' menu no
|
||
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'ls --color $realpath'
|
||
zstyle ':fzf-tab:complete:__zoxide_z:*' fzf-preview 'ls --color $realpath'
|
||
setopt autocd notify
|
||
# End of lines configured by zsh-newuser-install
|
||
#
|
||
}
|
||
|
||
__keychain_setup__() {
|
||
setupKeys() {
|
||
local keys=(
|
||
"homelab-id_rsa"
|
||
"hetzner_id_rsa"
|
||
)
|
||
|
||
for i in "${keys[@]}"; do
|
||
if [ -n $i ]; then
|
||
if [ -e "$i" ]; then
|
||
eval "$(keychain --eval --noask --agents ssh ~/.ssh/$i)"
|
||
fi
|
||
else
|
||
return 69
|
||
fi
|
||
done
|
||
}
|
||
|
||
if ! setupKeys; then
|
||
eval "$(keychain --eval --noask --agents ssh ~/.ssh/{homelab-id_rsa,hetzner_id_rsa})"
|
||
fi
|
||
}
|
||
|
||
__end__() {
|
||
if command_exists fastfetch; then
|
||
clear &&
|
||
fastfetch --config os
|
||
|
||
fi
|
||
|
||
if command_exists cowsay; then
|
||
alias clear='clear && cowsay -f tux "$(uptime --pretty)"'
|
||
cowsay -f tux "$(uptime --pretty)"
|
||
else
|
||
echo_missing "cowasy"
|
||
fi
|
||
|
||
__shell_qol__
|
||
error_log
|
||
}
|
||
|
||
main() {
|
||
__defaults__
|
||
__get_Packager__
|
||
__sources__
|
||
__end__
|
||
|
||
if command_exists keychain; then
|
||
__keychain_setup__
|
||
else
|
||
echo_missing "keychain"
|
||
fi
|
||
}
|
||
|
||
if check_root; then
|
||
main
|
||
fi
|