#!/bin/bash # Battery Life Optimizer - TLP and PowerTOP Installation Script # For Debian/Ubuntu-based Linux distributions set -euo pipefail # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Logging function log() { echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" } success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if running as root check_root() { if [[ $EUID -eq 0 ]]; then error "This script should not be run as root!" error "It will use sudo when necessary." exit 1 fi } # Check if system is supported check_system() { if [[ ! -f /etc/debian_version ]] && [[ ! -f /etc/ubuntu_version ]]; then warning "This script is optimized for Debian/Ubuntu systems." read -p "Continue anyway? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then exit 1 fi fi } # Update package lists update_packages() { log "Updating package lists..." sudo apt update success "Package lists updated" } # Install TLP install_tlp() { log "Installing TLP (Advanced Power Management)..." # Check if TLP is already installed if command -v tlp &> /dev/null; then warning "TLP is already installed" return 0 fi # Install TLP and additional packages sudo apt install -y tlp tlp-rdw # For ThinkPads, install additional tools if dmidecode -s system-product-name | grep -qi "thinkpad"; then log "ThinkPad detected, installing additional tools..." sudo apt install -y tp-smapi-dkms acpi-call-dkms fi # Enable and start TLP service sudo systemctl enable tlp.service sudo systemctl start tlp.service success "TLP installed and configured" } # Install PowerTOP install_powertop() { log "Installing PowerTOP..." # Check if PowerTOP is already installed if command -v powertop &> /dev/null; then warning "PowerTOP is already installed" return 0 fi sudo apt install -y powertop success "PowerTOP installed" } # Setup PowerTOP service for automatic tuning setup_powertop_service() { log "Setting up PowerTOP auto-tune service..." # Create systemd service file for PowerTOP sudo tee /etc/systemd/system/powertop.service > /dev/null <