mirror of
https://github.com/tanrax/bash-folders.git
synced 2025-10-10 18:25:50 +02:00
Moved everything into functions for scoping; removed individual variables/functions for each script, since its all hardcoded anyway; added option to specify battery to check
This commit is contained in:
@@ -1,20 +1,16 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
[[ -n "${*}" ]] && printf '%s\n' "Error: ${*}" >&2
|
|
||||||
|
|
||||||
cat << EOF
|
cat << EOF
|
||||||
USAGE: ${0##*/} [OPTION] --folder PATH
|
USAGE: ${0##*/} [OPTION] PATH
|
||||||
|
|
||||||
Launches other scripts in different battery states
|
Launches other scripts for different battery states
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
--help Display this usage message and exit
|
-h, --help Display this usage message and exit
|
||||||
--folder PATH PATH where the different scripts are located
|
-l, --low INT Low battery percentage (default: ${defaults['low']})
|
||||||
--low INT Low battery percentage (default: 15)
|
-h, --high INT High battery percentage (default: ${defaults['high']})
|
||||||
--high INT High battery percentage (default: 85)
|
-b, --battery INT Battery to be checked
|
||||||
|
|
||||||
STATES:
|
STATES:
|
||||||
discharching When the battery is in use
|
discharching When the battery is in use
|
||||||
@@ -23,117 +19,95 @@ STATES:
|
|||||||
high When the battery reaches the high percentag
|
high When the battery reaches the high percentag
|
||||||
full When the battery is full
|
full When the battery is full
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
status() {
|
|
||||||
cat /sys/class/power_supply/BAT0/status
|
|
||||||
}
|
|
||||||
|
|
||||||
capacity() {
|
|
||||||
cat /sys/class/power_supply/BAT0/capacity
|
|
||||||
}
|
|
||||||
|
|
||||||
run_discharging() {
|
|
||||||
if ! [[ -f "$PATH_DISCHARGING_SCRIPT" ]]; then
|
|
||||||
touch "$PATH_DISCHARGING_SCRIPT"
|
|
||||||
chmod +x "$PATH_DISCHARGING_SCRIPT"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$(status)" == "Discharging" ]]; then
|
|
||||||
$PATH_DISCHARGING_SCRIPT
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
run_charging() {
|
|
||||||
if ! [[ -f "$PATH_CHARGING_SCRIPT" ]]; then
|
|
||||||
touch "$PATH_CHARGING_SCRIPT"
|
|
||||||
chmod +x "$PATH_CHARGING_SCRIPT"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$(status)" == "Charging" ]]; then
|
|
||||||
$PATH_CHARGING_SCRIPT
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
run_low() {
|
|
||||||
if ! [[ -f "$PATH_LOW_SCRIPT" ]]; then
|
|
||||||
touch "$PATH_LOW_SCRIPT"
|
|
||||||
chmod +x "$PATH_LOW_SCRIPT"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$(status)" == "Discharging" ]] && [[ "$(capacity)" -le "$LOW_BATTERY" ]]; then
|
|
||||||
$PATH_LOW_SCRIPT
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
run_high() {
|
|
||||||
if ! [[ -f "$PATH_HIGH_SCRIPT" ]]; then
|
|
||||||
touch "$PATH_HIGH_SCRIPT"
|
|
||||||
chmod +x "$PATH_HIGH_SCRIPT"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$(status)" == "Charging" ]] && [[ "$(capacity)" -ge "$HIGH_BATTERY" ]]; then
|
|
||||||
$PATH_HIGH_SCRIPT
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
run_full() {
|
|
||||||
if ! [[ -f "$PATH_FULL_SCRIPT" ]]; then
|
|
||||||
touch "$PATH_FULL_SCRIPT"
|
|
||||||
chmod +x "$PATH_FULL_SCRIPT"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$(status)" = "Full" ]]; then
|
|
||||||
$PATH_FULL_SCRIPT
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
run_discharging
|
local cap
|
||||||
run_charging
|
cap="$(capacity)"
|
||||||
run_low
|
|
||||||
run_high
|
|
||||||
run_full
|
|
||||||
}
|
|
||||||
|
|
||||||
while [[ $# -gt 0 ]]; do
|
case "$(status | tr '[:upper:]' '[:lower:]')" in
|
||||||
case "${1}" in
|
full )
|
||||||
--folder)
|
run_full
|
||||||
FOLDER_ORIGIN="$2"
|
|
||||||
shift 2
|
|
||||||
;;
|
;;
|
||||||
--low)
|
discharging )
|
||||||
LOW_BATTERY="$2"
|
run_discharging
|
||||||
shift 2
|
(( cap <= settings['low'] )) && run_low
|
||||||
;;
|
;;
|
||||||
--high)
|
charging )
|
||||||
HIGH_BATTERY="$2"
|
run_charging
|
||||||
shift 2
|
(( cap >= settings['high'] )) && run_high
|
||||||
;;
|
|
||||||
*)
|
|
||||||
usage "Unknown option: $1"
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
}
|
||||||
|
|
||||||
LOW_BATTERY=20
|
run() {
|
||||||
HIGH_BATTERY=80
|
set -e
|
||||||
DISCHARGING_SCRIPT="discharging"
|
|
||||||
PATH_DISCHARGING_SCRIPT="$FOLDER_ORIGIN/$DISCHARGING_SCRIPT"
|
|
||||||
CHARGING_SCRIPT="charging"
|
|
||||||
PATH_CHARGING_SCRIPT="$FOLDER_ORIGIN/$CHARGING_SCRIPT"
|
|
||||||
LOW_SCRIPT="low"
|
|
||||||
PATH_LOW_SCRIPT="$FOLDER_ORIGIN/$LOW_SCRIPT"
|
|
||||||
HIGH_SCRIPT="high"
|
|
||||||
PATH_HIGH_SCRIPT="$FOLDER_ORIGIN/$HIGH_SCRIPT"
|
|
||||||
FULL_SCRIPT="full"
|
|
||||||
PATH_FULL_SCRIPT="$FOLDER_ORIGIN/$FULL_SCRIPT"
|
|
||||||
|
|
||||||
if [[ -z "$FOLDER_ORIGIN" ]]; then
|
local status capacity path
|
||||||
printf '%s\n' "Error: The --folder flag is required" >&2
|
status="$(< "/sys/class/power_supply/BAT${settings['battery']}/status")"
|
||||||
exit 1
|
capacity="$(< "/sys/class/power_supply/BAT${settings['battery']}/capacity")"
|
||||||
else
|
|
||||||
start
|
case "${status,,}" in
|
||||||
fi
|
discharging )
|
||||||
|
"${1}/${status,,}"
|
||||||
|
if (( capacity <= settings['low'] )); then
|
||||||
|
"${1}/low"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
charging )
|
||||||
|
"${1}/${status,,}"
|
||||||
|
if (( capacity >= settings['high'] )); then
|
||||||
|
"${1}/high"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
full )
|
||||||
|
"${1}/high"
|
||||||
|
"${1}/${status,,}"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
main() {
|
||||||
|
local -A defaults settings
|
||||||
|
local opts i
|
||||||
|
|
||||||
|
opts="$(getopt \
|
||||||
|
--options hl:H:b: \
|
||||||
|
--longoptions help,low:,high:,battery: \
|
||||||
|
--name "${0##*/}" \
|
||||||
|
-- "${@}" \
|
||||||
|
)"
|
||||||
|
|
||||||
|
defaults['low']="15"
|
||||||
|
defaults['high']="85"
|
||||||
|
defaults['battery']="0"
|
||||||
|
for i in "${!defaults[@]}"; do settings["$i"]="${defaults["$i"]}"; done
|
||||||
|
|
||||||
|
eval set -- "${opts}"
|
||||||
|
while true; do
|
||||||
|
case "${1}" in
|
||||||
|
-h | --help ) usage; return 0;;
|
||||||
|
-l | --low ) settings['low']="${2}"; shift;;
|
||||||
|
-H | --high ) settings['high']="${2}"; shift;;
|
||||||
|
* )
|
||||||
|
printf '%s\n' "Unknown option: '${1}'" >&2
|
||||||
|
usage
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ -z "${1}" ]]; then
|
||||||
|
printf '%s\n' "No folder specified" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
mkdir --parents "${1}"
|
||||||
|
|
||||||
|
run "${1}"
|
||||||
|
}
|
||||||
|
|
||||||
|
main "${@}"
|
||||||
|
Reference in New Issue
Block a user