# shellcheck    shell=sh

___x_cmd_os_kernal(){
    :
}

___x_cmd_os_release_code(){
    ___x_cmd_os_release | ___x_cmd_cmds_awk -v FS="=" '$1=="DISTRIB_CODENAME"{ print $2; exit 0; }'
}

___x_cmd_os_extract(){
    ___x_cmd_cmds_awk -v name="${1:-Provide name}" -v code=1 -v FS='=' '
$1==name { print $2; code=0 }
END { exit code }
'
}

___x_cmd_os_release_name(){
    if [ -f /etc/os-release ] && ___x_cmd_os_extract "ID" </etc/os-release 2>/dev/null; then
        return 0
    fi

    local s

    if s=$( ___x_cmd_cmds_cat /etc/issue 2>/dev/null); then
        # Ubuntu 20.04.2 LTS
        printf  "${s%/*}\n"
        return 0
    fi

    if s=$(___x_cmd_cmds_cat /etc/redhat-release 2>/dev/null); then
        # redhat or centos
        # CentOS Linux release 7.6.1810 (Core)
        if [ s != "${s#CentOS}" ]; then
            printf "%s" centos
        else
            printf "%s" redhat
        fi
        return 0
    fi

    if s=$(___x_cmd_cmds_cat /etc/debian_version 2>/dev/null); then
        printf "%s"  debian
        return 0
    fi

    if s=$(___x_cmd_cmds_cat /etc/slackware_version 2>/dev/null); then
        printf "%s"  slackware
        return 0
    fi

    if [ -f /etc/os-release ] && ___x_cmd_os_extract DISTRIB_ID </etc/lsb-release 2>/dev/null | tr "[:upper:]" "[:lower:]"; then
        return 0
    fi

    uname -s 2>/dev/null
    return
}

# shellcheck disable=SC2120
___x_cmd_os_release(){
    if [ $# -eq 0 ]; then
        ___x_cmd_os_release_all
        return
    fi

    ___x_cmd readl "$@" <<A
$(___x_cmd_os_release_all)
A
}

# #1 release-name
# #2 release-version
# #3 release-codename
___x_cmd_os_release_all(){

    if s=$(___x_cmd_cmds_cat /etc/lsb-release 2>/dev/null); then
        printf "%s" "$s" | ___x_cmd_os_extract "DISTRIB_ID"
        printf "%s" "$s" | ___x_cmd_os_extract "DISTRIB_RELEASE"
        printf "%s" "$s" | ___x_cmd_os_extract "DISTRIB_CODENAME"
        return 0
    fi

    local s
    if s=$(___x_cmd_cmds_cat /etc/os-release 2>/dev/null); then
        printf "%s" "$s" | ___x_cmd_os_extract "ID"
        printf "%s" "$s" | ___x_cmd_os_extract "VERSION_ID"

        return 0

        # NAME="Alpine Linux"
        # ID=alpine
        # VERSION_ID=3.10.0
        # PRETTY_NAME="Alpine Linux v3.10"
        # HOME_URL="https://alpinelinux.org/"
        # BUG_REPORT_URL="https://bugs.alpinelinux.org/"

        # Example 2

        # NAME="CentOS Linux"
        # VERSION="7 (Core)"
        # ID="centos"
        # ID_LIKE="rhel fedora"
        # VERSION_ID="7"
        # PRETTY_NAME="CentOS Linux 7 (Core)"
        # ANSI_COLOR="0;31"
        # CPE_NAME="cpe:/o:centos:centos:7"
        # HOME_URL="https://www.centos.org/"
        # BUG_REPORT_URL="https://bugs.centos.org/"

        # CENTOS_MANTISBT_PROJECT="CentOS-7"
        # CENTOS_MANTISBT_PROJECT_VERSION="7"
        # REDHAT_SUPPORT_PRODUCT="centos"
        # REDHAT_SUPPORT_PRODUCT_VERSION="7"
    fi

    if command -v uname 1>/dev/null 2>/dev/null; then
        uname -s
        uname -r
        return 0
    fi

    return 1

    # local s
    # if s=$(command cat /etc/redhat-release); then
    #     # redhat or centos
    #     # CentOS Linux release 7.6.1810 (Core)
    #     if [ s != ${s#CentOS} ]; then
    #         echo "centos"
    #     else
    #         echo "redhat"
    #     fi
    # fi

    # if s=$(command cat /etc/debian_version); then
    #     echo
    # fi

    # if s=$(command cat /etc/slackware_version); then
    #     echo slackware
    # fi

    # if s=$(/etc/lsb-release); then
    #     # DISTRIB_ID=Ubuntu
    #     # DISTRIB_RELEASE=20.04
    #     # DISTRIB_CODENAME=focal
    #     # DISTRIB_DESCRIPTION="Ubuntu 20.04.2 LTS"
    #     echo ubuntu
    # fi

    # if s=$(command cat /etc/issue); then
    #     # Ubuntu 20.04.2 LTS
    #     echo ubuntu
    # fi

}

