From 2446198d702f4a4759c3710f8a8266ca3742477f Mon Sep 17 00:00:00 2001 From: dtb Date: Sat, 13 Aug 2022 16:29:04 -0400 Subject: [PATCH] separate out battery frontend --- bin/battery | 21 +++++++-------------- bin/battery.linux | 6 ++++++ bin/battery.netbsd | 7 +++++++ 3 files changed, 20 insertions(+), 14 deletions(-) create mode 100755 bin/battery.linux create mode 100755 bin/battery.netbsd diff --git a/bin/battery b/bin/battery index f1db841..93ed99c 100755 --- a/bin/battery +++ b/bin/battery @@ -1,18 +1,11 @@ #!/bin/sh -# adapted from other script i wrote, also in the public domain -if [ "$(uname)" = "Linux" ]; then - # this is a dirty hack. - # acpi | awk '{print $4}' will print the battery percentage, - # and s/,$// strips the trailing comma. - printf "%s\n" "$(acpi | awk '{print $4}' | sed 's/,$//')" -elif [ "$(uname)" = "NetBSD" ]; then - printf "%s\n" "$(envstat -s acpibat0:charge \ - | sed 1,2d \ - | cut -d ':' -f 2 \ - | awk '{print $1}' \ - | cut -d '.' -f 1)" -else +# battery -> battery.linux battery.netbsd etc +subprogram="$0"."$(uname | lowercase)" + +if ! command -v "$subprogram" >/dev/null 2>&1 && ! test -e "$subprogram"; then printf "%s: unsupported system\n" "$(uname)" 1>&2 - exit 1 + exit 70 # sysexits(3) EX_SOFTWARE fi + +"$subprogram" | exit 70 # sysexits(3) EX_SOFTWARE diff --git a/bin/battery.linux b/bin/battery.linux new file mode 100755 index 0000000..adc49d7 --- /dev/null +++ b/bin/battery.linux @@ -0,0 +1,6 @@ +#!/bin/sh + +# this is a dirty hack. +# acpi | awk '{print $4}' will print the battery percentage, +# and s/,$// strips the trailing comma. +acpi | awk '{print $4}' | sed 's/,$//' diff --git a/bin/battery.netbsd b/bin/battery.netbsd new file mode 100755 index 0000000..7e32eff --- /dev/null +++ b/bin/battery.netbsd @@ -0,0 +1,7 @@ +#!/bin/sh + +envstat -s acpibat0:charge \ + | sed 1,2d \ + | cut -d ':' -f 2 \ + | awk '{print $1}' \ + | cut -d '.' -f 1