feat(unitproc/bkt-prune_path_rootside):Add Bash function
[BK-2020-03.git] / user / check_ots.sh
CommitLineData
eb1125b8
SBS
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
282cc59d 8
eb1125b8 9# Plumbing
282cc59d
SBS
10yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
11die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
eb1125b8
SBS
12must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
13check_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
20upgrade_ots() {
21 # Depends: GNU Coreutils 8.32 (date), ots 0.7.0
22
23 file_in="$1";
24 file_bak="$file_in".bak ;
282cc59d 25
eb1125b8
SBS
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
33verify_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
41main() {
42 must check_givens "$1";
43 must upgrade_ots "$1";
44 must verify_ots "$1";
45};
282cc59d 46
eb1125b8 47main "$@";
282cc59d 48
eb1125b8
SBS
49# Author: Steven Baltakatei Sandoval
50# License: GPLv3+
51
52# ots 0.7.0
53# See https://opentimestamps.org/