#!/bin/sh -e
#
# batt-info - battery and charging status for macos

info() {
  printf "\\e[1m%b\\e[0m\n" "$*"
}

section() {
  grey="\033[0;37m"
  blue="\033[0;34m"
  reset="\033[0m"
  printf '%b\n' "${grey}"
  printf '%b\n' "=== ${blue}$1${grey}"
  printf '%b\n' "==============================================================${reset}"
}

battery=$(system_profiler SPPowerDataType | grep Wattage | cut -c 7-)
charging=$(pmset -g batt | tail -1 | cut -d ')' -f 2)

case "$1" in
  -h | --help)
    printf '%s\n' "${0##*/}: [-v|--verbose] print detailed info"
    exit 0
    ;;
  -v | --verbose)
    section "ac power adapter"
    pmset -g ac 2>/dev/null
    section "system load advisory"
    pmset -g sysload
    section "power state - capabilites"
    pmset -g systemstate
    ;;
  *)
    info "${battery}" 2>/dev/null
    info "${charging}"
    ;;
esac
