#!/bin/bash

## Copyright (C) 2023 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

## qubes-raw-backup-adrelanos-shared bails out with exit 1 by default
## until the consumer edits it, so shellcheck flags the body as
## unreachable.
# shellcheck disable=SC2317

set -x
set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
shopt -s inherit_errexit

source /usr/bin/qubes-raw-backup-adrelanos-shared

command -v -- cmp >/dev/null
command -v -- blockdev >/dev/null

## Get the size of the original disk in bytes.
original_size=$(blockdev --getsize64 "$original_disk")

## Using cp with --bytes="$original_size" to ensure that the comparison stops
## at the size of the original disk, even if the backup disk is larger.
## This way, the comparison is not confused if the backup_disk is larger.
pv "$original_disk" | cmp --bytes="$original_size" /dev/stdin "$backup_disk"

## Alternative. Untested!
#ddrescue --verbose --no-scrape --size="$original_size" "$original_disk" "$backup_disk" ~/logfile
