1
0
Files
dotfiles/scripts/30-install-packages.sh

126 lines
3.5 KiB
Bash

#!/bin/bash
# scripts/30-install-packages.sh - Install packages from package lists
set -euo pipefail
# Source utilities
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/utils.sh"
# Detect package manager
detect_package_manager() {
if command_exists apt; then
echo "apt"
elif command_exists yum; then
echo "yum"
elif command_exists dnf; then
echo "dnf"
elif command_exists pacman; then
echo "pacman"
elif command_exists brew; then
echo "brew"
else
echo "unknown"
fi
}
# Install packages with detected package manager
install_packages() {
local package_manager="$1"
shift
local packages=("$@")
if [ ${#packages[@]} -eq 0 ]; then
log_info "No packages to install"
return 0
fi
case "$package_manager" in
apt)
log_info "Installing packages with apt..."
sudo apt update && sudo apt install -y "${packages[@]}"
;;
yum)
log_info "Installing packages with yum..."
sudo yum install -y "${packages[@]}"
;;
dnf)
log_info "Installing packages with dnf..."
sudo dnf install -y "${packages[@]}"
;;
pacman)
log_info "Installing packages with pacman..."
sudo pacman -S --noconfirm "${packages[@]}"
;;
brew)
log_info "Installing packages with brew..."
brew install "${packages[@]}"
;;
*)
log_error "Unknown package manager. Please install packages manually."
return 1
;;
esac
}
# Read packages from file
read_package_file() {
local file="$1"
if [ -f "$file" ]; then
# Filter out comments and empty lines
grep -v '^#' "$file" | grep -v '^[[:space:]]*$' | tr '\n' ' '
fi
}
install_package_lists() {
local dotfiles_dir="$1"
local packages_dir="$dotfiles_dir/packages"
if [ ! -d "$packages_dir" ]; then
log_warning "No packages directory found at $packages_dir"
return 0
fi
local package_manager
package_manager=$(detect_package_manager)
if [ "$package_manager" = "unknown" ]; then
log_error "No supported package manager found"
return 1
fi
log_info "Detected package manager: $package_manager"
# Install package lists in order
local package_files=("base.txt" "cli-tools.txt" "dev.txt" "gui.txt")
for package_file in "${package_files[@]}"; do
local file_path="$packages_dir/$package_file"
if [ -f "$file_path" ]; then
log_info "Installing packages from $package_file..."
local packages
packages=$(read_package_file "$file_path")
if [ -n "$packages" ]; then
# Convert space-separated string to array
read -ra package_array <<< "$packages"
install_packages "$package_manager" "${package_array[@]}"
log_success "Completed installation from $package_file"
else
log_info "No packages found in $package_file"
fi
else
log_info "Package file $package_file not found, skipping"
fi
done
}
# Run if called directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
if [ $# -ne 1 ]; then
log_error "Usage: $0 <dotfiles_directory>"
exit 1
fi
install_package_lists "$1"
fi