240 lines
5.9 KiB
Bash
Executable File
240 lines
5.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -e
|
|
|
|
# ====================
|
|
# Logging (early definitions for clone step)
|
|
# ====================
|
|
log_info() { echo "[INFO] $*"; }
|
|
log_warn() { echo "[WARN] $*"; }
|
|
log_error() { echo "[ERROR] $*" >&2; }
|
|
|
|
# ====================
|
|
# Detect installation method and setup DOTFILES_DIR
|
|
# ====================
|
|
if [ -f "$(dirname "$0")/zsh/zshrc" ]; then
|
|
# Running from cloned repo
|
|
DOTFILES_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
CLEANUP_DOTFILES=false
|
|
log_info "Running from cloned repository at $DOTFILES_DIR"
|
|
else
|
|
# Running from curl, need to clone
|
|
DOTFILES_DIR="$HOME/.dotfiles-tmp"
|
|
CLEANUP_DOTFILES=true
|
|
|
|
log_info "Running from curl, cloning dotfiles repository..."
|
|
|
|
rm -rf "$DOTFILES_DIR"
|
|
git clone https://github.com/jjsalinas/dotfiles.git "$DOTFILES_DIR"
|
|
|
|
log_info "Repository cloned to temporary location: $DOTFILES_DIR"
|
|
fi
|
|
|
|
# ====================
|
|
# Defaults
|
|
# ====================
|
|
THEME="clean"
|
|
ADD_NVM=false
|
|
DRY_RUN=false
|
|
|
|
ZSH_DIR="$HOME/.zsh"
|
|
ZSHRC="$HOME/.zshrc"
|
|
ZSHRC_LOCAL="$HOME/.zshrc.local"
|
|
ZSH_CUSTOM="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}"
|
|
|
|
# ====================
|
|
# Helper functions
|
|
# ====================
|
|
run() {
|
|
if $DRY_RUN; then
|
|
log_info "[dry-run] $*"
|
|
else
|
|
eval "$@"
|
|
fi
|
|
}
|
|
|
|
# ====================
|
|
# Help
|
|
# ====================
|
|
print_help() {
|
|
cat << 'EOF'
|
|
Dotfiles installer (zsh-focused)
|
|
|
|
Usage:
|
|
./install.sh [options]
|
|
|
|
Options:
|
|
--theme <name> Oh My Zsh theme to use (default: clean)
|
|
--add-nvm Enable Node Version Manager config
|
|
--dry-run Show actions without making changes
|
|
--help Show this help and exit
|
|
|
|
What this does:
|
|
• Symlinks zsh configuration from this repo
|
|
• Installs required zsh plugins
|
|
• Enables syntax highlighting (green/red commands)
|
|
• Enables fzf history search
|
|
• Adds ergonomic keybindings
|
|
• Works on Ubuntu and Fedora
|
|
|
|
Examples:
|
|
./install.sh
|
|
./install.sh --theme robbyrussell
|
|
./install.sh --add-nvm
|
|
./install.sh --theme agnoster --add-nvm --dry-run
|
|
EOF
|
|
}
|
|
|
|
# ====================
|
|
# Argument parsing
|
|
# ====================
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--theme)
|
|
THEME="$2"
|
|
shift
|
|
;;
|
|
--add-nvm)
|
|
ADD_NVM=true
|
|
;;
|
|
--dry-run)
|
|
DRY_RUN=true
|
|
;;
|
|
--help)
|
|
print_help
|
|
exit 0
|
|
;;
|
|
*)
|
|
log_error "Unknown option: $1"
|
|
exit 1
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
# ====================
|
|
# Preconditions
|
|
# ====================
|
|
if [ ! -d "$HOME/.oh-my-zsh" ]; then
|
|
log_error "Oh My Zsh is not installed. Install it first."
|
|
exit 1
|
|
fi
|
|
|
|
# ====================
|
|
# OS detection
|
|
# ====================
|
|
log_info "Detecting OS"
|
|
if command -v apt >/dev/null 2>&1; then
|
|
PKG_INSTALL="sudo apt install -y"
|
|
elif command -v dnf >/dev/null 2>&1; then
|
|
PKG_INSTALL="sudo dnf install -y"
|
|
else
|
|
PKG_INSTALL=""
|
|
fi
|
|
|
|
# ====================
|
|
# Dependencies
|
|
# ====================
|
|
log_info "Installing dependencies"
|
|
if [ -n "$PKG_INSTALL" ]; then
|
|
command -v git >/dev/null 2>&1 || run "$PKG_INSTALL git"
|
|
command -v fzf >/dev/null 2>&1 || run "$PKG_INSTALL fzf"
|
|
fi
|
|
|
|
# ====================
|
|
# Plugins
|
|
# ====================
|
|
log_info "Installing zsh-syntax-highlighting"
|
|
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" ]; then
|
|
run "git clone https://github.com/zsh-users/zsh-syntax-highlighting \
|
|
'$ZSH_CUSTOM/plugins/zsh-syntax-highlighting'"
|
|
else
|
|
log_warn "zsh-syntax-highlighting already installed"
|
|
fi
|
|
|
|
log_info "Installing zsh-autosuggestions (disabled by default)"
|
|
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-autosuggestions" ]; then
|
|
run "git clone https://github.com/zsh-users/zsh-autosuggestions \
|
|
'$ZSH_CUSTOM/plugins/zsh-autosuggestions'"
|
|
else
|
|
log_warn "zsh-autosuggestions already installed"
|
|
fi
|
|
|
|
# ====================
|
|
# Backup existing files
|
|
# ====================
|
|
if [ -f "$ZSHRC" ] && [ ! -L "$ZSHRC" ]; then
|
|
log_info "Backing up existing .zshrc"
|
|
run "cp '$ZSHRC' '$ZSHRC.backup.$(date +%s)'"
|
|
fi
|
|
|
|
# ====================
|
|
# Copy or Symlink files
|
|
# ====================
|
|
log_info "Creating zsh config directory"
|
|
run "mkdir -p '$ZSH_DIR'"
|
|
|
|
if $CLEANUP_DOTFILES; then
|
|
# Running from curl - copy files instead of symlinking
|
|
log_info "Copying zsh config files"
|
|
run "cp '$DOTFILES_DIR/zsh/zshrc' '$ZSHRC'"
|
|
run "cp '$DOTFILES_DIR/zsh/keybindings.zsh' '$ZSH_DIR/keybindings.zsh'"
|
|
run "cp '$DOTFILES_DIR/zsh/history.zsh' '$ZSH_DIR/history.zsh'"
|
|
run "cp '$DOTFILES_DIR/zsh/fzf.zsh' '$ZSH_DIR/fzf.zsh'"
|
|
run "cp '$DOTFILES_DIR/zsh/plugins.zsh' '$ZSH_DIR/plugins.zsh'"
|
|
|
|
if $ADD_NVM; then
|
|
log_info "Copying NVM config"
|
|
run "cp '$DOTFILES_DIR/zsh/nvm.zsh' '$ZSH_DIR/nvm.zsh'"
|
|
else
|
|
run "rm -f '$ZSH_DIR/nvm.zsh' || true"
|
|
fi
|
|
else
|
|
# Running from cloned repo - use symlinks for easier updates
|
|
log_info "Linking zsh config files"
|
|
run "ln -sf '$DOTFILES_DIR/zsh/zshrc' '$ZSHRC'"
|
|
run "ln -sf '$DOTFILES_DIR/zsh/keybindings.zsh' '$ZSH_DIR/keybindings.zsh'"
|
|
run "ln -sf '$DOTFILES_DIR/zsh/history.zsh' '$ZSH_DIR/history.zsh'"
|
|
run "ln -sf '$DOTFILES_DIR/zsh/fzf.zsh' '$ZSH_DIR/fzf.zsh'"
|
|
run "ln -sf '$DOTFILES_DIR/zsh/plugins.zsh' '$ZSH_DIR/plugins.zsh'"
|
|
|
|
if $ADD_NVM; then
|
|
log_info "Linking NVM config"
|
|
run "ln -sf '$DOTFILES_DIR/zsh/nvm.zsh' '$ZSH_DIR/nvm.zsh'"
|
|
else
|
|
run "rm -f '$ZSH_DIR/nvm.zsh' || true"
|
|
fi
|
|
fi
|
|
|
|
# ====================
|
|
# Theme handling
|
|
# ====================
|
|
log_info "Setting theme: $THEME"
|
|
if ! $DRY_RUN; then
|
|
cat << EOF > "$ZSHRC_LOCAL"
|
|
export ZSH_THEME="$THEME"
|
|
EOF
|
|
else
|
|
log_info "[dry-run] would write ~/.zshrc.local with theme $THEME"
|
|
fi
|
|
|
|
# ====================
|
|
# Cleanup
|
|
# ====================
|
|
if $CLEANUP_DOTFILES; then
|
|
log_info "Cleaning up temporary files"
|
|
if ! $DRY_RUN; then
|
|
rm -rf "$DOTFILES_DIR"
|
|
else
|
|
log_info "[dry-run] would remove $DOTFILES_DIR"
|
|
fi
|
|
fi
|
|
|
|
# ====================
|
|
# Done
|
|
# ====================
|
|
log_info "Dotfiles installation complete"
|
|
log_info "Open a new terminal or run: exec zsh"
|
|
if $DRY_RUN; then
|
|
log_warn "Dry-run mode enabled — no changes were made"
|
|
fi
|