#!/bin/bash

# bgtimer - background timer
# Copyright (C) 2026 notweerdmonk
#
# 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.

## @file bgtimer
## @author notweerdmonk
## @brief Register SIGUSR1 handler in a shell to execute a command and start a
## timer.
##
## 2026
##
## Register/derigister signal handler. Command(s) to be executed on timeout are
## stored in BACKGROUND_TASK_TIMER_CMD environment variable and can be modified.
##
## PID of the target shell is required to start the timer. Time can be provided
## in RFC 2822 or ISO 8601 formats.

# Name of this script
scriptname="$(basename "${BASH_SOURCE[0]}")"
scriptpath="$(realpath "$scriptname")"

# Courtesy: mklement0 via Stack Overflow
# https://stackoverflow.com/a/28776166
# https://stackoverflow.com/users/45375/mklement0
function is_sourced {
  case ${0##*/} in bash|-bash) return 0;; esac
  return 1  # NOT sourced.
}

is_sourced && sourced=1 || sourced=0

# (De)register SIGUSR1 handler or modify timer command
[[ $sourced -gt 0 ]] && \
  {
    {
      [[ -z "$1" || $1 == help ]] && \
        {
          echo    "  Usage: source $(basename ${BASH_SOURCE[0]}) <cmd>" >&2
          echo >&2
          echo    "  Source script to (de)register SIGUSR1 action with timer commands" >&2
          echo >&2
          echo    "  Arguments" >&2
          echo >&2
          echo -e "\tcommand\t\tModify the command carried out by the timer" >&2
          echo >&2
          echo -e "\tdisarm\t\tDeregister timer handler from current shell" >&2
          echo -e "\tdisable" >&2
          echo -e "\tcancel" >&2
          echo >&2
          return 0
        }
    } ||
    [[ "$1" == command ]] && export BACKGROUND_TASK_TIMER_CMD="$2" || \
      {
        trap_handler_name=__"${scriptname/ /_}"_sigusr1_trap
        [[ "$1" == disarm || "$1" == disable || "$1" == cancel  ]] && \
          {
            FIFO="/tmp/bash_wakeup_pipe_$$"
            BGTIMER_STORAGE="/dev/shm/bgtimer_storage_$$"
            [[ -n "$BGTIMER_READER_PID" ]] && kill "$BGTIMER_READER_PID" 2>/dev/null
            unset BGTIMER_READER_PID
            [[ -f "$BGTIMER_STORAGE" ]] && \
              {
                read -r PREV_SIGWINCH_ACTION < <(cat "$BGTIMER_STORAGE")
                trap "${PREV_SIGWINCH_ACTION:--}" SIGWINCH
                rm -f "$BGTIMER_STORAGE"
              }
            rm -f "$FIFO"
            trap - SIGUSR1
            unset "$trap_handler_name"
            unset BACKGROUND_TASK_TIMER_CMD
            return 0
          } || \
            {
              [[ "$1" == "-p" ]] && \
                {
                  shift
                  [[ "$1" == - ]] && \
                      export BACKGROUND_TASK_TIMER_CMD="tput bel" || \
                      export BACKGROUND_TASK_TIMER_CMD="$1"
                  FIFO="/tmp/bash_wakeup_pipe_$$"
                  [[ -n "$BGTIMER_READER_PID" ]] && \
                    kill "$BGTIMER_READER_PID" 2>/dev/null
                  rm -f "$FIFO"
                  mkfifo "$FIFO"
                  eval "function $trap_handler_name {
                    cmd=\"\$BACKGROUND_TASK_TIMER_CMD\"
                    [[ -z \$cmd ]] && cmd=\"\$@\"
                    [[ -n \$cmd ]] && eval \"\$cmd\"
                  }"
                  declare -f "$trap_handler_name"
                  BGTIMER_STORAGE="/dev/shm/bgtimer_storage_$$"
                  PREV_SIGWINCH_ACTION=$(trap -p SIGWINCH | sed -E "s/^trap -- '//; s/' SIGWINCH$//; s/'\\\\''/'/g")

                  echo "$PREV_SIGWINCH_ACTION" > "$BGTIMER_STORAGE"
                  trap "${PREV_SIGWINCH_ACTION:+${PREV_SIGWINCH_ACTION}; }echo -n" SIGWINCH
                  (
                    while [[ -p "$FIFO" ]]; do
                      # This blocks natively inside the OS layer, consuming 0% CPU
                      read line < "$FIFO" && \
                        # PUT YOUR ACTUAL COMMAND PAYLOAD HERE
                        # (e.g., executing scripts, modifying a status, etc.)
                        "$trap_handler_name" "$1" && \
                        kill -SIGWINCH $$
                    done
                  ) 2>/dev/null &
                  BGTIMER_READER_PID=$!
                  disown "$BGTIMER_READER_PID"
                  echo "Registered alarm for $$"
                } || \
                {
                  BGTIMER_STORAGE="/dev/shm/bgtimer_storage_$$"
                  PREV_SIGWINCH_ACTION=$(\
                    trap -p SIGWINCH | \
                    sed -E "s/^trap -- '//; s/' SIGWINCH$//; s/'\\\\''/'/g"\
                  )
                  echo "$PREV_SIGWINCH_ACTION" > "$BGTIMER_STORAGE"
                  trap "${PREV_SIGWINCH_ACTION:+${PREV_SIGWINCH_ACTION}; }echo -n" SIGWINCH
                  [[ "$1" == - ]] && \
                      export BACKGROUND_TASK_TIMER_CMD="tput bel" || \
                      export BACKGROUND_TASK_TIMER_CMD="$1"
                  eval "function $trap_handler_name {
                    cmd=\"\$BACKGROUND_TASK_TIMER_CMD\"
                    [[ -z \$cmd ]] && cmd=\"\$@\"
                    [[ -n \$cmd ]] && eval \"\$cmd\"
                    kill -SIGWINCH $$
                  }"
                  declare -f "$trap_handler_name"
                  trap "$trap_handler_name \"$1\"" SIGUSR1
                  echo "Registered $trap_handler_name for SIGUSR1"
                }
            }
      }
    return 0
  }

