bash-folders/bash-folders-battery-hook.sh

174 lines
3.8 KiB
Bash
Raw Normal View History

2023-05-02 15:36:38 +02:00
#!/usr/bin/env bash
# --
# Description: Script that launches other scripts in different battery states.
# --
# Cron: * * * * * bash-folders-battery-hook.sh --folder [folder path]
# --
# START
set -e
# FUNCTIONS
usage() {
if [ "$*" != "" ] ; then
echo "Error: $*"
fi
cat << EOF
Usage: $PROGNAME [OPTION]
Script that launches other scripts in different battery states.
"discharging" When the battery is in use.
"charging" When the battery is charging.
2023-05-03 15:20:54 +02:00
"low" When it reaches the low percentage.
"high" When it reaches the high percentage.
2023-05-02 15:36:38 +02:00
"full" When the battery is full.
Options:
--folder [path] Folder where the different scripts are located.
2023-05-03 15:20:54 +02:00
--low [number] Low battery percentage. Default 15.
--high [number] High battery percentage. Default 85.
2023-05-02 15:36:38 +02:00
--help Display this usage message and exit
EOF
exit 1
}
2023-05-03 15:20:54 +02:00
status() {
# Possible values: Discharging, Charging and Full
cat /sys/class/power_supply/BAT0/status
}
capacity() {
# Possible values: 0-100
cat /sys/class/power_supply/BAT0/capacity
}
run_discharging() {
# Check if discharging script exists
2023-05-03 19:42:05 +02:00
if [ ! -f "$PATH_DISCHARGING_SCRIPT" ]; then
2023-05-03 15:20:54 +02:00
# If not, create it
2023-05-03 19:42:05 +02:00
touch "$PATH_DISCHARGING_SCRIPT"
chmod +x "$PATH_DISCHARGING_SCRIPT"
2023-05-03 15:20:54 +02:00
fi
# If status is discharging, run discharging script
2023-05-03 19:42:05 +02:00
if [ "$(status)" = "Discharging" ]; then
2023-05-03 15:20:54 +02:00
$PATH_DISCHARGING_SCRIPT
2023-05-02 15:36:38 +02:00
fi
2023-05-03 15:20:54 +02:00
}
2023-05-02 15:36:38 +02:00
2023-05-03 15:20:54 +02:00
run_charging() {
# Check if charging script exists
2023-05-03 19:42:05 +02:00
if [ ! -f "$PATH_CHARGING_SCRIPT" ]; then
2023-05-03 15:20:54 +02:00
# If not, create it
2023-05-03 19:42:05 +02:00
touch "$PATH_CHARGING_SCRIPT"
chmod +x "$PATH_CHARGING_SCRIPT"
2023-05-03 15:20:54 +02:00
fi
# If status is charging, run charging script
2023-05-03 19:42:05 +02:00
if [ "$(status)" = "Charging" ]; then
2023-05-03 15:20:54 +02:00
$PATH_CHARGING_SCRIPT
fi
}
run_low() {
# Check if low script exists
2023-05-03 19:42:05 +02:00
if [ ! -f "$PATH_LOW_SCRIPT" ]; then
2023-05-03 15:20:54 +02:00
# If not, create it
2023-05-03 19:42:05 +02:00
touch "$PATH_LOW_SCRIPT"
chmod +x "$PATH_LOW_SCRIPT"
2023-05-03 15:20:54 +02:00
fi
# If status is discharging and battery is low, run low script
2023-05-03 19:42:05 +02:00
if [ "$(status)" = "Discharging" ] && [ "$(capacity)" -le "$LOW_BATTERY" ]; then
2023-05-03 15:20:54 +02:00
$PATH_LOW_SCRIPT
fi
}
run_high() {
# Check if high script exists
2023-05-03 19:42:05 +02:00
if [ ! -f "$PATH_HIGH_SCRIPT" ]; then
2023-05-03 15:20:54 +02:00
# If not, create it
2023-05-03 19:42:05 +02:00
touch "$PATH_HIGH_SCRIPT"
chmod +x "$PATH_HIGH_SCRIPT"
2023-05-03 15:20:54 +02:00
fi
# If status is charging and battery is high, run high script
2023-05-03 19:42:05 +02:00
if [ "$(status)" = "Charging" ] && [ "$(capacity)" -ge "$HIGH_BATTERY" ]; then
2023-05-03 15:20:54 +02:00
$PATH_HIGH_SCRIPT
fi
}
run_full() {
# Check if full script exists
2023-05-03 19:42:05 +02:00
if [ ! -f "$PATH_FULL_SCRIPT" ]; then
2023-05-03 15:20:54 +02:00
# If not, create it
2023-05-03 19:42:05 +02:00
touch "$PATH_FULL_SCRIPT"
chmod +x "$PATH_FULL_SCRIPT"
2023-05-03 15:20:54 +02:00
fi
# If status is charging and battery is full, run full script
2023-05-03 19:42:05 +02:00
if [ "$(status)" = "Full" ]; then
2023-05-03 15:20:54 +02:00
$PATH_FULL_SCRIPT
fi
2023-05-02 15:36:38 +02:00
}
start() {
2023-05-03 15:20:54 +02:00
# Run all scripts
run_discharging
run_charging
run_low
run_high
run_full
2023-05-02 15:36:38 +02:00
}
# CONTROLE ARGUMENTS
2023-05-03 19:42:05 +02:00
# Parse command line arguments
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--folder)
FOLDER_ORIGIN="$2"
shift # past argument
shift # past value
;;
--low)
LOW_BATTERY="$2"
shift # past argument
shift # past value
;;
--high)
HIGH_BATTERY="$2"
shift # past argument
shift # past value
;;
*)
usage "Unknown option: $1"
;;
2023-05-02 15:36:38 +02:00
esac
done
2023-05-03 19:42:05 +02:00
# VARIABLES
PROGNAME=$(basename "$0")
2023-06-26 09:19:46 +02:00
LOW_BATTERY=20
HIGH_BATTERY=80
2023-05-03 19:42:05 +02:00
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"
# Check if the required --folder flag is provided
if [ -z "$FOLDER_ORIGIN" ]; then
echo "Error: The --folder flag is required."
exit 1
else
start
2023-05-02 15:36:38 +02:00
fi