Files
shell/setup/setup.sh
Peter Wood 0123fc6007 feat: Add comprehensive Plex recovery validation script
- Introduced `validate-plex-recovery.sh` for validating Plex database recovery.
- Implemented checks for service status, database integrity, web interface accessibility, API functionality, and recent logs.
- Added detailed recovery summary and next steps for users.

fix: Improve Debian patching script for compatibility

- Enhanced `debian-patches.sh` to securely download and execute bootstrap scripts.
- Updated package mapping logic and ensured proper permissions for patched files.

fix: Update Docker test scripts for better permission handling

- Modified `run-docker-tests.sh` to set appropriate permissions on logs directory.
- Ensured log files have correct permissions after test runs.

fix: Enhance setup scripts for secure installations

- Updated `setup.sh` to securely download and execute installation scripts for zoxide and nvm.
- Improved error handling for failed downloads.

fix: Refine startup script for log directory permissions

- Adjusted `startup.sh` to set proper permissions for log directories and files.

chore: Revamp update-containers.sh for better error handling and logging

- Rewrote `update-containers.sh` to include detailed logging and error handling.
- Added validation for Docker image names and improved overall script robustness.
2025-06-05 07:22:28 -04:00

833 lines
33 KiB
Bash
Executable File

#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DOTFILES_DIR="$(dirname "$SCRIPT_DIR")"
DOTFILES_SUBDIR="$DOTFILES_DIR/dotfiles"
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${GREEN}Starting system setup...${NC}"
# Detect OS
if [ -f /etc/os-release ]; then
. /etc/os-release
OS_NAME=$ID
OS_VERSION=$VERSION_ID
else
echo -e "${YELLOW}Unable to detect OS, assuming Debian/Ubuntu...${NC}"
OS_NAME="ubuntu"
OS_VERSION="22.04"
fi
echo -e "${GREEN}Detected OS: ${OS_NAME} ${OS_VERSION}${NC}"
# Function to determine the package manager to use
determine_pkg_manager() {
if command -v nala &> /dev/null; then
echo "nala"
elif [ "$OS_NAME" = "fedora" ]; then
echo "dnf"
else
echo "apt"
fi
}
# Set up package management based on OS
if [ "$OS_NAME" = "fedora" ]; then
echo -e "${YELLOW}Setting up Fedora repositories and package management...${NC}"
# Install prerequisites for Fedora
sudo dnf install -y wget gpg
# Setup VS Code repository for Fedora
echo -e "${YELLOW}Setting up VS Code repository for Fedora...${NC}"
sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'
# Setup GitHub CLI repository for Fedora
echo -e "${YELLOW}Setting up GitHub CLI repository for Fedora...${NC}"
sudo dnf install -y 'dnf-command(config-manager)'
# Use a different approach to add the GitHub CLI repo to avoid the "--add-repo" error
sudo curl -fsSL https://cli.github.com/packages/rpm/gh-cli.repo -o /etc/yum.repos.d/gh-cli.repo
# Setup COPR repository for eza
echo -e "${YELLOW}Setting up COPR repository for eza...${NC}"
sudo dnf copr enable -y alternateved/eza
# Setup COPR repository for lazygit
echo -e "${YELLOW}Setting up COPR repository for lazygit...${NC}"
sudo dnf copr enable -y shaps/lazygit
# Update package lists
echo -e "${YELLOW}Updating package lists for Fedora...${NC}"
sudo dnf check-update -y || true
else
# Add apt repositories for Debian/Ubuntu
echo -e "${YELLOW}Setting up apt repositories...${NC}"
# Install prerequisites
sudo apt-get install -y wget gpg apt-transport-https
# Setup Nala repository - Try multiple methods
echo -e "${YELLOW}Setting up Nala repository...${NC}"
# First check if Nala is available in standard repositories (newer Ubuntu versions)
if apt-cache show nala &>/dev/null; then
echo -e "${GREEN}Nala is available in standard repositories${NC}"
else
echo -e "${YELLOW}Nala not found in standard repositories. Trying alternative installation methods...${NC}"
# Check Ubuntu version
if grep -q "noble\|lunar\|mantic\|jammy" /etc/os-release; then
echo -e "${GREEN}Ubuntu $(grep VERSION_CODENAME /etc/os-release | cut -d= -f2) detected. Ensuring universe repository is enabled...${NC}"
# Make sure universe repository is enabled
sudo apt-get update
sudo apt-get install -y software-properties-common
sudo add-apt-repository -y universe
else
# For older Ubuntu versions try to install directly
echo -e "${YELLOW}Older Ubuntu version detected. Trying to install Nala directly...${NC}"
sudo apt-get update
sudo apt-get install -y software-properties-common
fi
fi
# Setup eza repository for Ubuntu/Debian
echo -e "${YELLOW}Setting up eza repository...${NC}"
if ! apt-cache show eza &>/dev/null; then
# Add eza repository for older Ubuntu/Debian versions
echo -e "${YELLOW}Adding eza repository...${NC}"
sudo mkdir -p -m 755 /etc/apt/keyrings
wget -qO- https://raw.githubusercontent.com/eza-community/eza/main/deb.asc | sudo gpg --dearmor -o /etc/apt/keyrings/gierens.gpg
echo "deb [signed-by=/etc/apt/keyrings/gierens.gpg] http://deb.gierens.de stable main" | sudo tee /etc/apt/sources.list.d/gierens.list
sudo chmod 644 /etc/apt/keyrings/gierens.gpg /etc/apt/sources.list.d/gierens.list
else
echo -e "${GREEN}Eza is available in standard repositories${NC}"
fi
# Setup VS Code repository
echo -e "${YELLOW}Setting up VS Code repository...${NC}"
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > /tmp/packages.microsoft.gpg
sudo install -D -o root -g root -m 644 /tmp/packages.microsoft.gpg /etc/apt/keyrings/packages.microsoft.gpg
sudo sh -c 'echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" > /etc/apt/sources.list.d/vscode.list'
rm /tmp/packages.microsoft.gpg
# Setup GitHub CLI repository
echo -e "${YELLOW}Setting up GitHub CLI repository...${NC}"
sudo mkdir -p -m 755 /etc/apt/keyrings
out=$(mktemp) && wget -nv -O$out https://cli.github.com/packages/githubcli-archive-keyring.gpg \
&& sudo install -D -o root -g root -m 644 "$out" /etc/apt/keyrings/githubcli-archive-keyring.gpg \
&& rm "$out" \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
# Update package lists (ignoring previous errors)
echo -e "${YELLOW}Updating package lists...${NC}"
sudo apt update || echo -e "${YELLOW}Warning: apt update had some errors, but continuing...${NC}"
# Install Nala if not already installed
echo -e "${YELLOW}Checking for Nala package manager...${NC}"
if ! command -v nala &> /dev/null; then
echo -e "${YELLOW}Installing Nala package manager...${NC}"
if sudo apt-get install -y nala; then
echo -e "${GREEN}Nala installed successfully!${NC}"
# Update PKG_MANAGER since we now have nala
PKG_MANAGER="nala"
else
echo -e "${YELLOW}Failed to install Nala. Continuing with standard apt...${NC}"
fi
else
echo -e "${GREEN}Nala is already installed!${NC}"
fi
fi
# Determine which package manager to use
PKG_MANAGER=$(determine_pkg_manager)
echo -e "${GREEN}Using package manager: $PKG_MANAGER${NC}"
# Load packages from package list
mapfile -t pkgs < <(grep -v '^//' "$SCRIPT_DIR/packages.list" | grep -v -e '^$' | sed 's|//.*||' | awk '{print $1}' | grep -v '^$')
# Map Debian/Ubuntu package names to Fedora equivalents if needed
declare -A fedora_pkg_map
fedora_pkg_map["bat"]="bat"
fedora_pkg_map["fd-find"]="fd" # On Fedora, the package is called 'fd'
# eza is available from COPR repository
fedora_pkg_map["eza"]="eza"
# lazygit is available from COPR repository
fedora_pkg_map["lazygit"]="lazygit"
# lazydocker will be installed manually from GitHub releases
# Add more package mappings as needed
# Process the package list based on OS
install_pkg_list=()
special_installs=()
for pkg in "${pkgs[@]}"; do
# Skip nala package on non-Debian/Ubuntu systems
if [ "$pkg" = "nala" ] && [ "$OS_NAME" != "ubuntu" ] && [ "$OS_NAME" != "debian" ]; then
continue
fi
# Handle packages that need special installation
if [ "$pkg" = "lazydocker" ]; then
special_installs+=("$pkg")
continue
fi
# Handle fabric installation
if [ "$pkg" = "fabric" ]; then
special_installs+=("$pkg")
continue
fi
# Handle ollama Docker installation
if [ "$pkg" = "ollama" ]; then
special_installs+=("$pkg")
continue
fi
# Handle lazygit - available in COPR for Fedora, special install for Debian/Ubuntu
if [ "$pkg" = "lazygit" ] && [ "$OS_NAME" != "fedora" ]; then
special_installs+=("$pkg")
continue
fi
# Check if we need to map the package name for Fedora
if [ "$OS_NAME" = "fedora" ] && [[ -n "${fedora_pkg_map[$pkg]}" ]]; then
install_pkg_list+=("${fedora_pkg_map[$pkg]}")
else
install_pkg_list+=("$pkg")
fi
done
# Install packages using the determined package manager
echo -e "${YELLOW}Installing packages from packages.list with $PKG_MANAGER...${NC}"
case $PKG_MANAGER in
nala)
# Configure Nala mirrors
echo -e "${YELLOW}Configuring Nala mirrors...${NC}"
# First, remove any existing nala sources list to avoid issues with invalid mirrors
sudo rm -f /etc/apt/sources.list.d/nala-sources.list 2>/dev/null
# Try to fetch mirrors with less aggressive settings
echo -e "${YELLOW}Attempting to find faster mirrors (this may fail and that's okay)...${NC}"
if ! sudo nala fetch --auto --fetches 1 --country auto 2>/dev/null; then
echo -e "${YELLOW}Note: Fast mirror selection failed, using default mirrors (this is normal and safe)${NC}"
# Remove any potentially corrupted Nala sources
sudo rm -f /etc/apt/sources.list.d/nala-sources.list 2>/dev/null
else
echo -e "${GREEN}Fast mirrors configured successfully!${NC}"
fi
# Install packages using Nala
sudo nala install -y "${install_pkg_list[@]}" || {
echo -e "${YELLOW}Nala install failed, falling back to apt...${NC}"
sudo apt-get install -y "${install_pkg_list[@]}"
}
;;
dnf)
# Install packages using DNF
sudo dnf install -y "${install_pkg_list[@]}"
;;
apt)
# Install packages using APT
sudo apt-get install -y "${install_pkg_list[@]}"
;;
esac
echo -e "${GREEN}Package installation completed for $OS_NAME $OS_VERSION.${NC}"
# Install Go if not present (required for Fabric and other Go tools)
echo -e "${YELLOW}Checking Go installation...${NC}"
if ! command -v go &> /dev/null; then
echo -e "${YELLOW}Installing Go programming language...${NC}"
GO_VERSION="1.21.5" # Stable version that works well with Fabric
# Download and install Go
wget -q "https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz" -O /tmp/go.tar.gz
# Remove any existing Go installation
sudo rm -rf /usr/local/go
# Extract Go to /usr/local
sudo tar -C /usr/local -xzf /tmp/go.tar.gz
rm /tmp/go.tar.gz
echo -e "${GREEN}Go ${GO_VERSION} installed successfully!${NC}"
echo -e "${YELLOW}Go PATH will be configured in shell configuration${NC}"
else
echo -e "${GREEN}Go is already installed: $(go version)${NC}"
fi
# Handle special installations that aren't available through package managers
echo -e "${YELLOW}Installing special packages...${NC}"
for pkg in "${special_installs[@]}"; do
case $pkg in
"lazydocker")
if ! command -v lazydocker &> /dev/null; then
echo -e "${YELLOW}Installing Lazydocker from GitHub releases...${NC}"
LAZYDOCKER_VERSION=$(curl -s "https://api.github.com/repos/jesseduffield/lazydocker/releases/latest" | grep -Po '"tag_name": "v\K[^"]*')
curl -Lo lazydocker.tar.gz "https://github.com/jesseduffield/lazydocker/releases/latest/download/lazydocker_${LAZYDOCKER_VERSION}_Linux_x86_64.tar.gz"
mkdir -p lazydocker-temp
tar xf lazydocker.tar.gz -C lazydocker-temp
sudo mv lazydocker-temp/lazydocker /usr/local/bin
rm -rf lazydocker-temp lazydocker.tar.gz
echo -e "${GREEN}Lazydocker installed successfully!${NC}"
else
echo -e "${GREEN}Lazydocker is already installed${NC}"
fi
;;
"fabric")
if ! command -v fabric &> /dev/null; then
echo -e "${YELLOW}Installing Fabric from GitHub releases...${NC}"
# Download and install the latest Fabric binary for Linux AMD64
curl -L https://github.com/danielmiessler/fabric/releases/latest/download/fabric-linux-amd64 -o /tmp/fabric
chmod +x /tmp/fabric
sudo mv /tmp/fabric /usr/local/bin/fabric
echo -e "${GREEN}Fabric binary installed successfully!${NC}"
# Verify installation
if fabric --version; then
echo -e "${GREEN}Fabric installation verified!${NC}"
echo -e "${YELLOW}Running Fabric setup...${NC}"
# Create fabric config directory
mkdir -p "$HOME/.config/fabric"
# Run fabric setup with proper configuration
echo -e "${YELLOW}Setting up Fabric patterns and configuration...${NC}"
# Initialize fabric with default patterns
fabric --setup || echo -e "${YELLOW}Initial fabric setup completed${NC}"
# Update patterns to get the latest
echo -e "${YELLOW}Updating Fabric patterns...${NC}"
fabric --updatepatterns || echo -e "${YELLOW}Pattern update completed${NC}"
# Configure Ollama as the default model provider
echo -e "${YELLOW}Configuring Fabric to use Ollama...${NC}"
# Create or update fabric config to use Ollama
cat > "$HOME/.config/fabric/.env" << 'FABRIC_EOF'
# Fabric Configuration for Ollama
DEFAULT_MODEL=phi3:mini
OLLAMA_API_BASE=http://localhost:11434
FABRIC_EOF
echo -e "${GREEN}Fabric setup completed successfully!${NC}"
echo -e "${YELLOW}Fabric is configured to use Ollama at http://localhost:11434${NC}"
echo -e "${YELLOW}Default model: phi3:mini${NC}"
echo -e "${YELLOW}You can test fabric with: fabric --list-patterns${NC}"
else
echo -e "${RED}Fabric installation verification failed${NC}"
fi
else
echo -e "${GREEN}Fabric is already installed${NC}"
# Still try to update patterns and ensure Ollama configuration
echo -e "${YELLOW}Updating Fabric patterns...${NC}"
fabric --updatepatterns || echo -e "${YELLOW}Pattern update completed${NC}"
# Ensure Ollama configuration exists
mkdir -p "$HOME/.config/fabric"
if [ ! -f "$HOME/.config/fabric/.env" ]; then
echo -e "${YELLOW}Creating Ollama configuration for existing Fabric installation...${NC}"
cat > "$HOME/.config/fabric/.env" << 'FABRIC_EOF'
# Fabric Configuration for Ollama
DEFAULT_MODEL=phi3:mini
OLLAMA_API_BASE=http://localhost:11434
FABRIC_EOF
fi
fi
;;
"ollama")
# Ollama installation is handled in the main Ollama Docker setup section below
echo -e "${YELLOW}Ollama Docker installation will be handled in dedicated section...${NC}"
;;
"lazygit")
if ! command -v lazygit &> /dev/null; then
echo -e "${YELLOW}Installing Lazygit from GitHub releases...${NC}"
LAZYGIT_VERSION=$(curl -s "https://api.github.com/repos/jesseduffield/lazygit/releases/latest" | grep -Po '"tag_name": "v\K[^"]*')
curl -Lo lazygit.tar.gz "https://github.com/jesseduffield/lazygit/releases/latest/download/lazygit_${LAZYGIT_VERSION}_Linux_x86_64.tar.gz"
mkdir -p lazygit-temp
tar xf lazygit.tar.gz -C lazygit-temp
sudo mv lazygit-temp/lazygit /usr/local/bin
rm -rf lazygit-temp lazygit.tar.gz
echo -e "${GREEN}Lazygit installed successfully!${NC}"
else
echo -e "${GREEN}Lazygit is already installed${NC}"
fi
;;
*)
echo -e "${YELLOW}Unknown special package: $pkg${NC}"
;;
esac
done
# Setup Ollama with Docker for local AI (required for Fabric)
if [ "${SKIP_OLLAMA:-false}" = "true" ]; then
echo -e "${YELLOW}Skipping Ollama installation (SKIP_OLLAMA=true)${NC}"
else
# Setup Ollama with Docker for local AI (required for Fabric)
echo -e "${YELLOW}Setting up Ollama with Docker for local AI support...${NC}"
# Check if user can run docker commands without sudo
if docker ps >/dev/null 2>&1; then
DOCKER_CMD="docker"
echo -e "${GREEN}Docker access confirmed without sudo${NC}"
else
echo -e "${YELLOW}Docker requires sudo access (group membership may need session refresh)${NC}"
DOCKER_CMD="sudo docker"
fi
# Check if Ollama Docker container is already running
if ! $DOCKER_CMD ps | grep -q ollama; then
echo -e "${YELLOW}Setting up Ollama Docker container...${NC}"
# Pull the Ollama Docker image
$DOCKER_CMD pull ollama/ollama:latest
# Create a Docker volume for Ollama data
$DOCKER_CMD volume create ollama-data 2>/dev/null || true
# Remove any existing ollama container
$DOCKER_CMD rm -f ollama 2>/dev/null || true
# Start Ollama container with GPU support (if available) or CPU-only
if command -v nvidia-docker &> /dev/null || $DOCKER_CMD info 2>/dev/null | grep -q nvidia; then
echo -e "${YELLOW}Starting Ollama with GPU support...${NC}"
$DOCKER_CMD run -d \
--name ollama \
--restart unless-stopped \
--gpus all \
-v ollama-data:/root/.ollama \
-p 11434:11434 \
ollama/ollama
else
echo -e "${YELLOW}Starting Ollama in CPU-only mode...${NC}"
$DOCKER_CMD run -d \
--name ollama \
--restart unless-stopped \
-v ollama-data:/root/.ollama \
-p 11434:11434 \
ollama/ollama
fi
# Wait for the container to be ready
echo -e "${YELLOW}Waiting for Ollama to start...${NC}"
sleep 10
# Install a lightweight model for basic functionality
echo -e "${YELLOW}Installing a basic AI model (phi3:mini)...${NC}"
$DOCKER_CMD exec ollama ollama pull phi3:mini
echo -e "${GREEN}Ollama Docker setup completed with phi3:mini model!${NC}"
echo -e "${YELLOW}Ollama is accessible at http://localhost:11434${NC}"
else
echo -e "${GREEN}Ollama Docker container is already running${NC}"
fi
# Add helper aliases for Ollama Docker management
OLLAMA_ALIASES_FILE="$HOME/.oh-my-zsh/custom/ollama-aliases.zsh"
echo -e "${YELLOW}Setting up Ollama Docker aliases...${NC}"
cat > "$OLLAMA_ALIASES_FILE" << 'EOF'
# Ollama Docker Management Aliases
alias ollama-start='docker start ollama'
alias ollama-stop='docker stop ollama'
alias ollama-restart='docker restart ollama'
alias ollama-logs='docker logs -f ollama'
alias ollama-shell='docker exec -it ollama /bin/bash'
alias ollama-pull='docker exec ollama ollama pull'
alias ollama-list='docker exec ollama ollama list'
alias ollama-run='docker exec ollama ollama run'
alias ollama-status='docker ps | grep ollama'
# Function to run ollama commands in Docker
ollama() {
if [ "$1" = "serve" ]; then
echo "Ollama is running in Docker. Use 'ollama-start' to start the container."
return 0
fi
# Check if user can run docker without sudo
if docker ps >/dev/null 2>&1; then
docker exec ollama ollama "$@"
else
sudo docker exec ollama ollama "$@"
fi
}
EOF
echo -e "${GREEN}Ollama Docker aliases created in $OLLAMA_ALIASES_FILE${NC}"
echo -e "${YELLOW}You can install additional models with: ollama pull <model-name>${NC}"
# Function to finalize Fabric configuration after Ollama is running
configure_fabric_for_ollama() {
echo -e "${YELLOW}Finalizing Fabric configuration for Ollama...${NC}"
# Ensure Ollama is accessible before configuring Fabric
local max_attempts=30
local attempt=0
while [ $attempt -lt $max_attempts ]; do
if curl -s http://localhost:11434/api/tags >/dev/null 2>&1; then
echo -e "${GREEN}Ollama API is accessible, configuring Fabric...${NC}"
break
fi
echo -e "${YELLOW}Waiting for Ollama to be ready... (attempt $((attempt + 1))/$max_attempts)${NC}"
sleep 2
attempt=$((attempt + 1))
done
if [ $attempt -eq $max_attempts ]; then
echo -e "${YELLOW}Warning: Ollama API not accessible, Fabric configuration may need manual setup${NC}"
return
fi
# Create a comprehensive Fabric configuration
mkdir -p "$HOME/.config/fabric"
# Create the main configuration file
cat > "$HOME/.config/fabric/config.yaml" << 'FABRIC_CONFIG_EOF'
# Fabric Configuration for Ollama Integration
model:
default: "phi3:mini"
providers:
ollama:
base_url: "http://localhost:11434"
api_key: "" # Ollama doesn't require an API key for local access
# Default provider
default_provider: "ollama"
# Pattern settings
patterns:
auto_update: true
directory: "~/.config/fabric/patterns"
FABRIC_CONFIG_EOF
echo -e "${GREEN}Fabric configuration file created${NC}"
}
# Call the configuration function if Ollama container is running
if docker ps | grep -q ollama; then
configure_fabric_for_ollama
fi
fi # End SKIP_OLLAMA check
# Install Zsh if not already installed
echo -e "${YELLOW}Installing Zsh...${NC}"
if ! command -v zsh &> /dev/null; then
case $PKG_MANAGER in
nala)
sudo nala install -y zsh
;;
dnf)
sudo dnf install -y zsh
;;
apt)
sudo apt-get install -y zsh
;;
esac
fi
# Install Oh My Zsh
echo -e "${YELLOW}Installing Oh My Zsh...${NC}"
if [ ! -d "$HOME/.oh-my-zsh" ]; then
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
else
echo "Oh My Zsh already installed"
fi
# Install zoxide
echo -e "${YELLOW}Installing zoxide...${NC}"
if ! command -v zoxide &> /dev/null; then
TEMP_ZOXIDE=$(mktemp)
if curl -sS https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh -o "$TEMP_ZOXIDE"; then
echo -e "${YELLOW}Zoxide installer downloaded, executing...${NC}"
bash "$TEMP_ZOXIDE"
rm -f "$TEMP_ZOXIDE"
else
echo -e "${RED}ERROR: Failed to download zoxide installer${NC}"
exit 1
fi
# Ensure .local/bin is in PATH
if [[ ":$PATH:" != *":$HOME/.local/bin:"* ]]; then
echo 'export PATH="$PATH:$HOME/.local/bin"' >> "$HOME/.bashrc"
export PATH="$PATH:$HOME/.local/bin"
fi
fi
# Install nvm (Node Version Manager)
echo -e "${YELLOW}Installing nvm...${NC}"
if [ ! -d "$HOME/.nvm" ]; then
TEMP_NVM=$(mktemp)
if curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh -o "$TEMP_NVM"; then
echo -e "${YELLOW}NVM installer downloaded, executing...${NC}"
bash "$TEMP_NVM"
rm -f "$TEMP_NVM"
else
echo -e "${RED}ERROR: Failed to download nvm installer${NC}"
exit 1
fi
fi
# Load nvm regardless of whether it was just installed or already existed
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
# Install and set up Node.js only if nvm is available
if command -v nvm &>/dev/null; then
# Install latest LTS version of Node.js
nvm install --lts
nvm use --lts
# Set the LTS version as default
nvm alias default 'lts/*'
else
echo -e "${YELLOW}Warning: nvm installation may require a new shell session${NC}"
fi
# Define a reusable function for cloning Zsh plugins
clone_zsh_plugin() {
local plugin_url=$1
local plugin_dir=$2
if [ ! -d "$plugin_dir" ]; then
git clone "$plugin_url" "$plugin_dir"
fi
}
# Install Zsh plugins using the reusable function
echo -e "${YELLOW}Installing Zsh plugins...${NC}"
ZSH_CUSTOM="$HOME/.oh-my-zsh/custom"
PLUGINS_DIR="$ZSH_CUSTOM/plugins"
clone_zsh_plugin "https://github.com/zsh-users/zsh-autosuggestions" "$PLUGINS_DIR/zsh-autosuggestions"
clone_zsh_plugin "https://github.com/zsh-users/zsh-syntax-highlighting" "$PLUGINS_DIR/zsh-syntax-highlighting"
clone_zsh_plugin "https://github.com/MichaelAquilina/zsh-you-should-use" "$PLUGINS_DIR/zsh-you-should-use"
# Set up bash completion for backup scripts
echo -e "${YELLOW}Setting up bash completion for backup scripts...${NC}"
COMPLETION_SCRIPT="$DOTFILES_DIR/completions/backup-scripts-completion.bash"
if [ -f "$COMPLETION_SCRIPT" ]; then
# Create completions directory in home
mkdir -p "$HOME/.local/share/bash-completion/completions"
# Copy completion script to user's completion directory
cp "$COMPLETION_SCRIPT" "$HOME/.local/share/bash-completion/completions/"
# Make sure it's executable
chmod +x "$HOME/.local/share/bash-completion/completions/backup-scripts-completion.bash"
echo -e "${GREEN}Bash completion script installed successfully!${NC}"
else
echo -e "${YELLOW}Warning: Bash completion script not found at $COMPLETION_SCRIPT${NC}"
fi
# Set up dotfiles
echo -e "${YELLOW}Setting up dotfiles...${NC}"
# Consolidate symbolic link creation for dotfiles
ln -sf "$DOTFILES_SUBDIR/.zshrc" "$HOME/.zshrc"
ln -sf "$DOTFILES_SUBDIR/.nanorc" "$HOME/.nanorc" 2>/dev/null || true
ln -sf "$DOTFILES_SUBDIR/.profile" "$HOME/.profile" 2>/dev/null || true
ln -sf "$DOTFILES_SUBDIR/.gitconfig" "$HOME/.gitconfig" 2>/dev/null || true
# Create custom aliases based on available commands
echo -e "${YELLOW}Setting up custom aliases...${NC}"
ZSH_CUSTOM="$HOME/.oh-my-zsh/custom"
ALIASES_FILE="$ZSH_CUSTOM/aliases.zsh"
mkdir -p "$ZSH_CUSTOM"
# Create a copy of the original aliases file for backup
cp "$DOTFILES_SUBDIR/my-aliases.zsh.original" "$ALIASES_FILE.bak"
# First, copy all general aliases except those we'll modify based on OS and available commands
grep -v "^alias cat=" "$DOTFILES_SUBDIR/my-aliases.zsh.original" | \
grep -v "^alias fd=" | \
grep -v "^alias fzf=" | \
grep -v "^alias ls=" | \
grep -v "^alias ll=" | \
grep -v "^alias la=" | \
grep -v "^alias l=" | \
grep -v "^alias tree=" | \
grep -v "^alias lt=" | \
grep -v "^alias llt=" | \
grep -v "^alias lg=" | \
grep -v "^alias lh=" | \
grep -v "^alias lr=" | \
grep -v "^alias lx=" > "$ALIASES_FILE"
# Function to check for command existence and add appropriate alias
add_conditional_alias() {
local cmd_name=$1
local debian_cmd=$2
local fedora_cmd=$3
local alias_line=$4
echo -e "${YELLOW}Checking for $cmd_name command...${NC}"
if command -v "$debian_cmd" &> /dev/null; then
echo "alias $cmd_name=\"$debian_cmd\"" >> "$ALIASES_FILE"
echo -e "${GREEN}Using $debian_cmd for $cmd_name alias${NC}"
return 0
elif command -v "$fedora_cmd" &> /dev/null; then
echo "alias $cmd_name=\"$fedora_cmd\"" >> "$ALIASES_FILE"
echo -e "${GREEN}Using $fedora_cmd for $cmd_name alias${NC}"
return 0
else
echo -e "${YELLOW}Neither $debian_cmd nor $fedora_cmd found. Skipping $cmd_name alias.${NC}"
return 1
fi
}
# Set up the cat/bat alias
add_conditional_alias "cat" "batcat" "bat"
# Set up the fd alias
add_conditional_alias "fd" "fdfind" "fd"
# Set up fzf preview with correct bat command
if command -v fzf &> /dev/null; then
if command -v batcat &> /dev/null; then
echo "alias fzf=\"fzf --preview='batcat {}'\"" >> "$ALIASES_FILE"
echo -e "${GREEN}Using batcat for fzf preview${NC}"
elif command -v bat &> /dev/null; then
echo "alias fzf=\"fzf --preview='bat {}'\"" >> "$ALIASES_FILE"
echo -e "${GREEN}Using bat for fzf preview${NC}"
else
echo "alias fzf=\"fzf\"" >> "$ALIASES_FILE"
echo -e "${YELLOW}No bat/batcat found for fzf preview. Using basic fzf alias.${NC}"
fi
fi
# Set up eza aliases if eza is available
if command -v eza &> /dev/null; then
echo -e "${YELLOW}Setting up eza aliases...${NC}"
cat >> "$ALIASES_FILE" << 'EOF'
# 🌟 Eza aliases - Modern replacement for ls
alias ls="eza --icons=always -a --color=auto --group-directories-first"
alias la="eza --icons=always -la --color=auto --group-directories-first"
alias ll="eza --icons=always -la --classify=always -h --color=auto --group-directories-first"
alias l="eza --icons=always -1 -a --color=auto --group-directories-first"
alias lt="eza --icons=always -a --tree --level=2 --color=auto --group-directories-first"
alias llt="eza --icons=always -la --tree --level=2 --color=auto --group-directories-first"
alias lg="eza --icons=always -la --git --color=auto --group-directories-first"
alias lh="eza --icons=always -la --color=auto --group-directories-first --sort=size"
alias lr="eza --icons=always -la --color=auto --group-directories-first --sort=modified"
alias lx="eza --icons=always -la --color=auto --group-directories-first --sort=extension"
alias tree="eza --icons=always -a --tree --color=auto --group-directories-first"
EOF
echo -e "${GREEN}Eza aliases configured successfully!${NC}"
else
echo -e "${YELLOW}Eza not found. Using traditional ls aliases.${NC}"
cat >> "$ALIASES_FILE" << 'EOF'
# Traditional ls aliases
alias ll="ls -laFh --group-directories-first --color=auto"
EOF
fi
# Save the customized aliases to the dotfiles directory for reference
echo -e "${YELLOW}Saving customized aliases to dotfiles directory...${NC}"
if [ -f "$ALIASES_FILE" ]; then
# Copy the customized aliases to the dotfiles directory as my-aliases.zsh
# This file will be ignored by git but serves as a local reference
cp "$ALIASES_FILE" "$DOTFILES_SUBDIR/my-aliases.zsh"
fi
# Make sure the aliases file gets sourced immediately in the current shell
echo -e "${GREEN}Custom aliases configured for $OS_NAME.${NC}"
# Set Zsh as default shell if not already set
if [[ "$SHELL" != *"zsh"* ]]; then
echo -e "${YELLOW}Setting Zsh as default shell...${NC}"
ZSH_PATH=$(which zsh)
if [ -f "$ZSH_PATH" ]; then
# Check if zsh is already in /etc/shells
if ! grep -q "$ZSH_PATH" /etc/shells; then
echo -e "${YELLOW}Adding $ZSH_PATH to /etc/shells...${NC}"
echo "$ZSH_PATH" | sudo tee -a /etc/shells
fi
# Set as default shell
chsh -s "$ZSH_PATH"
else
echo -e "${RED}Zsh not found at $ZSH_PATH. Please set it as your default shell manually.${NC}"
fi
fi
# Display useful information about the setup
echo -e "\n${GREEN}=== Setup Summary ===${NC}"
echo -e "${GREEN}OS: $OS_NAME $OS_VERSION${NC}"
echo -e "${GREEN}Package Manager: $PKG_MANAGER${NC}"
echo -e "${GREEN}Shell: $(basename "$SHELL") → zsh${NC}"
# Test Ollama and Fabric integration
echo -e "\n${GREEN}=== Testing Ollama and Fabric Integration ===${NC}"
echo -e "${YELLOW}Testing Ollama Docker container...${NC}"
if docker ps | grep -q ollama; then
echo -e "${GREEN}✓ Ollama Docker container is running${NC}"
# Test if Ollama API is responding
echo -e "${YELLOW}Testing Ollama API...${NC}"
if curl -s http://localhost:11434/api/tags >/dev/null 2>&1; then
echo -e "${GREEN}✓ Ollama API is responding${NC}"
# List available models
echo -e "${YELLOW}Available Ollama models:${NC}"
docker exec ollama ollama list || echo -e "${YELLOW}No models listed or command failed${NC}"
else
echo -e "${YELLOW}⚠ Ollama API not responding yet (may need more time to start)${NC}"
fi
else
echo -e "${RED}✗ Ollama Docker container is not running${NC}"
fi
echo -e "\n${YELLOW}Testing Fabric installation...${NC}"
if command -v fabric &> /dev/null; then
echo -e "${GREEN}✓ Fabric is installed${NC}"
# Test fabric patterns
echo -e "${YELLOW}Testing Fabric patterns...${NC}"
if fabric --list-patterns >/dev/null 2>&1; then
echo -e "${GREEN}✓ Fabric patterns are available${NC}"
echo -e "${YELLOW}Number of patterns: $(fabric --list-patterns 2>/dev/null | wc -l)${NC}"
else
echo -e "${YELLOW}⚠ Fabric patterns may need to be updated${NC}"
fi
# Check fabric configuration
if [ -f "$HOME/.config/fabric/.env" ]; then
echo -e "${GREEN}✓ Fabric Ollama configuration found${NC}"
else
echo -e "${YELLOW}⚠ Fabric Ollama configuration not found${NC}"
fi
else
echo -e "${RED}✗ Fabric is not installed${NC}"
fi
echo -e "\n${GREEN}=== Post-Installation Instructions ===${NC}"
echo -e "${YELLOW}1. Restart your shell or run: source ~/.zshrc${NC}"
echo -e "${YELLOW}2. Test Ollama: ollama list${NC}"
echo -e "${YELLOW}3. Test Fabric: fabric --list-patterns${NC}"
echo -e "${YELLOW}4. Try a Fabric pattern: echo 'Hello world' | fabric --pattern summarize${NC}"
echo -e "${YELLOW}5. Install more models: ollama pull llama2${NC}"
echo -e "${YELLOW}6. Manage Ollama container: ollama-start, ollama-stop, ollama-logs${NC}"
echo -e "\n${GREEN}=== Useful Commands ===${NC}"
echo -e "${YELLOW}• View running containers: docker ps${NC}"
echo -e "${YELLOW}• Ollama logs: docker logs -f ollama${NC}"
echo -e "${YELLOW}• Fabric help: fabric --help${NC}"
echo -e "${YELLOW}• Update patterns: fabric --updatepatterns${NC}"
echo -e "\n${GREEN}Setup completed successfully for $OS_NAME $OS_VERSION!${NC}"
echo -e "${YELLOW}Note: You may need to log out and log back in for all changes to take effect.${NC}"