Skip to content

Instantly share code, notes, and snippets.

@AVGVSTVS96
Last active September 7, 2024 21:15
Show Gist options
  • Select an option

  • Save AVGVSTVS96/3e202a6b8fe7d9a057aefef4a2a22ba9 to your computer and use it in GitHub Desktop.

Select an option

Save AVGVSTVS96/3e202a6b8fe7d9a057aefef4a2a22ba9 to your computer and use it in GitHub Desktop.
setup.sh
#!/bin/bash
set -uo pipefail
# ------------------------
# --- Helper Functions ---
# ------------------------
log() {
echo ""
echo "$1"
echo ""
}
warn() {
echo "Warning: $1" >&2
}
error() {
echo "Error: $1" >&2
}
critical_error() {
error "$1"
exit 1
}
# -----------------
# --- Detect OS ---
# -----------------
OS_TYPE="$(uname)"
if [[ "$OS_TYPE" != "Darwin" && "$OS_TYPE" != "Linux" ]]; then
critical_error "Unsupported operating system: $OS_TYPE"
fi
# -----------------------------
# --- Homebrew Installation ---
# -----------------------------
log "Checking for Homebrew installation..."
if ! command -v brew &> /dev/null; then
log "Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || critical_error "Failed to install Homebrew"
log "Homebrew installed successfully."
else
log "Homebrew is already installed."
fi
# Determine Homebrew prefix
if [[ "$OS_TYPE" == "Darwin" ]]; then
BREW_PREFIX=$(/usr/local/bin/brew --prefix 2>/dev/null || /opt/homebrew/bin/brew --prefix)
elif [[ "$OS_TYPE" == "Linux" ]]; then
BREW_PREFIX="/home/linuxbrew/.linuxbrew"
fi
BREW_PATH="$BREW_PREFIX/bin/brew"
# -----------------------------
# --- Add Brew to .zprofile ---
# -----------------------------
log "Ensuring Homebrew is initialized in .zprofile..."
BREW_INIT="eval \"\$($BREW_PATH shellenv)\""
if ! grep -q "$BREW_PATH" "$HOME/.zprofile"; then
echo "$BREW_INIT" >> "$HOME/.zprofile" || warn "Failed to add Homebrew initialization to .zprofile"
log "Homebrew initialization added to .zprofile."
else
log "Homebrew is already initialized in .zprofile."
fi
log "Sourcing .zprofile to ensure Homebrew is available..."
source "$HOME/.zprofile" || warn "Failed to source .zprofile"
log "Initializing Homebrew environment..."
if [[ -x "$BREW_PATH" ]]; then
eval "$($BREW_PATH shellenv)" || critical_error "Failed to initialize Homebrew environment"
log "Homebrew environment initialized successfully."
else
critical_error "Homebrew executable not found at $BREW_PATH"
fi
# --------------------
# --- Install Stow ---
# --------------------
log "Installing stow via Homebrew..."
brew install stow || critical_error "Failed to install stow via Homebrew"
# ---------------------------
# --- Clone Dotfiles Repo ---
# ---------------------------
log "Checking if dotfiles repository is already cloned..."
if [[ -d "$HOME/dotfiles/.git" ]]; then
log "Dotfiles repository already exists. Skipping clone."
else
log "Cloning dotfiles repository into $HOME..."
git clone https://github.com/avgvstvs96/dotfiles.git "$HOME/dotfiles" || critical_error "Failed to clone dotfiles repository"
fi
# ---------------------
# --- Stow Dotfiles ---
# ---------------------
log "Changing to the dotfiles directory and stowing from there..."
cd "$HOME/dotfiles" || critical_error "Failed to change to dotfiles directory"
stow_dirs=("bat" "brew" "completions" "fish" "gh" "gh-dash" "git" "graphite" "lazygit" "nvim" "scripts" "wezterm" "yazi" "zed" "zsh")
for dir in "${stow_dirs[@]}"; do
if [[ -L "$HOME/.$dir" || -d "$HOME/.$dir" ]]; then
log "Directory $dir is already stowed. Skipping..."
else
log "Stowing $dir from $HOME/dotfiles..."
stow "$dir" || warn "Failed to stow $dir"
fi
done
# ---------------------------------
# --- Install Homebrew Packages ---
# ---------------------------------
log "Installing Homebrew packages from Brewfile..."
if [[ -f "$HOME/dotfiles/brew/Brewfile" ]]; then
brew bundle --file="$HOME/dotfiles/brew/Brewfile" || warn "Failed to install some Homebrew packages"
else
warn "Brewfile not found. Skipping Homebrew package installation..."
fi
# ------------------------------
# --- Install NVM if Missing ---
# ------------------------------
log "Checking for NVM installation..."
if [[ ! -d "${NVM_DIR:-$HOME/.nvm}" ]]; then
log "Installing NVM..."
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash || warn "Failed to install NVM"
fi
# ---------------------------
# --- Configure bat Theme ---
# ---------------------------
log "Configuring bat theme..."
BAT_THEME_DIR="$(bat --config-dir)/themes"
if [[ ! -f "$BAT_THEME_DIR/tokyonight_night.tmTheme" ]]; then
log "Installing bat theme..."
mkdir -p "$BAT_THEME_DIR"
curl -o "$BAT_THEME_DIR/tokyonight_night.tmTheme" https://raw.githubusercontent.com/folke/tokyonight.nvim/main/extras/sublime/tokyonight_night.tmTheme || warn "Failed to download bat theme"
bat cache --build || warn "Failed to build bat cache"
fi
log "Dotfiles setup complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment