feat(user/bkmpv2):Add bkshuf wrapper for mpv
[BK-2020-03.git] / unitproc / bkbtcblockclock
... / ...
CommitLineData
1#!/bin/bash
2# Desc: Prints latest block from bitcoin-cli
3# Usage: bkbtcblockclock
4# Output: stdout: timestamped block numbers
5# Depends: bitcoin-cli (from bitcoind), jq, gnu coreutils 8.30
6# Version: 0.0.1
7
8#==BEGIN Define script parameters==
9delay="1"
10#===BEGIN Define variables===
11declare -Ag appRollCall # Associative array for storing app status
12declare -Ag fileRollCall # Associative array for storing file status
13declare -Ag dirRollCall # Associative array for storing dir status
14#===END Define variables===
15
16
17#===BEGIN Declare local script functions===
18yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
19die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
20try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
21checkapp() {
22 # Desc: If arg is a command, save result in assoc array 'appRollCall'
23 # Usage: checkapp arg1 arg2 arg3 ...
24 # Version: 0.1.1
25 # Input: global assoc. array 'appRollCall'
26 # Output: adds/updates key(value) to global assoc array 'appRollCall'
27 # Depends: bash 5.0.3
28 local returnState
29
30 #===Process Args===
31 for arg in "$@"; do
32 if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command
33 appRollCall[$arg]="true";
34 if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
35 else
36 appRollCall[$arg]="false"; returnState="false";
37 fi;
38 done;
39
40 #===Determine function return code===
41 if [ "$returnState" = "true" ]; then
42 return 0;
43 else
44 return 1;
45 fi;
46} # Check that app exists
47checkfile() {
48 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
49 # Usage: checkfile arg1 arg2 arg3 ...
50 # Version: 0.1.1
51 # Input: global assoc. array 'fileRollCall'
52 # Output: adds/updates key(value) to global assoc array 'fileRollCall';
53 # Output: returns 0 if app found, 1 otherwise
54 # Depends: bash 5.0.3
55 local returnState
56
57 #===Process Args===
58 for arg in "$@"; do
59 if [ -f "$arg" ]; then
60 fileRollCall["$arg"]="true";
61 if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
62 else
63 fileRollCall["$arg"]="false"; returnState="false";
64 fi;
65 done;
66
67 #===Determine function return code===
68 if [ "$returnState" = "true" ]; then
69 return 0;
70 else
71 return 1;
72 fi;
73} # Check that file exists
74checkdir() {
75 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
76 # Usage: checkdir arg1 arg2 arg3 ...
77 # Version 0.1.1
78 # Input: global assoc. array 'dirRollCall'
79 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
80 # Output: returns 0 if app found, 1 otherwise
81 # Depends: Bash 5.0.3
82 local returnState
83
84 #===Process Args===
85 for arg in "$@"; do
86 if [ -d "$arg" ]; then
87 dirRollCall["$arg"]="true";
88 if ! [ "$returnState" = "false" ]; then returnState="true"; fi
89 else
90 dirRollCall["$arg"]="false"; returnState="false";
91 fi
92 done
93
94 #===Determine function return code===
95 if [ "$returnState" = "true" ]; then
96 return 0;
97 else
98 return 1;
99 fi
100} # Check that dir exists
101displayMissing() {
102 # Desc: Displays missing apps, files, and dirs
103 # Usage: displayMissing
104 # Version 0.1.1
105 # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
106 # Output: stderr: messages indicating missing apps, file, or dirs
107 # Depends: bash 5, checkAppFileDir()
108 local missingApps value appMissing missingFiles fileMissing
109 local missingDirs dirMissing
110
111 #==BEGIN Display errors==
112 #===BEGIN Display Missing Apps===
113 missingApps="Missing apps :";
114 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
115 for key in "${!appRollCall[@]}"; do
116 value="${appRollCall[$key]}";
117 if [ "$value" = "false" ]; then
118 #echo "DEBUG:Missing apps: $key => $value";
119 missingApps="$missingApps""$key ";
120 appMissing="true";
121 fi;
122 done;
123 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
124 echo "$missingApps" 1>&2;
125 fi;
126 unset value;
127 #===END Display Missing Apps===
128
129 #===BEGIN Display Missing Files===
130 missingFiles="Missing files:";
131 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
132 for key in "${!fileRollCall[@]}"; do
133 value="${fileRollCall[$key]}";
134 if [ "$value" = "false" ]; then
135 #echo "DEBUG:Missing files: $key => $value";
136 missingFiles="$missingFiles""$key ";
137 fileMissing="true";
138 fi;
139 done;
140 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
141 echo "$missingFiles" 1>&2;
142 fi;
143 unset value;
144 #===END Display Missing Files===
145
146 #===BEGIN Display Missing Directories===
147 missingDirs="Missing dirs:";
148 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
149 for key in "${!dirRollCall[@]}"; do
150 value="${dirRollCall[$key]}";
151 if [ "$value" = "false" ]; then
152 #echo "DEBUG:Missing dirs: $key => $value";
153 missingDirs="$missingDirs""$key ";
154 dirMissing="true";
155 fi;
156 done;
157 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
158 echo "$missingDirs" 1>&2;
159 fi;
160 unset value;
161 #===END Display Missing Directories===
162
163 #==END Display errors==
164} # Display missing apps, files, dirs
165getBlockNum() {
166 # depends: yell(), die(), try()
167 blockNum="$(try bitcoin-cli getblockchaininfo | try jq '."headers"')"
168 echo "$blockNum";
169}
170printNum() {
171 # depends: yell(), die(), try()
172 # usage printNum arg1
173 echo "[$(date --iso-8601=seconds)]:$1"
174 }
175main() {
176 # Depends: bitcoin-cli (from bitcoind)
177 # Depends: getBlockNum(), printNum()
178
179 # Check dependencies
180 if ! checkapp bitcoin-cli jq date; then
181 yell "ERROR:Apps missing." >&2;
182 displayMissing;
183 exit 1;
184 fi;
185
186 lastBlock="$(getBlockNum)";
187 printNum "$lastBlock";
188 #echo "$lastBlock";
189 n=0;
190 while true; do
191 # update latestBlock
192 latestBlock="$(getBlockNum)";
193
194 # compare last and latestBlock
195 if [[ $latestBlock -gt $lastBlock ]]; then
196 ## true: new block
197 ## print block
198 printNum "$latestBlock";
199 #echo "$(date --iso-8601=seconds)":"$latestBlock"
200 ## update lastBlock
201 lastBlock="$latestBlock"
202 :
203 else
204 ## no new block
205 sleep "$delay"
206 fi;
207 # update counter
208 ((n++));
209 done;
210}
211#===END Declare local script functions===
212#==END Define script parameters==
213
214main "$@";
215
216# Author: Steven Baltakatei Sandoval
217# License: GPLv3+