# 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_)  ___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
    [ ! -e "$fp" ] || read -r data <"$fp"
    tmp="${data#*)* * * * * * * * * * * * * * * * * * * * }"
    x_="${tmp%% *}"
}

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

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
}

fi
