#!/usr/bin/env bash set -Eeuo pipefail usage() { printf 'Usage: %s [--all|--blake3|--sha256]\n' "${0##*/}" } mode=all if [[ $# -gt 1 ]]; then usage >&2 exit 2 fi if [[ $# -eq 1 ]]; then case "$1" in --all) mode=all ;; --blake3) mode=blake3 ;; --sha256) mode=sha256 ;; -h|--help) usage; exit 0 ;; *) usage >&2; exit 2 ;; esac fi script_dir="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)" repo_root="$(CDPATH= cd -- "$script_dir/.." && pwd)" data_dir="$repo_root/data" catalog="$repo_root/catalog.tsv" artifact="$data_dir/artifact.yaml" [[ -f "$catalog" ]] || { printf 'ERROR: missing catalog.tsv\n' >&2; exit 1; } [[ -f "$artifact" ]] || { printf 'ERROR: missing data/artifact.yaml\n' >&2; exit 1; } catalog_id="$(awk -F '\t' 'NR == 2 {print $1}' "$catalog")" catalog_path="$(awk -F '\t' 'NR == 2 {print $2}' "$catalog")" metadata_id="$(awk '$1 == "id:" {print $2; exit}' "$artifact")" [[ "$catalog_path" == data ]] || { printf 'ERROR: catalog path is not data\n' >&2; exit 1; } [[ "$catalog_id" == "$metadata_id" ]] || { printf 'ERROR: catalog ID %s differs from artifact ID %s\n' "$catalog_id" "$metadata_id" >&2 exit 1 } temporary="$(mktemp -d "${TMPDIR:-/tmp}/pop-1kgp-verify.XXXXXX")" trap 'rm -rf -- "$temporary"' EXIT ( cd "$data_dir" find . -maxdepth 1 -type f \ \( -name 'chr*.pgen.zst' -o -name 'chr*.pvar.zst' -o -name 'chr*.psam' \) \ -print | sed 's#^./##' | LC_ALL=C sort ) > "$temporary/payloads" payload_count="$(wc -l < "$temporary/payloads" | tr -d ' ')" [[ "$payload_count" == 75 ]] || { printf 'ERROR: expected 75 chromosome payloads, found %s\n' "$payload_count" >&2 exit 1 } for extension in pgen.zst pvar.zst psam; do count="$(awk -v suffix=".$extension" 'index($0, suffix) == length($0) - length(suffix) + 1 {n++} END {print n+0}' "$temporary/payloads")" [[ "$count" == 25 ]] || { printf 'ERROR: expected 25 .%s files, found %s\n' "$extension" "$count" >&2 exit 1 } done verify_coverage() { local checksum_file="$1" awk '{print $2}' "$checksum_file" | sed 's#^\*##; s#^\./##' | LC_ALL=C sort > "$temporary/checksum-paths" if ! cmp -s "$temporary/payloads" "$temporary/checksum-paths"; then printf 'ERROR: %s does not cover exactly the 75 payload files\n' "${checksum_file##*/}" >&2 diff -u "$temporary/payloads" "$temporary/checksum-paths" >&2 || true exit 1 fi } if [[ "$mode" == all || "$mode" == sha256 ]]; then sha_file="$data_dir/checksums.sha256" [[ -f "$sha_file" ]] || { printf 'ERROR: missing data/checksums.sha256\n' >&2; exit 1; } verify_coverage "$sha_file" if command -v sha256sum >/dev/null 2>&1; then sha_check=(sha256sum --check) elif command -v shasum >/dev/null 2>&1; then sha_check=(shasum -a 256 --check) else printf 'ERROR: sha256sum or shasum is required\n' >&2 exit 127 fi printf 'Verifying SHA-256 payload hashes...\n' (cd "$data_dir" && "${sha_check[@]}" checksums.sha256) fi if [[ "$mode" == all || "$mode" == blake3 ]]; then b3_file="$data_dir/checksums.blake3" [[ -f "$b3_file" ]] || { printf 'ERROR: missing data/checksums.blake3\n' >&2; exit 1; } verify_coverage "$b3_file" command -v b3sum >/dev/null 2>&1 || { printf 'ERROR: b3sum is required; install it with: brew install b3sum\n' >&2 exit 127 } printf 'Verifying BLAKE3 payload hashes...\n' (cd "$data_dir" && b3sum --check checksums.blake3) fi printf 'Verified one catalogued artifact and %s payload files using %s.\n' "$payload_count" "$mode"