#!/usr/bin/env bash # Battery Calibration Script for Arch Linux on MacBook Air # Helps safely discharge/charge battery to recalibrate SMC. LOGFILE="$HOME/battery_calibration.log" THRESHOLD_SHUTDOWN=2 # % at which system will auto-shutdown THRESHOLD_WARN1=10 THRESHOLD_WARN2=5 get_capacity() { cat /sys/class/power_supply/BAT0/capacity } get_status() { cat /sys/class/power_supply/BAT0/status } log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOGFILE" } log "=== Starting battery calibration session ===" while true; do CAPACITY=$(get_capacity) STATUS=$(get_status) log "Battery: $CAPACITY% | Status: $STATUS" if [[ "$STATUS" == "Discharging" ]]; then if (( CAPACITY <= THRESHOLD_WARN1 && CAPACITY > THRESHOLD_WARN2 )); then notify-send "Battery Calibration" "Battery low: $CAPACITY%. Save work." elif (( CAPACITY <= THRESHOLD_WARN2 && CAPACITY > THRESHOLD_SHUTDOWN )); then notify-send "Battery Calibration" "Battery critical: $CAPACITY%. Shutdown soon." elif (( CAPACITY <= THRESHOLD_SHUTDOWN )); then log "Battery reached $CAPACITY%. Shutting down for full discharge..." systemctl poweroff fi fi sleep 60 # check every minute done