setup_env/_lib.sh

98 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
# Shared functions sourced by setup_env.sh and sync.sh
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Back up and create symlink — skips if already pointing at the right target
create_symlink() {
local target=$1
local link_name=$2
if [[ -L $link_name && "$(readlink "$link_name")" == "$target" ]]; then
return
fi
if [[ -L $link_name || -f $link_name ]]; then
echo "Backing up existing $link_name to ${link_name}.backup"
mv "$link_name" "${link_name}.backup"
fi
echo "Creating symlink: $link_name -> $target"
ln -s "$target" "$link_name"
}
# Like create_symlink, but migrates an existing real file into the repo first,
# or bootstraps from the example file if neither exists.
migrate_and_link() {
local target=$1
local link_name=$2
if [[ ! -e "$target" ]]; then
if [[ -f "$link_name" && ! -L "$link_name" ]]; then
echo "Migrating existing $link_name -> $target"
cp "$link_name" "$target"
elif [[ -f "${target}_example" ]]; then
echo "Bootstrapping $target from example (fill in real values)"
cp "${target}_example" "$target"
fi
fi
create_symlink "$target" "$link_name"
}
deploy_configs() {
echo "Deploying configuration files as symlinks..."
local config_dir="$SCRIPT_DIR/dotfiles"
for config in .bashrc .vimrc .gitconfig .pspgconf .psqlrc .tmux.conf; do
create_symlink "$config_dir/$config" ~/"$config"
done
for config in .bashrc_local .bashrc_paths; do
migrate_and_link "$config_dir/$config" ~/"$config"
done
echo "Sourcing .bashrc..."
source ~/.bashrc
}
deploy_bin() {
echo "Deploying scripts to ~/.local/bin/ ..."
local src_dir="$SCRIPT_DIR/dotfiles/bin"
[[ -d "$src_dir" ]] || return 0
mkdir -p ~/.local/bin
for script in "$src_dir"/*; do
[[ -f "$script" ]] || continue
create_symlink "$script" ~/.local/bin/"$(basename "$script")"
done
}
deploy_nvim() {
echo "Deploying nvim lua modules to ~/.config/nvim/lua/ ..."
local src_dir="$SCRIPT_DIR/dotfiles/nvim"
[[ -d "$src_dir" ]] || return 0
if [[ ! -d ~/.config/nvim/lua ]]; then
echo " ~/.config/nvim/lua missing — run install_neovim.sh + install_nvchad.sh first. Skipping."
return 0
fi
for mod in "$src_dir"/*.lua; do
[[ -f "$mod" ]] || continue
create_symlink "$mod" ~/.config/nvim/lua/"$(basename "$mod")"
done
if [[ -d "$src_dir/plugins" ]]; then
mkdir -p ~/.config/nvim/lua/plugins
for mod in "$src_dir/plugins"/*.lua; do
[[ -f "$mod" ]] || continue
create_symlink "$mod" ~/.config/nvim/lua/plugins/"$(basename "$mod")"
done
fi
if [[ -d "$src_dir/queries" ]]; then
find "$src_dir/queries" -name "*.scm" | while read -r qfile; do
local rel="${qfile#"$src_dir/queries/"}"
local dest=~/.config/nvim/queries/"$rel"
mkdir -p "$(dirname "$dest")"
create_symlink "$qfile" "$dest"
done
fi
}