#!/bin/bash # PowerTOP Analysis Tool # Generates detailed power consumption reports and recommendations set -euo pipefail # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' # No Color # Default values REPORT_DIR="$HOME/.battery-optimizer/reports" DURATION=60 OUTPUT_FORMAT="html" # Function to show usage show_usage() { echo "PowerTOP Analysis Tool" echo echo "Usage: $0 [OPTIONS]" echo echo "Options:" echo " -d, --duration SECONDS Analysis duration in seconds (default: 60)" echo " -o, --output DIR Output directory (default: ~/.battery-optimizer/reports)" echo " -f, --format FORMAT Output format: html, csv, txt (default: html)" echo " -a, --auto-tune Apply PowerTOP auto-tune (requires sudo)" echo " -s, --show-report Show latest report in browser" echo " -h, --help Show this help message" echo echo "Examples:" echo " $0 Generate 60-second report" echo " $0 -d 120 -f html Generate 2-minute HTML report" echo " $0 -a Apply auto-tune optimizations" echo " $0 -s Show latest report" } # Function to check dependencies check_dependencies() { if ! command -v powertop &> /dev/null; then echo -e "${RED}Error: PowerTOP is not installed${NC}" echo "Please run the installation script first: ./scripts/install.sh" exit 1 fi } # Function to create report directory create_report_dir() { if [[ ! -d "$REPORT_DIR" ]]; then mkdir -p "$REPORT_DIR" echo -e "${GREEN}Created report directory: $REPORT_DIR${NC}" fi } # Function to run PowerTOP analysis run_analysis() { local duration=$1 local output_file="$2" echo -e "${BLUE}Running PowerTOP analysis for $duration seconds...${NC}" echo "This requires sudo privileges for hardware access." echo # Create a temporary file for PowerTOP output local temp_file=$(mktemp) case "$OUTPUT_FORMAT" in "html") sudo powertop --html="$output_file" --time="$duration" ;; "csv") sudo powertop --csv="$output_file" --time="$duration" ;; "txt") sudo powertop --time="$duration" > "$temp_file" 2>&1 cp "$temp_file" "$output_file" rm "$temp_file" ;; esac if [[ -f "$output_file" ]]; then echo -e "${GREEN}Analysis complete! Report saved to: $output_file${NC}" # Show file size local file_size=$(du -h "$output_file" | cut -f1) echo -e "${CYAN}Report size: $file_size${NC}" return 0 else echo -e "${RED}Error: Failed to generate report${NC}" return 1 fi } # Function to apply auto-tune apply_auto_tune() { echo -e "${YELLOW}Applying PowerTOP auto-tune optimizations...${NC}" echo "This will modify system power settings." read -p "Continue? (y/N): " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then sudo powertop --auto-tune echo -e "${GREEN}Auto-tune applied successfully${NC}" echo "Note: These settings are temporary and will reset on reboot" echo "To make them permanent, consider adding them to startup scripts" else echo "Auto-tune cancelled" fi } # Function to show latest report show_latest_report() { local latest_html=$(find "$REPORT_DIR" -name "*.html" -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -d' ' -f2-) if [[ -n "$latest_html" && -f "$latest_html" ]]; then echo -e "${GREEN}Opening latest report: $latest_html${NC}" # Try different browsers if command -v xdg-open &> /dev/null; then xdg-open "$latest_html" elif command -v firefox &> /dev/null; then firefox "$latest_html" & elif command -v chromium &> /dev/null; then chromium "$latest_html" & elif command -v google-chrome &> /dev/null; then google-chrome "$latest_html" & else echo "No suitable browser found. Please open manually:" echo "$latest_html" fi else echo -e "${YELLOW}No HTML reports found in $REPORT_DIR${NC}" echo "Generate a report first with: $0 -f html" fi } # Function to show quick summary show_quick_summary() { echo -e "${CYAN}Quick PowerTOP Summary:${NC}" echo "----------------------------------------" # Show current power consumption estimate sudo powertop --time=1 2>/dev/null | grep -E "(The battery reports|Wh)" | head -5 echo echo -e "${CYAN}Top power consumers (run full analysis for details):${NC}" sudo powertop --time=1 2>/dev/null | grep -A 10 "Top 10 power consumers" | tail -10 } # Function to generate filename generate_filename() { local timestamp=$(date +"%Y%m%d_%H%M%S") local hostname=$(hostname -s) echo "powertop_${hostname}_${timestamp}.${OUTPUT_FORMAT}" } # Main function main() { local auto_tune=false local show_report=false local quick_summary=false # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in -d|--duration) DURATION="$2" shift 2 ;; -o|--output) REPORT_DIR="$2" shift 2 ;; -f|--format) OUTPUT_FORMAT="$2" shift 2 ;; -a|--auto-tune) auto_tune=true shift ;; -s|--show-report) show_report=true shift ;; -q|--quick) quick_summary=true shift ;; -h|--help) show_usage exit 0 ;; *) echo "Unknown option: $1" show_usage exit 1 ;; esac done # Validate format if [[ ! "$OUTPUT_FORMAT" =~ ^(html|csv|txt)$ ]]; then echo -e "${RED}Error: Invalid output format '$OUTPUT_FORMAT'${NC}" echo "Supported formats: html, csv, txt" exit 1 fi # Check dependencies check_dependencies # Handle special actions if [[ "$show_report" == true ]]; then show_latest_report exit 0 fi if [[ "$auto_tune" == true ]]; then apply_auto_tune exit 0 fi if [[ "$quick_summary" == true ]]; then show_quick_summary exit 0 fi # Create report directory create_report_dir # Generate output filename local output_file="$REPORT_DIR/$(generate_filename)" # Run analysis if run_analysis "$DURATION" "$output_file"; then echo echo -e "${CYAN}Analysis Summary:${NC}" echo "• Report location: $output_file" echo "• Analysis duration: $DURATION seconds" echo "• Format: $OUTPUT_FORMAT" if [[ "$OUTPUT_FORMAT" == "html" ]]; then echo echo "To view the report, run: $0 --show-report" echo "Or open manually: firefox '$output_file'" fi echo echo -e "${YELLOW}Tip: Run '$0 --auto-tune' to apply recommended optimizations${NC}" fi } # Run main function if script is executed directly if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi