#!/bin/bash
version="2022-01-25"
this="${0##*/}"
USAGE="\
    $this@v$version
    Wrapper around 'upower' to show battery info(s).
    Usage:  $this --[help|all|oneline|notify]

    'oneline' [Default] prints 'status | time | percentage'
     'notify' sends 'oneline' to 'notify-send'
        'all' prints 'upower -i'
       'help' prints this text
"
arg=${1:---oneline}
if [[ "$arg" = "--help" ]]; then
    echo -e "$USAGE"
    exit 0
fi

batt_name=`upower -e | grep 'BAT'`
batt_info=`upower -i $batt_name`
if [[ "$arg" = "--all" ]]; then
    echo -e "$batt_info"
    exit 0
fi

batt_oneline=`echo "$batt_info" | grep -E "state|percentage|to\ full|to\ empty" | §awk -F: 2 | sed 's/^ *//g' | tr -s '\n' '|' | sed 's/|$/\n/' | sed 's/|/ | /g'`
if [[ "$arg" = "--oneline" ]]; then
    echo -e "$batt_oneline"
    exit 0
fi

icon=`echo "$batt_info" | grep "icon-name" | §awk -F\' 2`
notify-send --icon=$icon "Battery" "$batt_oneline"