commit d8a7a0e2eab9cf83beb46d5777b168651804cbef Author: Mattia Mascarello Date: Mon May 6 07:58:45 2024 +0200 first commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..05700e2 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..3cc20d2 --- /dev/null +++ b/README.md @@ -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] +``` + +## Examples + +* `sleepuntil -v "tomorrow 9am"` +* `sleepuntil 10pm && execute_evening_task` \ No newline at end of file diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..dbf5406 --- /dev/null +++ b/install.sh @@ -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 \ No newline at end of file diff --git a/sleepuntil b/sleepuntil new file mode 100755 index 0000000..6c5a4e9 --- /dev/null +++ b/sleepuntil @@ -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] " + 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 diff --git a/sleepuntil.1 b/sleepuntil.1 new file mode 100644 index 0000000..4391e72 --- /dev/null +++ b/sleepuntil.1 @@ -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] +.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 +.SH BUGS +Report bugs to: https://github.com/MatMasIt/sleepuntil +.SH COPYRIGHT +Copyright \(co 2024 Mattia Mascarello \ No newline at end of file