Small syntax/indentation and other obvious cleanups before rewriting

This commit is contained in:
Alex Paarfus
2023-06-27 12:02:39 -04:00
parent b3e605a595
commit b65272285a
3 changed files with 127 additions and 216 deletions

View File

@@ -1,118 +1,96 @@
#!/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
[[ -n "${*}" ]] && printf '%s\n' "Error: ${*}" >&2
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.
"low" When it reaches the low percentage.
"high" When it reaches the high percentage.
"full" When the battery is full.
USAGE: ${0##*/} [OPTION] --folder PATH
Options:
--folder [path] Folder where the different scripts are located.
--low [number] Low battery percentage. Default 15.
--high [number] High battery percentage. Default 85.
Launches other scripts in different battery states
OPTIONS:
--help Display this usage message and exit
--folder PATH PATH where the different scripts are located
--low INT Low battery percentage (default: 15)
--high INT High battery percentage (default: 85)
STATES:
discharching When the battery is in use
charging When the battery is charging
low When the battery reaches the low percentage
high When the battery reaches the high percentag
full When the battery is full
EOF
exit 1
}
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
if [ ! -f "$PATH_DISCHARGING_SCRIPT" ]; then
# If not, create it
if ! [[ -f "$PATH_DISCHARGING_SCRIPT" ]]; then
touch "$PATH_DISCHARGING_SCRIPT"
chmod +x "$PATH_DISCHARGING_SCRIPT"
fi
# If status is discharging, run discharging script
if [ "$(status)" = "Discharging" ]; then
if [[ "$(status)" == "Discharging" ]]; then
$PATH_DISCHARGING_SCRIPT
fi
}
run_charging() {
# Check if charging script exists
if [ ! -f "$PATH_CHARGING_SCRIPT" ]; then
# If not, create it
if ! [[ -f "$PATH_CHARGING_SCRIPT" ]]; then
touch "$PATH_CHARGING_SCRIPT"
chmod +x "$PATH_CHARGING_SCRIPT"
fi
# If status is charging, run charging script
if [ "$(status)" = "Charging" ]; then
if [[ "$(status)" == "Charging" ]]; then
$PATH_CHARGING_SCRIPT
fi
}
run_low() {
# Check if low script exists
if [ ! -f "$PATH_LOW_SCRIPT" ]; then
# If not, create it
if ! [[ -f "$PATH_LOW_SCRIPT" ]]; then
touch "$PATH_LOW_SCRIPT"
chmod +x "$PATH_LOW_SCRIPT"
fi
# If status is discharging and battery is low, run low script
if [ "$(status)" = "Discharging" ] && [ "$(capacity)" -le "$LOW_BATTERY" ]; then
if [[ "$(status)" == "Discharging" ]] && [[ "$(capacity)" -le "$LOW_BATTERY" ]]; then
$PATH_LOW_SCRIPT
fi
}
run_high() {
# Check if high script exists
if [ ! -f "$PATH_HIGH_SCRIPT" ]; then
# If not, create it
if ! [[ -f "$PATH_HIGH_SCRIPT" ]]; then
touch "$PATH_HIGH_SCRIPT"
chmod +x "$PATH_HIGH_SCRIPT"
fi
# If status is charging and battery is high, run high script
if [ "$(status)" = "Charging" ] && [ "$(capacity)" -ge "$HIGH_BATTERY" ]; then
if [[ "$(status)" == "Charging" ]] && [[ "$(capacity)" -ge "$HIGH_BATTERY" ]]; then
$PATH_HIGH_SCRIPT
fi
}
run_full() {
# Check if full script exists
if [ ! -f "$PATH_FULL_SCRIPT" ]; then
# If not, create it
if ! [[ -f "$PATH_FULL_SCRIPT" ]]; then
touch "$PATH_FULL_SCRIPT"
chmod +x "$PATH_FULL_SCRIPT"
fi
# If status is charging and battery is full, run full script
if [ "$(status)" = "Full" ]; then
if [[ "$(status)" = "Full" ]]; then
$PATH_FULL_SCRIPT
fi
}
start() {
# Run all scripts
run_discharging
run_charging
run_low
@@ -120,27 +98,19 @@ start() {
run_full
}
# CONTROLE ARGUMENTS
# Parse command line arguments
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
while [[ $# -gt 0 ]]; do
case "${1}" in
--folder)
FOLDER_ORIGIN="$2"
shift # past argument
shift # past value
shift 2
;;
--low)
LOW_BATTERY="$2"
shift # past argument
shift # past value
shift 2
;;
--high)
HIGH_BATTERY="$2"
shift # past argument
shift # past value
shift 2
;;
*)
usage "Unknown option: $1"
@@ -148,9 +118,6 @@ do
esac
done
# VARIABLES
PROGNAME=$(basename "$0")
LOW_BATTERY=20
HIGH_BATTERY=80
DISCHARGING_SCRIPT="discharging"
@@ -164,9 +131,8 @@ 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."
if [[ -z "$FOLDER_ORIGIN" ]]; then
printf '%s\n' "Error: The --folder flag is required" >&2
exit 1
else
start

View File

