first commit

This commit is contained in:
Mattia Mascarello 2024-05-06 07:58:45 +02:00
commit d8a7a0e2ea
5 changed files with 215 additions and 0 deletions

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Mattia Mascarello
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS 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 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

26
README.md Normal file
View File

@ -0,0 +1,26 @@
# sleepuntil
Sleep Until is a Bash script that allows you to suspend execution until a specified date and time is reached. It provides a convenient way to schedule tasks or delay actions until a desired moment.
## Requirements
- Bash (Bourne Again Shell)
- Unix-like operating system (Linux, macOS, OpenBSD, ...)
## Installation
1. Clone this repository
2. `chmod +x install.sh` (make it executable)
3. Run `./install.sh`. This will install the script system-wide
4. If your system is not supported for automatic installation, proceed as illustrated by the install script
## Usage
```bash
sleepuntil [-v] <datetime>
```
## Examples
* `sleepuntil -v "tomorrow 9am"`
* `sleepuntil 10pm && execute_evening_task`

72
install.sh Executable file
View File

@ -0,0 +1,72 @@
#!/bin/bash
# Check if script is run with sudo
if [ "$(id -u)" != "0" ]; then
echo "This script must be run with sudo." 1>&2
exit 1
fi
# Function to copy files
install_files() {
cp "$1" "$2"
if [ $? -ne 0 ]; then
echo "Failed to copy $1 to $2" 1>&2
exit 1
fi
}
# Function to display installation success message
installation_success() {
echo "Installation complete."
}
# Function to provide manual installation instructions
manual_installation() {
echo "Manual installation steps:"
echo "1. Make the script '$script_path' executable (usually chmod +x '$script_path')"
echo "1. Copy '$script_path' to a directory in your PATH."
echo "2. Copy the man page '$man_page_path' to the appropriate man page directory."
echo "3. Update the man page database using the appropriate command for your system."
}
# Detect operating system and set installation variables accordingly
case "$(uname -s)" in
Linux*)
install_dir="/usr/local/bin"
man_dir="/usr/share/man/man1"
man_db_command="mandb"
;;
Darwin*)
install_dir="/usr/local/bin"
man_dir="/usr/share/man/man1"
man_db_command="makewhatis"
;;
OpenBSD*)
install_dir="/usr/local/bin"
man_dir="/usr/share/man/man1"
man_db_command="makewhatis"
;;
*)
echo "Unsupported operating system. Manual installation required."
manual_installation
exit 1
;;
esac
# Path to your script and man page
script_path="sleepuntil"
man_page_path="sleepuntil.1"
chmod +x $script_path
# Install the script
install_files "$script_path" "$install_dir"
# Install the man page
install_files "$man_page_path" "$man_dir"
# Update man page database
$man_db_command "$man_dir"
# Display installation success message
installation_success

66
sleepuntil Executable file
View File

@ -0,0 +1,66 @@
#!/bin/bash
# Function to calculate seconds until a given datetime
seconds_until() {
target="$1"
current=$(date +%s)
target=$(date -d "$target" +%s)
echo $((target - current))
}
# Function to sleep until a given datetime
sleep_until() {
target="$1"
seconds=$(seconds_until "$target")
if [ $seconds -gt 0 ]; then
if [ "$verbose" == true ]; then
echo "Sleeping until $target..."
fi
sleep $seconds
fi
}
# Function to display help information
display_help() {
echo "Usage: sleepuntil [-v] <datetime>"
echo "Options:"
echo " -v Enable verbose mode"
echo " -h Display this help message"
echo ""
echo "More information at man sleepuntil"
}
# Main script starts here
verbose=false
# Parse options
while getopts ":vh" opt; do
case ${opt} in
v )
verbose=true
;;
h )
display_help
exit 0
;;
\? )
echo "Invalid option: -$OPTARG" 1>&2
display_help
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ $# -ne 1 ]; then
echo "Error: Missing datetime argument" 1>&2
display_help
exit 1
fi
target_datetime="$1"
sleep_until "$target_datetime"
if [ "$verbose" == true ]; then
echo "Time reached, stopping."
fi

30
sleepuntil.1 Normal file
View File

@ -0,0 +1,30 @@
.TH sleep_until 1 "May 2024" "1.0" "sleep_until man page"
.SH NAME
sleep_until \- sleep until a specified datetime
.SH SYNOPSIS
.B sleep_until
[\-v] <datetime>
.SH DESCRIPTION
The
.B sleep_until
command sleeps until the specified datetime is reached.
.SH OPTIONS
.TP
.BR \-v
Enable verbose mode.
.TP
.BR \-h
Help message.
.SH EXAMPLES
.TP
.BR sleep_until " 'tomorrow 8:00'"
Sleep until tomorrow 8:00.
.TP
.BR sleep_until " 10pm " && " evening_task"
Sleeps until 10pm, then evening_task is executed.
.SH AUTHOR
Mattia Mascarello <mattia.mascarello@edu.unito.it>
.SH BUGS
Report bugs to: https://github.com/MatMasIt/sleepuntil
.SH COPYRIGHT
Copyright \(co 2024 Mattia Mascarello