installs/yazi.sh
2025-05-18 18:08:18 +02:00

184 lines
5.5 KiB
Bash

{
#!/usr/bin/env bash
# ─< Check if the given command exists silently >─────────────────────────────────────────
command_exists() {
command -v "$@" >/dev/null 2>&1
}
# ─< package variable >───────────────────────────────────────────────────────────────────
unset PACKAGE
# ─< argument list variables >────────────────────────────────────────────────────────────
silent=false
sleep 0.1
PACKAGE=yazi
if command_exists "$PACKAGE"; then
echo_warning "$PACKAGE is already installed!"
echo_warning "Exiting now!"
exit 69
fi
# ─< parse arguments and get variable contents >──────────────────────────────────────────
for arg in "$@"; do
case "$arg" in
--silent | -s)
export silent=true
;;
esac
done
# WHY:
# This import will give you the following variables:
# _sudo="sudo -E" <- only if non root user
# distro = <distro name, like 'arch', 'debian', 'fedora'..>
# arch = bool
# fedora = bool
# opensuse = bool....
# You can then use it for, `if $arch; then`
# Also this gives you the _install command, which installs a package pased on the packagemanager/distro used.
# CAUTION:
# This only wokrs for generic package names, like neovim, or vim, or tmux etc..
# not every package packagemanager has the same packagenames for their packages..
getImports() {
i="https://git.k4li.de/scripts/imports/raw/branch/main/distros.sh"
import="$(mktemp)"
if command_exists curl; then
curl -fsSL $i -o $import
else
echo "curl is required, but missing.."
exit 1
fi
source "$import"
sleep 0.3
rm "$import"
}
getDependencies() {
echo_info "Checking build dependencies.."
# INFO:
# ╭─────────────────────────────────────────────────────────────────────────╮
# │ You can define dependencies for various linux distros here. It will │
# │ automagically be pulled via the $pkgArray[$distro] variable │
# ╰─────────────────────────────────────────────────────────────────────────╯
depsDebian=(libpoppler-dev)
depsFedora=()
depsOpensuse=()
depsArch=()
depsAlpine=()
generalDeps=(
ffmpeg
make
gcc
7zip
)
declare -A deps=(
[debian]="depsDebian"
[ubuntu]="depsUbuntu"
[fedora]="depsFedora"
[arch]="depsArch"
[alpine]="depsAlpine"
[opensuse]="depsOpensuse"
)
# INFO:
# ╭────────────────────────────────────────────────────────────────╮
# │ This variable stores the packages you provided for each distro │
# ╰────────────────────────────────────────────────────────────────╯
declare -n pkgArray="${deps[$distro]}"
case "$distro" in
debian | ubuntu | arch | fedora | alpine | opensuse)
checkAndInstall "${generalDeps[@]}"
checkAndInstall "${pkgArray[@]}"
;;
*)
echo_error "There are no dependencies to install for $distro"
return 69
;;
esac
}
evalCargo() {
if [ -e "$HOME/.cargo/env" ]; then
echo_note "Using $HOME/.cargo/env.."
. "$HOME/.cargo/env"
fi
}
i_yazi() {
evalCargo
if command_exists cargo; then
echo_info "Installing yazi through cargo"
elif command_exists curl; then
echo_note "no cargo found, using curl to install rustup"
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
evalCargo
run rustup update
echo_info "Installing yazi through cargo"
else
echo_warning "neither cargo, nor curl were found. Cannot continue!"
return 69
fi
run cargo install --locked yazi-fm yazi-cli
}
yazi_setup() {
run ya pack -u || run ya pack -i
}
c_yazi() {
if [ -e "$HOME/.config/yazi/package.toml" ]; then
if command_exists ya; then
yazi_setup || echo_error "Yazi was not setup properly.."
else
if [ -e "$HOME/.cargo/env" ]; then
echo_info "Loadet $HOME/.cargo/env"
. "$HOME/.cargo/env"
yazi_setup || echo_error "Yazi was not setup properly.."
else
echo_error "Something went wrong when installing yazi.. ya is not available.."
fi
fi
else
echo_warning "There was no yazi config found.. "
fi
}
main() {
if $silent; then
echo_warning "Executing script silently!"
fi
case "$distro" in
arch)
_install yazi
;;
debian | ubuntu | fedora)
if getDependencies; then
i_yazi &&
c_yazi
fi
;;
*)
echo_warning "$distro is not compatible with this script"
;;
esac
}
if getImports; then
main
fi
}