#!/bin/sh

# Stop on errors
set -e

# Use $1 as a file, or /proc/pmu/battery_0 if none are provided
file="${1:-/proc/pmu/battery_0}"

# Get the charge!
#  1. find the line starting with the text 'charge' in $file;
#  2. cut the line on the ':' character, and take the 2nd part.
charge=$(grep -E '^charge' "$file" | cut -d':' -f2)
# Replace any space character with nothing
charge=${charge// /}

# Same with max_charge...
max_charge=$(grep -E '^max_charge' "$file" | cut -d':' -f2)
max_charge=${max_charge// /}

#...
current=$(grep -E '^current' "$file" | cut -d':' -f2)
current=${current// /}

#...
time_rem=$(grep -E '^time rem\.' "$file" | cut -d':' -f2)
time_rem=${time_rem// /}

# Let's do maths!
# Take the charge, multiply it by 100, then divide it
# by the max charge to get the battery percentage!
echo "$(( $charge*100 / $max_charge ))%"

# Clever string trickery!
# Check if $current is Greater Than 0,
#  if it is not, displys "dis", with no new line at the end!
[[ "$current" -gt 0 ]] || echo -n "dis"
# Show the word "charging" (this can combines with the "dis" above to make a word!)
#  then show $time_rem divided by 60 for the numbers of minutes left!
echo "charging ($(( "$time_rem" / 60 )) mins)"
