From eb1125b83d87f25e05bf94728e7d107fa714649b Mon Sep 17 00:00:00 2001
From: Steven Baltakatei Sandoval <baltakatei@gmail.com>
Date: Tue, 28 Mar 2023 21:06:15 +0000
Subject: [PATCH 1/1] feat(user/check_ots.sh):Handle .ots.bak, .OTS files.

- style(user/check_ots.sh):Organize into bash functions.
---
 user/check_ots.sh | 55 ++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 47 insertions(+), 8 deletions(-)
 mode change 100644 => 100755 user/check_ots.sh

diff --git a/user/check_ots.sh b/user/check_ots.sh
old mode 100644
new mode 100755
index 586e13e..b32dac9
--- a/user/check_ots.sh
+++ b/user/check_ots.sh
@@ -1,14 +1,53 @@
-#!/bin/bash
+#!/usr/bin/env bash
+# Desc: Upgrades and verfies ots files.
+# Usage: check_ots.sh [file]
+# Example: ./check_ots.sh foo.txt.ots
+# Note: Only outputs stderr or stdout on upgrade or verify failures.
+# Version 0.0.2
+# Depends: Bash 5.1.16, GNU Coreutils 8.32 (date), ots 0.7.0
 
+# Plumbing
 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
-try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
+must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
+check_givens() {
+    if [[ ! -f $1 ]]; then die "FATAL:Not a file:$1"; fi;
+    re=".ots$|.OTS$";
+    if [[ ! "$1" =~ $re ]]; then die "FATAL:Not an OTS file extension:$1"; fi;
+    if [[ $# -ne 1 ]]; then die "FATAL:Incorrect argument count:$#"; fi;
+    if ! command -v ots 1>/dev/random 2>&1; then die "FATAL:Not found:ots"; fi;
+};  # Check given assumptions
+upgrade_ots() {
+    # Depends: GNU Coreutils 8.32 (date), ots 0.7.0
+    
+    file_in="$1";
+    file_bak="$file_in".bak ;
 
-if [[ ! -f $1 ]]; then die "FATAL:Not a file:$1"; fi;
+    # # Upgrade ots
+    if [[ ! -f "$file_bak" ]]; then
+        "$(which ots)" u "$file_in" 1>/dev/null 2>&1;
+    else
+        yell "ERROR:Upgrade failed due to existing backup:$file_bak";
+    fi;
+}; # Upgrades ots file
+verify_ots() {
+    # Depends: ots 0.7.0
+    
+    if ! ( ots v "$1" 1>/dev/random 2>&1 ); then
+        yell "ERROR:Verification failed:$1";
+        ots v "$1"; # Show error
+    fi;
+}; # Verify ots file
+main() {
+    must check_givens "$1";
+    must upgrade_ots "$1";
+    must verify_ots "$1";
+}; 
 
-ots u "$1" 1>/dev/null 2>&1;
+main "$@";
 
-if ! ( ots v "$1" 1>/dev/null 2>/dev/null ); then
-    yell "ERROR:Verification failed:$1";
-    ots v "$1";
-fi;
+# Author: Steven Baltakatei Sandoval
+# License: GPLv3+
+
+# ots 0.7.0
+#   See https://opentimestamps.org/
-- 
2.39.5