#!/bin/bash

# This is free and unencumbered software 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, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# 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 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.
#
# For more information, please refer to <https://unlicense.org/>

# notweerdmonk 2026

# play rattles on the terminal

function bell {
  [[ -z "${1}" ]] && echo "Provide duration" && return 1

  ! [[ "${1}" =~ ^(0x[[:xdigit:].]+|[0-9.]+)[smhd]?$ ]] && \
    echo "Invalid duration" && return 1

  tput bel
  sleep "${1}" > /dev/null 2>&1
  [[ "${?}" -ne 0 ]] && echo "Invalid durtation" && return 1

  while true
  do
    tput bel
    sleep "${1}" > /dev/null 2>&1
  done
}

function index {
  local element="$1"
  local -n array="${2}"
  for index in "${!array[@]}"
  do
    [[ "${array[$index]}" == "$element" ]] && \
      echo "${index}" && return 0
  done
  echo "-1"
}

function set_mypath {
  declare -g g_mypath="$(realpath "${0}")"
}

function rattles {
  command -v infocmp > /dev/null 2>&1 && \
    [[ -z "$(infocmp | grep -oP "bel=[^,]+,{1}")" ]] && \
    echo "Terminal does not support bell" && exit 1

  declare -a tims=(0.2 0.3 0.5 0.7 0.9 1.1 1.3 1.5)

  count="${1}"

  (
    ! [[ "${count}" =~ ^[0-9]+$ ]] || \
    [[ "${count}" -lt 1 ]] || \
    [[ "${count}" -gt 8 ]]
  ) && echo "Too many rattles" && return 1

  while [[ "${count}" -gt 0 ]]
  do
    ((count--))
    t=("$(shuf -n1 -e "${tims[@]}")")
    bell_tims+=("${t}")
    unset "tims["$(index "${t}" tims)"]"
  done

  echo "${bell_tims[@]}" | xargs -n 1 -P "${1}" "${g_mypath}" "bell"
}

[[ -z "${1}" ]] && exit 1

[[ "${1}" =~ ^-?h(elp)?$ ]] && \
  {
    echo -e "rattles - play rattles on the terminal"
    echo
    echo -e "USAGE"
    echo -e "\trattles <ARG> [<DURATION>]"
    echo
    echo -e "DESCRIPTION"
    echo -e "\tThis command accepts a first argument that can be:"
    echo -e "\t- Number of rattles to play, should be a positive integer or hex number. "
    echo
    echo -e "\t- \"bell\": Rings the terminal bell. Requires the duration inbetween bell"
    echo -e "\t rings (a positive integer or hex number) as second argument."
    echo
    echo -e "\tA rattle consists of a terminal bell repeating at certain interval which"
    echo -e "\t is chosen randomly from a set. Maximum of eight rattles can be played."
    echo
    echo -e "ARGUMENTS"
    echo -e "\t<ARG>"
    echo -e "\t\tN\tThe number for rattles to play."
    echo -e "\t\t\tA positive integer or hex number."
    echo -e "\t\t\"bell\"\tRing the terminal bell repeatedly with "
    echo -e "\t\t\tprovided duration."
    echo
    echo -e "\t<DURATION>"
    echo -e "\t\tA floating point number with an optional suffix that can"
    echo -e "\t\t be either of 's' for seconds (the default), 'm' for "
    echo -e "\t\tminutes, 'h' for hours or 'd' for days. A duration of 0 "
    echo -e "\t\tdisables the associated timeout."
    echo -e "\t\tThis shall be the duration with which the terminal bell "
    echo -e "\t\trepeats ringing if "bell" is used. Otherwise this shall "
    echo -e "\t\tbe the duration till which the rattle is played."
    echo
  } && exit 0

[[ "${1}" == "bell" ]] && (bell "${2}" || true) && exit 0

set_mypath

[[ -z "${2}" ]] && (rattles "${1}" || true) && exit 0

! [[ "${2}" =~ ^(0x[[:xdigit:].]+|[[0-9.]+)[smhd]?$ ]] && \
  echo "Invalid duration" && exit 1

timeout "${2}" "${g_mypath}" "${1}"
