#!/bin/sh
#
# This script will walk the directory it is run from and verify the
# SHA-512 digests found in any 'SHA512' files.  You can use it to
# verify a mirrored release tree.
#
# Copyright David Cantrell <dcantrell@burdell.org>
# SPDX-License-Identifier: Apache-2.0

PATH=/bin:/usr/bin
CWD="$(realpath "$(pwd)")"

find "${CWD}" -type f -name SHA512 | while read -r fname ; do
    dname="$(dirname "${fname}")"
    cd "${dname}" || exit 1
    while read -r first digest ; do
        filename="$(echo "${first}" | cut -d '(' -f 2 | cut -d ')' -f 1)"
        computed="$(openssl dgst -sha512 "${filename}" | rev | cut -d ' ' -f 1 | rev)"
        printf "%s: " "${filename}"
        if [ "${digest}" = "${computed}" ]; then
            printf "OK\n"
        else
            printf "FAILED\n"
        fi
    done < SHA512
done