# Arguments not provided
[[ $# -lt 2 ]] && \
  {
    scriptname="$(basename "${BASH_SOURCE[0]}")"
    echo    "  $scriptname - shell background timer" >&2
    echo >&2
    echo    "  Source script to (de)register SIGUSR1 action with timer commands" >&2
    echo >&2
    echo    "  USAGE" >&2
    echo -e "\t$scriptname [-c pid]" >&2
    echo -e "\t$scriptname [-d seconds] pid" >&2
    echo -e "\t$scriptname time (RFC 2822 or ISO 8601) pid" >&2
    echo >&2
    echo    "  OPTIONS" >&2
    echo -e "\t-c\t\tCancel the timer process with this pid (printed upon start)" >&2
    echo -e "\t-d\t\tTimer duration from now in seconds" >&2
    echo >&2
    exit 0
  }

[[ -n "$1" && "$1" =~ ^-c$ ]] && \
  {
    target_pid="$2"
    [[ "$target_pid" =~ ^[0-9]+$ ]] || \
      {
        echo "$scriptname: invalid pid: $target_pid" >&2
        exit 1
      }
    kill "$target_pid"
    echo "Timer with pid: $target_pid cancelled"
    exit 0
  }

[[ -n "$1" && "$1" =~ ^-d$ ]] && \
  {
    [[ "$2" =~ ^[0-9]+$ ]] || \
      {
        echo "$scriptname: -d requires a non-negative integer of seconds" >&2
        exit 1
      }
    delta="$2"
    time_till="$(($(date +"%s%N") + $(($delta * 1000000000))))"
    time="$(date -d @"$(($time_till / 1000000000))")"
    target_pid="$3"
  } || \
  {
    time="$1"
    target_pid="$2"
  }

[[ "$target_pid" =~ ^[0-9]+$ ]] || \
  {
    echo "$scriptname: invalid pid: $target_pid" >&2
    exit 1
  }

# Background timer
[[ -z "$time_till" ]] && time_till=$(date -d "$time" +%s%N)
while [[ $(date +%s%N) -lt $time_till ]]; do sleep 0.01; done && \
  {
    FIFO="/tmp/bash_wakeup_pipe_$target_pid"
    [[ -p "$FIFO" ]] && \
      {
        echo  > "$FIFO"
      } || \
      {
        [[ -f "/dev/shm/bgtimer_storage_$target_pid" ]] && \
          {
            kill -SIGUSR1 "$target_pid"
            kill -SIGWINCH "$target_pid"
          }
      }
  } &


timer_pid="$!"

echo -e \
"Timer started till time: $time\n"\
"  Shell pid: $target_pid\n"\
"  Timer pid: $timer_pid"
