split into setup_env.sh (fresh install) and sync.sh (day-to-day sync)

This commit is contained in:
Paul Trowbridge 2026-05-10 03:13:30 -04:00
parent d5bc117023
commit 8931988ec7
3 changed files with 122 additions and 120 deletions

82
_lib.sh Executable file
View File

@ -0,0 +1,82 @@
#!/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
}

View File

@ -1,6 +1,9 @@
#!/bin/bash #!/bin/bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "$SCRIPT_DIR/_lib.sh"
# Function to install packages
install_packages() { install_packages() {
local packages=(tmux vim git pspg bat fzf ripgrep) local packages=(tmux vim git pspg bat fzf ripgrep)
local missing=() local missing=()
@ -16,156 +19,45 @@ install_packages() {
sudo apt install -y "${missing[@]}" sudo apt install -y "${missing[@]}"
} }
# Install Tmux Plugin Manager (TPM) if not already installed
install_tpm() { install_tpm() {
if [[ ! -d ~/.tmux/plugins/tpm ]]; then if [[ ! -d ~/.tmux/plugins/tpm ]]; then
echo "Installing Tmux Plugin Manager..." echo "Installing Tmux Plugin Manager..."
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
else else
echo "Tmux Plugin Manager already installed, skipping..." echo "Tmux Plugin Manager already installed, skipping."
fi fi
} }
# Install Vundle (Vim plugin manager) if not already installed
install_vundle() { install_vundle() {
if [[ ! -d ~/.vim/bundle/Vundle.vim ]]; then if [[ ! -d ~/.vim/bundle/Vundle.vim ]]; then
echo "Installing Vundle for Vim..." echo "Installing Vundle for Vim..."
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
else else
echo "Vundle already installed, skipping..." echo "Vundle already installed, skipping."
fi fi
} }
# Install git-bash-prompt if not already installed
install_git_bash_prompt() { install_git_bash_prompt() {
if [[ ! -d ~/.bash-git-prompt ]]; then if [[ ! -d ~/.bash-git-prompt ]]; then
echo "Installing git-bash-prompt..." echo "Installing git-bash-prompt..."
git clone https://github.com/magicmonty/bash-git-prompt.git ~/.bash-git-prompt --depth=1 git clone https://github.com/magicmonty/bash-git-prompt.git ~/.bash-git-prompt --depth=1
else else
echo "git-bash-prompt already installed, skipping..." echo "git-bash-prompt already installed, skipping."
fi fi
} }
# 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 if the dotfiles/ target doesn't exist yet and the
# home file is a real file (not a symlink), migrate it into the repo first.
# Used for gitignored files (.bashrc_local, .bashrc_paths) so existing machine
# config is preserved rather than buried in a .backup file.
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 configuration files as symlinks
deploy_configs() {
echo "Deploying configuration files as symlinks..."
CONFIG_DIR="$(pwd)/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 executable scripts from dotfiles/bin/ into ~/.local/bin/
deploy_bin() {
echo "Deploying scripts to ~/.local/bin/ ..."
local src_dir="$(pwd)/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 lua modules from dotfiles/nvim/ into ~/.config/nvim/lua/
# NOTE: requires ~/.config/nvim/ to already exist (see install_neovim.sh + install_nvchad.sh).
# Modules are required by name from ~/.config/nvim/lua/mappings.lua — e.g. require("td_mappings").
deploy_nvim() {
echo "Deploying nvim lua modules to ~/.config/nvim/lua/ ..."
local src_dir="$(pwd)/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
}
# Pull latest from remote and re-exec if the script itself changed
self_update() {
if ! git -C "$(pwd)" rev-parse --is-inside-work-tree &>/dev/null; then
echo "Warning: not a git repo, skipping self-update"
return
fi
echo "Pulling latest from remote..."
local before after
before=$(git rev-parse HEAD)
git pull
after=$(git rev-parse HEAD)
if [ "$before" != "$after" ]; then
echo "setup_env.sh updated — re-running with latest version..."
exec "$0" "$@"
fi
}
# Main script
main() { main() {
self_update "$@"
install_packages install_packages
if [ -f "$(pwd)/install_neovim.sh" ]; then bash "$SCRIPT_DIR/install_neovim.sh"
bash "$(pwd)/install_neovim.sh"
fi
install_tpm install_tpm
install_vundle install_vundle
install_git_bash_prompt install_git_bash_prompt
deploy_configs deploy_configs
deploy_bin deploy_bin
if [ -f "$(pwd)/install_nvchad.sh" ]; then bash "$SCRIPT_DIR/install_nvchad.sh"
bash "$(pwd)/install_nvchad.sh"
fi
deploy_nvim deploy_nvim
echo "Setup complete! Please restart your shell or run 'source ~/.bashrc' for changes to take effect." echo ""
echo "Setup complete! Restart your shell or run 'source ~/.bashrc'."
} }
main "$@" main "$@"

30
sync.sh
View File

@ -1 +1,29 @@
rsync -azv -e ssh -r /storage/emulated/0/DCIM/Camera pt@hptrow.me:/home/pt/pics #!/bin/bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "$SCRIPT_DIR/_lib.sh"
self_update() {
echo "Pulling latest setup_env..."
local before after
before=$(git -C "$SCRIPT_DIR" rev-parse HEAD)
git -C "$SCRIPT_DIR" pull
after=$(git -C "$SCRIPT_DIR" rev-parse HEAD)
if [ "$before" != "$after" ]; then
echo "sync.sh updated — re-running with latest version..."
exec "$0" "$@"
fi
}
main() {
self_update "$@"
deploy_configs
deploy_bin
bash "$SCRIPT_DIR/install_nvchad.sh"
deploy_nvim
echo ""
echo "Sync complete! Restart your shell or run 'source ~/.bashrc'."
}
main "$@"