# shellcheck shell=dash

# pfingerprint
# information
# Problably using subshell id
___x_cmd_ps_fingerprint(){
    local op="$1"; shift
    case "$op" in
        check)     ___x_cmd_ps_fingerprint_check "$@" ;;
        get|get_|toepoch|toepoch_)
                    ___x_cmd_ps_fingerprint_"$op" "$@" ;;
        *)         N=ps M="fingerprint no such option '$op'" log:ret:1 ;;
    esac
}

___x_cmd_ps_fingerprint_get(){
    local pid="${1:-$$}"
    local x_
    ___x_cmd_ps_fingerprint_get_ "$pid"
    printf "%s\n" "$x_"
}

___x_cmd_ps_fingerprint_check(){
    local pid="$1"
    local fingerprint="$2"
    pid="${pid:-$$}"

    if [ -z "$fingerprint" ]; then
        ___x_cmd_ps_fingerprint_check___rough "$pid"
    else
        ___x_cmd_ps_fingerprint_check___detail "$fingerprint" "$pid"
    fi
}

# https://man7.org/linux/man-pages/man5/proc.5.html
# 22 is starttime
___x_cmd_ps_fingerprint_check___detail(){
    local fingerprint="$1"
    local pid="${2:-$$}"
    local x_=;

    ___x_cmd_ps_fingerprint_get_ "$pid" || return $?
    [ "$x_" = "$fingerprint" ]
}

if [ -d /proc ]; then

___x_cmd_ps_fingerprint_get_(){
    local pid="${1:-$$}"

    local fp="/proc/$pid/stat"
    local IFS=" "
    local data=; local tmp
    [ -r "$fp" ]        || return 1
    read -r data <"$fp" || return $?
    tmp="${data#*)* * * * * * * * * * * * * * * * * * * * }"
    x_="${tmp%% *}"
}

___x_cmd_ps_fingerprint_check___rough(){
    local pid="${1:-$$}"
    [ -e "/proc/$pid" ]
}

___x_cmd_ps_fingerprint_toepoch_(){
    local tick="$1"
    ___x_cmd btime_
    x_=$(( x_ + (tick / 100) ))
}

else

___x_cmd_ps_fingerprint_get_(){
    local pid="${1:-$$}"
    x_=""

    local data=
    data="$(LC_ALL="$___X_CMD_LOCALE_DEF_C" ___x_cmd_cmds ps -o lstart -p "$pid")" || return

    local IFS=" "
    local tmp
    {
        read -r tmp
        read -r x_ || return 1
    } <<A
$data
A

}

___x_cmd_ps_fingerprint_check___rough(){
    local pid="${1:-$$}"
    ___x_cmd_cmds kill -0 "$pid" >/dev/null 2>&1
}

# x_ = "Wed Apr  8 05:24:09 2026"
___x_cmd_ps_fingerprint_toepoch_(){
    x_="$1"
    x_="$(date -j -f "%a %b %e %H:%M:%S %Y" "$x_" "+%s" 2>/dev/null)" || return
}

fi

___x_cmd_ps_fingerprint_toepoch(){
    local x_=""
    ___x_cmd_ps_fingerprint_toepoch_ "$@" || return $?
    printf "%s\n" "$x_"
}