dotfiles/install

310 lines
7.6 KiB
Bash
Executable File

#!/bin/bash
set -ueo pipefail
shopt -s nullglob
readonly TARGET_PATH="$HOME"
readonly SUB="$(pwd)/home/user"
declare -A TARGETS=(
["bash"]=".config/bash .bashrc .profile"
["zsh"]="%bash .config/zsh .zshrc .zprofile .zlogout .inputrc"
["tmux"]=".tmux.conf"
["alacritty"]=".config/alacritty"
["nvim"]=".config/nvim .local/bin/vim_askpass_helper .local/bin/vim_askpass_helper_python"
["ssh"]=""
["git"]=".config/git"
["ranger"]=".config/ranger"
["gpg"]=""
["i3"]=".xinitrc .config/i3 .config/i3status .local/bin/i3status_wrapper .config/picom .local/bin/slm"
["bat"]=".config/bat"
["font"]=""
["termux"]=""
["arch"]=""
["psql"]=".psqlrc"
["docker"]=".docker/cli-plugins"
["ipython"]=".ipython/profile_default/ipython_config.py"
)
_die() {
echo "$0: $1" >&2
exit "$2"
}
test "$0" = "./install" || _die "Error: Please, run this file from root of dotfiles, like this './install.sh TARGET" 1
_die_if_installed() {
if [ -e "$1" ]; then
_die "Already installed" 1
fi
}
_link_files_in_sandbox() {
local targetfile
for targetfile in "$@"
do
echo "installing: $targetfile"
if [ "${targetfile::1}" = "%" ]; then
_link_files_in_sandbox ${TARGETS["${targetfile:1}"]}
else
if [ ! "$(dirname "$targetfile")" = "." ]; then
mkdir -p "$SANDBOX_PATH/$(dirname "$targetfile")"
fi
ln -sT "$SUB/$targetfile" "$SANDBOX_PATH/$targetfile"
fi
done
}
_compare_sandbox_to_home() {
local comparisons
comparisons="$(diff -rq "$SANDBOX_PATH" "$TARGET_PATH")"
echo "$comparisons" | grep -vE "^Only in .+"
}
_merge_sandbox_to_home() {
set +e
cp -RTnP "$SANDBOX_PATH" "$TARGET_PATH"
set -e
}
__install_from_sandbox() {
local comparisons="$(_compare_sandbox_to_home)"
if [ -n "$comparisons" ]; then
echo "$comparisons" >&2
echo "Reverting..." >&2
_die "Found conflicting files. Exiting" 1
fi
echo "Merging to home..."
_merge_sandbox_to_home
echo "Successfully installed"
}
cmd_bash() {
_link_files_in_sandbox ${TARGETS["bash"]}
__install_from_sandbox
}
cmd_zsh() {
_link_files_in_sandbox ${TARGETS["zsh"]}
__install_from_sandbox
}
cmd_tmux() {
_link_files_in_sandbox ${TARGETS["tmux"]}
__install_from_sandbox
}
cmd_alacritty() {
_link_files_in_sandbox ${TARGETS["alacritty"]}
__install_from_sandbox
}
cmd_nvim() {
echo "sudo pacman -S npm ctags fzf glow; mkdir ~/.npm-global; npm config set prefix '~/.npm-global'"
_link_files_in_sandbox ${TARGETS["nvim"]}
__install_from_sandbox
}
cmd_ssh() {
cat "$SUB/.ssh/config" >> "$HOME/.ssh/config"
}
cmd_git() {
_link_files_in_sandbox ${TARGETS["git"]}
__install_from_sandbox
}
cmd_ranger() {
echo "sudo pacman -S highlight ttf-joypixels noto-fonts-emoji ueberzug poppler"
_link_files_in_sandbox ${TARGETS["ranger"]}
mkdir -p "$SANDBOX_PATH/.config/ranger/plugins"
git clone https://github.com/alexanderjeurissen/ranger_devicons "$SANDBOX_PATH/.config/ranger/plugins/ranger_devicons"
__install_from_sandbox
ranger --copy-config=all
}
cmd_gpg() {
_die_if_installed "$HOME/.gnupg"
cat "$SUB/.gnupg/gpg.conf" >> "$HOME/.gnupg/gpg.conf"
echo -e "default-cache-ttl 1\nmax-cache-ttl 1" > "$HOME/.gnupg/gpg-agent.conf"; echo RELOADAGENT | gpg-connect-agent
}
cmd_i3() {
echo "sudo pacman -S nitrogen picom compton ttf-font-awesome xdotool xclip maim playerctl"
_link_files_in_sandbox ${TARGETS["i3"]}
__install_from_sandbox
}
cmd_bat() {
_link_files_in_sandbox ${TARGETS["bat"]}
__install_from_sandbox
}
cmd_ipython() {
_link_files_in_sandbox ${TARGETS["ipython"]}
__install_from_sandbox
}
cmd_font() {
local sub
sub="$HOME/.local/share/fonts"
mkdir -p "$sub"
cd "$sub"
wget 'https://github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/FiraCode.zip'
unzip FiraCode.zip -d "$sub"
git clone 'https://github.com/powerline/fonts.git' --depth=1
cd fonts
./install.sh
}
cmd_termux() {
echo "apt install termux-api tsu"
}
cmd_arch() {
echo 'echo "ParallelDownloads = 5" >> /etc/pacman.conf'
echo 'sudo systemctl enable --now gpm'
}
cmd_psql() {
_link_files_in_sandbox ${TARGETS["psql"]}
__install_from_sandbox
}
cmd_docker() {
_link_files_in_sandbox ${TARGETS["docker"]}
__install_from_sandbox
}
is_target_installed() {
local not_fully_installed=0
local targetfile
for targetfile in ${TARGETS["$1"]}
do
if [ "${targetfile::1}" = "%" ]; then
is_target_installed "${targetfile:1}" || not_fully_installed=1
else
if [ ! -e "$TARGET_PATH/$targetfile" ]; then
echo "$targetfile not linked"
not_fully_installed=1
fi
fi
done
if (( not_fully_installed != 0 )); then
echo "Target '$1' not fully installed"
echo
return 1
fi
return 0
}
find_targets_that_depend_on() {
local target
for target in "${!TARGETS[@]}"
do
if [[ " ${TARGETS["$target"]} " =~ %$1 ]]; then
echo "$target"
fi
done
}
die_if_installed_targets_depend_on() {
for reverse_dependecy in $(find_targets_that_depend_on "$1")
do
if is_target_installed "$reverse_dependecy" >/dev/null; then
_die "target '$reverse_dependecy' is depends on installed target '$1'. Exiting..." 1
fi
done
}
cmd_unlink() {
local target targetfile
for target in "$@"
do
die_if_installed_targets_depend_on "$target"
for targetfile in ${TARGETS["$target"]}
do
if [ "${targetfile::1}" = "%" ]; then
continue
fi
if [ -e "$TARGET_PATH/$targetfile" ]; then
unlink "$TARGET_PATH/$targetfile"
fi
done
done
}
cmd_no_target() {
_die "TARGET not exists" 1
}
cmd_list() {
echo "${!TARGETS[@]}"
}
cmd_install() {
local target
for target in "$@"
do
SANDBOX_PATH="$(mktemp -td "${USER:=user}.dotfiles_XXXXXXX")"
case "$target" in
bash) shift; cmd_bash "$@" ;;
zsh) shift; cmd_zsh "$@" ;;
tmux) shift; cmd_tmux "$@" ;;
alacritty) shift; cmd_alacritty "$@" ;;
nvim) shift; cmd_nvim "$@" ;;
ssh) shift; cmd_ssh "$@" ;;
git) shift; cmd_git "$@" ;;
ranger) shift; cmd_ranger "$@" ;;
gpg) shift; cmd_gpg "$@" ;;
i3) shift; cmd_i3 "$@" ;;
bat) shift; cmd_bat "$@" ;;
font) shift; cmd_font "$@" ;;
termux) shift; cmd_termux "$@" ;;
arch) shift; cmd_arch "$@" ;;
psql) shift; cmd_psql "$@" ;;
docker) shift; cmd_docker "$@" ;;
ipython) shift; cmd_ipython "$@" ;;
*) shift; cmd_no_target "$@" ;;
esac
rm -rf "$SANDBOX_PATH"
done
}
cmd_help() {
echo "Dotfiles installation script:
Usage: ./install TARGET...
Usage: ./install unlink TARGET...
Usage: ./install check TARGET
Usage: ./install list"
}
unset executed_command
readonly executed_command="$1"
case "$executed_command" in
unlink) shift; cmd_unlink "$@" ;;
check) shift; is_target_installed "$@" ;;
list) shift; cmd_list "$@" ;;
help) shift; cmd_help "$@" ;;
*) shift; cmd_install "$executed_command" "$@" ;;
esac
exit 0