# shellcheck shell=dash

___x_cmd log init hash

xrc:mod:lib     hash        sum     assert

# TODO: bcrypt scrypt

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

    local op="$1";                      shift
    case "$op" in
        -h|--help)                      ___x_cmd help -m hash       "$@";    return 0 ;;
        speedup|match|info)             ___x_cmd_hash_"$op"         "$@" ;;
        verify|assert)                  ___x_cmd_hash_assert        "$@" ;;
        md5|sha1|sha256|sha384|sha512)  ___x_cmd_hash_"$op"         "$@" ;;
        --|--auto)                      ___x_cmd_hash_info          "$@" ;;
        *)                              ___x_cmd_hash_info  "$op"   "$@" ;;
                                        # N=hash M="Unsupported hash function -> $op" log:ret:64 ;;
    esac
}

___x_cmd_hash_speedup(){
    local binname
    while read -r binname; do
        ! ___x_cmd_hascmd "$binname" || continue
        ___x_cmd cosmo use "$binname"
    done <<A
md5sum
sha1sum
sha256sum
sha384sum
sha512sum
A

}

___x_cmd_hash_info(){
    local IFS=" "
    local algor=""
    while [ $# -gt 0 ]; do
        for algor in md5 sha1 sha256 sha384 sha512; do
            [ -r "$1" ] ||  N=hash M="Not a readable file -> $1" log:ret:1
            printf "%-8s:  " "$algor"
            ___x_cmd_hash "$algor" "$1" || return
        done
        shift
    done
}
