shell-scripts/habit-tracker
2024-03-03 16:32:28 -06:00

267 lines
10 KiB
Bash
Executable file

#!/bin/bash
############################################################
# Habit Tracker #
# #
# Runs in the background, and sends notifications. #
# #
# Change History #
# 03/11/2023 Out Of Ideas Original code. This is a #
# template for creating new #
# Bash shell script #
# Add new history entries as #
# needed. #
# #
# 01/12/2023 Out Of Ideas Initial resease; script not #
# worknig. #
# #
# 01/12/2023 Out Of Ideas Attempt to fix script, by #
# adjusting `at` syntax. #
# #
# 01/12/2023 Out Of Ideas Adjust comments copied from #
# another script. #
# #
# 01/12/2023 Out Of Ideas Attempt to fix script, by #
# adjusting `at` syntax agian. #
# #
# 01/12/2023 Out Of Ideas Got script working for one #
# habbit. #
# #
# 01/12/2023 Out Of Ideas Trim the space at the end of #
# MESSAGE, and create a test #
# habbit. #
# #
# 01/12/2023 Out Of Ideas Fix formatting. #
# #
############################################################
############################################################
############################################################
# #
# This software is hereby released into the public domain. #
# #
# Anyone is free to copy, modify, publish, use, compile, #
# sell, or distribute this software, either in source code #
# form or as a compiled binary, for any purpose, with or #
# without modification. There are no restrictions or #
# requirements for the use of this software. #
# #
# This software is provided "as is," without any warranty, #
# express or implied, including but not limited to the #
# warranties of merchantability, fitness for a particular #
# purpose, or non-infringement. In no event shall the #
# authors be liable for any claim, damages, or other #
# liability, whether in an action of contract, tort, or #
# otherwise, arising from, out of, or in connection with #
# this software or the use or other dealings in this #
# software. #
# #
# To the fullest extent possible under law, the authors #
# have waived all copyright and patent rights to this #
# software. You are free to use this software without #
# any restrictions on copyright or patent rights. #
# #
# This Public Domain declaration, including the waiver of #
# copyright and patent rights, applies to the full extent #
# permitted by applicable laws and regulations. #
# #
############################################################
############################################################
############################################################
# Help #
############################################################
Help()
{
# Display Help
echo "Runs in the background, and sends notifications"
echo
echo "Syntax: Habit Tracker [-h|p|v|V]"
echo "options:"
echo "h Print this Help."
echo "p Print the Public Domain declaration."
echo "v Verbose mode."
echo "V Print version and exit."
echo
}
############################################################
# Public Domain Declaration #
############################################################
PUBLIC_DOMAIN_DECLARATION='
This software is hereby released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, with or without modification. There are no restrictions or requirements for the use of this software.
This software is provided "as is," without any warranty, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, or non-infringement. In no event shall the authors be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with this software or the use or other dealings in this software.
To the fullest extent possible under law, the authors have waived all copyright and patent rights to this software. You are free to use this software without any restrictions on copyright or patent rights.
This Public Domain declaration, including the waiver of
copyright and patent rights, applies to the full extent
permitted by applicable laws and regulations.
'
############################################################
# Version #
############################################################
VERSION="Habit Tracker v1.1 alpha"
############################################################
# Check for root #
############################################################
CheckRoot()
{
# If we are not running as root, we exit the program.
if [ `id -u` != 0 ]
then
echo "ERROR: You must be root user to run this program"
exit
fi
}
############################################################
# Check for user #
############################################################
CheckUser()
{
# If we are running as root, we exit the program.
if [ `id -u` == 0 ]
then
echo "ERROR: You must NOT be root user to run this program"
exit
fi
}
############################################################
# Check for dependencies #
############################################################
CheckLibnotify()
{
# If 'notify-send' command is not found, we exit the program
if ! type notify-send &> /dev/null; then
echo "Error: 'notify-send' command not found. Please install libnotify."
exit 1
fi
}
CheckAt()
{
# If 'at' command is not found, we exit the program
if ! type at &> /dev/null; then
echo "Error: 'at' command not found. Please install at."
exit 1
fi
}
############################################################
# Main Program #
############################################################
############################################################
# Sanity checks #
############################################################
# Are we running as root?
#CheckRoot
CheckUser
# Is 'notify-send' comand found?
CheckLibnotify
# Is 'at' comand found?
CheckAt
############################################################
# Initialize variables #
############################################################
option=""
Msg=""
# Time
HOUR="3600000"
MINUTE="60000"
SECOND="1000"
# Habits
# HABIT TIME DURATION Message
# PRACTICE=("09:00" "$HOUR" "Practice")
TEST=("$(date -d '+1 minute' '+%H:%M')" "$MINUTE" "This is a test")
PRACTICE_GUITAR=("17:00" "$((2 * $HOUR))" "Practice Guitar")
PRACTICE_KARATE=("19:00" "$((2 * $HOUR))" "Practice Karate")
STRETCH=("19:00" "$((2 * $HOUR))" "Stretch")
SING=("15:00" "$((2 * $HOUR))" "Sing")
HABITS=("${PRACTICE_GUITAR[@]}" "${PRACTICE_KARATE[@]}" "${STRETCH[@]}" "${SING[@]}")
# Unset variables
unset VERBOSE
############################################################
# Initialize Functions #
############################################################
trim_string() {
# Usage: trim_string " example string "
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
############################################################
# Process the input options. Add options as needed. #
############################################################
# Get the options
while getopts ":hpvV" option;
do
case $option in
h) # display Help
Help
exit;;
p) # display $PUBLIC_DOMAIN_DECLARATION
echo $PUBLIC_DOMAIN_DECLARATION
exit;;
v) # enable $VERBOSE
VERBOSE=true;;
V) # display $VERSION
echo $VERSION
exit;;
\?) # incorrect option
echo "Error: Invalid option"
exit;;
esac
done
# Display a message before the program starts
#echo "$Msg"
# Get number of habits
n=$(( ${#HABITS[@]} / 3))
for ((i=0; i<$n; i++));
do
HABIT="${HABITS[@]:3*i:3}"
if [ $VERBOSE ];
then
echo $HABIT
fi
DURATION=$(echo "$HABIT" | awk '{ print $2 }')
MESSAGE=$(trim_string "$(echo "$HABIT" | awk '{ str=""; for (i = 3; i <= NF; i++) str = str $i " "; print str }')")
TIME=$(echo "$HABIT" | awk '{ print $1 }')
if [ $VERBOSE ];
then
echo "DURATION = $DURATION"
echo "MESSAGE = $MESSAGE"
echo "TIME = $TIME"
fi
if [ $VERBOSE ];
then
echo "echo 'DISPLAY=:0 notify-send -t \"$DURATION\" \"Habit Tracker\" \"$MESSAGE\"' | at $TIME"
echo "DISPLAY=:0 notify-send -t \"$DURATION\" \"Habit Tracker\" \"$MESSAGE\"" | at "$TIME"
else
echo "DISPLAY=:0 notify-send -t \"$DURATION\" \"Habit Tracker\" \"$MESSAGE\"" | at "$TIME"
fi
done