# shellcheck shell=dash

___x_cmd log init sleep

xrc:mod:lib     sleep       beacon

___x_cmd_sleep___main(){
    [ $# -gt 0 ]    ||  set -- --runmain

    local op="$1";      shift
    case "$op" in
        -h|--help)      ___x_cmd help -m sleep;     return 0 ;;

        schd|beacon)    ___x_cmd_sleep_"$op"            "$@" ;;
        --runmain)      ___x_cmd_sleep___runmain        "$@" ;;
        *)              ___x_cmd_sleep___runmain  "$op" "$@" ;;
    esac
}

___x_cmd_sleep___runmain(){
    [ $# -gt 0 ]    ||      set -- --help

    case "$1" in
        -h|--help)          ___x_cmd help -m sleep;     return 0 ;;
    esac

    local duration="$1";    shift

    local x_
    if  ___x_cmd humantime tosec_ "$duration"; then
        duration="$x_"
    fi

    sleep:debug "Sleep in seconds: $x_"
    ___x_cmd_cmds_sleep "$x_" || {
        sleep:info "Schedule interrupted during sleep. [SIGINT]"
        return 130
    }

    case "$1" in
        -|--)       shift ;;
    esac

    [ $# -eq 0 ] || "$@"
}
