#!/usr/bin/env bash set -euo pipefail # @describe Generate or verify SHA512 checksums # @meta version 1.0.0 # @meta require-tools sha512sum # @meta combine-shorts # # @flag -s --single-file Use single hash file (default: $HASHIT_SINGLE_FILE_NAME) # @flag -c --check Verify checksums # @flag -f --force Overwrite existing hash files # # @arg files+ Files to hash or verify # # @env HASHIT_SINGLE_FILE_NAME=SHA512SUMS Hash file name for single-file mode check_file_and_fail() { if [ -f "$1" ] && [ -z "${argc_force:-}" ]; then echo "error: file '$1' exists; use -f to force overwrite" >&2 exit 1 fi } main() { if [ -n "${argc_single_file:-}" ]; then if [ -n "${argc_check:-}" ]; then sha512sum -c "$HASHIT_SINGLE_FILE_NAME" else check_file_and_fail "$HASHIT_SINGLE_FILE_NAME" sha512sum "${argc_files[@]?}" >"$HASHIT_SINGLE_FILE_NAME" fi else local hash_file if [ -n "${argc_check:-}" ]; then for file in "${argc_files[@]?}"; do if [[ "$file" == *.sha512 ]]; then hash_file="$file" else hash_file="${file}.sha512" fi sha512sum -c "$hash_file" done else for file in "${argc_files[@]?}"; do hash_file="${file}.sha512" check_file_and_fail "$hash_file" echo "$(sha512sum "$file" | cut -d ' ' -f 1) $(basename "$file")" >"$hash_file" done fi fi } eval "$(argc --argc-eval "$0" "$@")"