@@ -1,74 +1,48 @@
#!/usr/bin/env bash
# --
# Description: Script that watches when new image (PNG or JPEG) are added and transform to WebP format.
# --
# Requirements: Install webp
# Example Debian: $sudo apt install webp
# --
# Cron: @reboot bash-folders-image-to-webp.sh >/dev/null 2>&1 &
# --
# START
set -e
# FUNCTIONS
usage() {
if [ "$*" != "" ] ; then
echo "Error: $*"
fi
[[ -n "${*}" ]] && printf '%s\n' "Error: ${*}" >&2
cat << EOF
Usage: $PROGNAME [OPTION]
Watches when new image (PNG or JPEG) are added and transform to WebP format.
Options:
--folder [path] Folder path where will be monitored.
USAGE: ${0##*/} [OPTION] --folder PATH
Watches when new image (PNG or JPEG) are added and transform to WebP format
OPTIONS:
--help Display this usage message and exit
--folder PATH PATH to be monitored
EOF
exit 1
}
start() {
# Monitors the selected folder
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" |
while read -r filename; do
# Gets the file extension
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" | while read -r filename; do
extension="${filename##*.}"
# Checks if the extension is in the extension list
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
if [[ "$ext" = "$extension" ]]; then
if [[ "$ext" == "$extension" ]]; then
filename_output="${filename%.*}.webp"
# Displays a flat file of information
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
# Converts the image to WebP
cwebp -q "$QUALITY" "$FOLDER_ORIGIN/$filename" -o "$FOLDER_ORIGIN/$filename_output"
# Remove a flat file of information
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
fi
done
done
}
# CONTROLE ARGUMENTS
# Parse command line arguments
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--folder)
FOLDER_ORIGIN="$2"
shift # past argument
shift # past value
shift 2
;;
--quality)
QUALITY="$2"
shift # past argument
shift # past value
shift 2
;;
*)
usage "Unknown option: $1"
@@ -76,23 +50,18 @@ do
esac
done
# VARIABLES
PROGNAME=$(basename "$0")
QUALITY="90"
MESSAGE_WAITING="converting_please_wait"
EXTENSIONS_TO_WATCH=("jpg" "jpeg" "png")
# CHECKS
# Check if exists cwebp
if ! which cwebp > /dev/null; then
echo "Error: You must install the WebP terminal tools."
if ! command -v cwebp > /dev/null; then
printf '%s\n' "Error: You must install WebP tooling" >&2
exit 1
fi
# Check if the required --folder flag is provided
if [ -z "$FOLDER_ORIGIN" ]; then
echo "Error: The --folder flag is required."
if [[ -z "$FOLDER_ORIGIN" ]]; then
printf '%s\n' "Error: The --folder flag is required" >&2
exit 1
fi

View File

@@ -1,60 +1,37 @@
#!/usr/bin/env bash
# --
# Description: Script that watches when new videos are added to a folder and optimizes them.
# --
# Requirements: Install inotify-tools and ffmpeg
# Example Debian: $sudo apt install ffmpeg
# --
# Cron: @reboot bash-folders-video-optimizer.sh >/dev/null 2>&1 &
# --
# START
set -e
# VARIABLES
PROGNAME=$(basename "$0")
FOLDER_ORIGIN="$2"
EXTENSIONS_TO_WATCH=("mkv" "mp4" "avi" "mov")
MESSAGE_WAITING="optimizing_please_wait"
# FUNCTIONS
usage() {
if [ "$*" != "" ] ; then
echo "Error: $*"
fi
[[ -n "${*}" ]] && printf '%s\n' "Error: ${*}" >&2
cat << EOF
Usage: $PROGNAME [OPTION]
USAGE: ${0##*/} [OPTION] --folder PATH
Watches when new videos are added to a folder and optimizes them.
Options:
--folder [path] Folder path where new video will be monitored and optimized
OPTIONS:
--help Display this usage message and exit
--folder PATH Folder path where new video will be monitored and optimized
EOF
exit 1
}
start() {
# Monitors the selected folder
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" |
while read -r filename; do
# Gets the file extension
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" | while read -r filename; do
extension="${filename##*.}"
# Checks if the extension is in the extension list
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
if [[ "$ext" = "$extension" ]]; then
# Check if the file name starts with "optimized"
if [[ "$filename" != optimized* ]]; then
filename_output="optimized_${filename%.*}.mp4"
# Displays a flat file of information
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
# Convert the file to MP4 format using ffmpeg in /tmp/
ffmpeg -i "$FOLDER_ORIGIN/$filename" -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -nostdin -shortest "/tmp/$filename_output"
# When finished move the optimized file
mv "/tmp/$filename_output" "$FOLDER_ORIGIN/$filename_output"
# Remove a flat file of information
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
fi
fi
@@ -62,10 +39,9 @@ start() {
done
}
# CONTROLE ARGUMENTS
isArg=""
while [ $# -gt 0 ] ; do
while [[ $# -gt 0 ]]; do
case "$1" in
--help)
usage
@@ -83,6 +59,6 @@ while [ $# -gt 0 ] ; do
shift
done
if [ -z $isArg ] ; then
if [[ -z "${isArg}" ]]; then
usage "Not enough arguments"
fi