feat(user/zeropad.sh):Permit ignoring of file extension via arg
[BK-2020-03.git] / user / check_ots.sh
1 #!/usr/bin/env bash
2 # Desc: Upgrades and verfies ots files.
3 # Usage: check_ots.sh [file]
4 # Example: ./check_ots.sh foo.txt.ots
5 # Note: Only outputs stderr or stdout on upgrade or verify failures.
6 # Version 0.0.2
7 # Depends: Bash 5.1.16, GNU Coreutils 8.32 (date), ots 0.7.0
8
9 # Plumbing
10 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
11 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
12 must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
13 check_givens() {
14 if [[ ! -f $1 ]]; then die "FATAL:Not a file:$1"; fi;
15 re=".ots$|.OTS$";
16 if [[ ! "$1" =~ $re ]]; then die "FATAL:Not an OTS file extension:$1"; fi;
17 if [[ $# -ne 1 ]]; then die "FATAL:Incorrect argument count:$#"; fi;
18 if ! command -v ots 1>/dev/random 2>&1; then die "FATAL:Not found:ots"; fi;
19 }; # Check given assumptions
20 upgrade_ots() {
21 # Depends: GNU Coreutils 8.32 (date), ots 0.7.0
22
23 file_in="$1";
24 file_bak="$file_in".bak ;
25
26 # # Upgrade ots
27 if [[ ! -f "$file_bak" ]]; then
28 "$(which ots)" u "$file_in" 1>/dev/null 2>&1;
29 else
30 yell "ERROR:Upgrade failed due to existing backup:$file_bak";
31 fi;
32 }; # Upgrades ots file
33 verify_ots() {
34 # Depends: ots 0.7.0
35
36 if ! ( ots v "$1" 1>/dev/random 2>&1 ); then
37 yell "ERROR:Verification failed:$1";
38 ots v "$1"; # Show error
39 fi;
40 }; # Verify ots file
41 main() {
42 must check_givens "$1";
43 must upgrade_ots "$1";
44 must verify_ots "$1";
45 };
46
47 main "$@";
48
49 # Author: Steven Baltakatei Sandoval
50 # License: GPLv3+
51
52 # ots 0.7.0
53 # See https://opentimestamps.org/