#!/bin/bash # utils.sh - Common utility functions for USM installer # Colors for terminal output readonly RED='\033[0;31m' readonly GREEN='\033[0;32m' readonly YELLOW='\033[1;33m' readonly BLUE='\033[0;34m' readonly CYAN='\033[0;36m' readonly BOLD='\033[1m' readonly NC='\033[0m' # No Color # Logging functions log_info() { echo -e "${GREEN}[INFO]${NC} $1" } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } log_step() { echo -e "${CYAN}==>${NC} ${BOLD}$1${NC}" } # Check if running as root is_root() { [[ $EUID -eq 0 ]] } # Check if a command exists command_exists() { command -v "$1" &>/dev/null } # Get the system's package manager detect_package_manager() { if command_exists apt-get; then echo "apt" elif command_exists dnf; then echo "dnf" elif command_exists yum; then echo "dnf" # Fallback to dnf implementation elif command_exists pacman; then echo "pacman" else echo "unknown" fi } # Get the system's sudo command get_sudo() { if is_root; then echo "" elif command_exists sudo; then echo "sudo" elif command_exists doas; then echo "doas" else echo "" fi } # Confirm action with user confirm() { local prompt="$1" local default="${2:-n}" local response if [[ "$ASSUME_YES" == "true" ]]; then return 0 fi if [[ "$default" == "y" ]]; then prompt="$prompt [Y/n]: " else prompt="$prompt [y/N]: " fi read -r -p "$prompt" response response="${response:-$default}" [[ "$response" =~ ^[Yy]$ ]] } # Create directory if it doesn't exist ensure_dir() { local dir="$1" if [[ ! -d "$dir" ]]; then mkdir -p "$dir" fi } # Extract XZ compressed data from this script extract_payload() { local output_dir="$1" local script_path="$0" local payload_start # Find the payload marker payload_start=$(grep -an "^__PAYLOAD_START__$" "$script_path" | cut -d: -f1) if [[ -z "$payload_start" ]]; then log_error "Could not find payload in installer" return 1 fi # Extract and decompress the payload tail -n +$((payload_start + 1)) "$script_path" | xz -d | tar -xf - -C "$output_dir" } # Cleanup function for traps cleanup() { if [[ -n "$TEMP_DIR" && -d "$TEMP_DIR" ]]; then rm -rf "$TEMP_DIR" fi } # Set up temporary directory setup_temp_dir() { TEMP_DIR=$(mktemp -d) trap cleanup EXIT } # Download a file download_file() { local url="$1" local output="$2" if command_exists curl; then curl -fsSL "$url" -o "$output" elif command_exists wget; then wget -q "$url" -O "$output" else log_error "Neither curl nor wget is available" return 1 fi } # Clone a git repository clone_repo() { local url="$1" local dest="$2" local branch="${3:-main}" if ! command_exists git; then log_error "git is not installed" return 1 fi git clone --depth 1 -b "$branch" "$url" "$dest" 2>/dev/null } # Get total size of a directory get_dir_size() { local dir="$1" du -sb "$dir" 2>/dev/null | cut -f1 } # Format bytes to human readable format_size() { local bytes=$1 local units=('B' 'KB' 'MB' 'GB' 'TB') local unit=0 local size=$bytes while (( size > 1024 && unit < 4 )); do size=$((size / 1024)) ((unit++)) done echo "$size${units[$unit]}" }