fix(bkgpslog):Add missing fi
[EVA-2020-02.git] / exec / bkgpslog
... / ...
CommitLineData
1#!/bin/bash
2
3# Desc: Records gps data until midnight
4# Author: Steven Baltakatei Sandoval; License: GPLv3+
5# Usage: bkgpslog --output [output dir]
6
7#==BEGIN Define script parameters==
8PATH="/opt/bktei:$PATH" # Add default baltakatei script install directory to PATH (necessary for other bk scripts)
9SCRIPT_HOSTNAME=$(hostname) # Save hostname of system running this script.
10SCRIPT_VERSION="bkgpslog 0.0.1" # Define version of script.
11SCRIPT_TIME_SHORT="$(date +%Y%m%dT%H%M%S%z)" # Save current date & time in ISO-8601 format.
12SCRIPT_DATE_SHORT="$(date +%Y%m%d)" # Save current date in ISO-8601 format.
13
14declare -Ag appRollCall # Associative array for storing app status
15declare -Ag fileRollCall # Associative array for storing file status
16declare -Ag dirRollCall # Associative array for storing dir status
17declare -a recPubKeys # for processArguments function
18declare recipients # for main function
19
20#===BEGIN Declare local script functions===
21checkapp() {
22 # Desc: If arg is a command, save result in assoc array 'appRollCall'
23 # Usage: checkapp arg1 arg2 arg3 ...
24 # Input: global assoc. array 'appRollCall'
25 # Output: adds/updates key(value) to global assoc array 'appRollCall'
26 local returnState
27 #echo "DEBUG:$(date +%S.%N)..Starting checkapp function."
28 #echo "DEBUG:args: $@"
29 #echo "DEBUG:returnState:$returnState"
30
31 #===Process Args===
32 for arg in "$@"; do
33 #echo "DEBUG:processing arg:$arg"
34 if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command
35 appRollCall[$arg]="true";
36 #echo "DEBUG:appRollCall[$arg]:"${appRollCall[$arg]}
37 if ! [ "$returnState" = "false" ]; then returnState="true"; fi
38 else
39 appRollCall[$arg]="false"; returnState="false";
40 fi
41 done
42
43 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
44 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
45
46 #===Determine function return code===
47 if [ "$returnState" = "true" ]; then
48 #echo "DEBUG:checkapp returns true for $arg";
49 return 0;
50 else
51 #echo "DEBUG:checkapp returns false for $arg";
52 return 1;
53 fi
54} # Check that app exists
55checkfile() {
56 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
57 # Usage: checkfile arg1 arg2 arg3 ...
58 # Input: global assoc. array 'fileRollCall'
59 # Output: adds/updates key(value) to global assoc array 'fileRollCall';
60 # Output: returns 0 if app found, 1 otherwise
61 local returnState
62
63 #===Process Args===
64 for arg in "$@"; do
65 #echo "DEBUG:processing arg:$arg"
66 if [ -f "$arg" ]; then
67 fileRollCall["$arg"]="true";
68 #echo "DEBUG:fileRollCall[\"$arg\"]:"${fileRollCall["$arg"]}
69 if ! [ "$returnState" = "false" ]; then returnState="true"; fi
70 else
71 fileRollCall["$arg"]="false"; returnState="false";
72 fi
73 done
74
75 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:fileRollCall key [$key] is:${fileRollCall[$key]}"; done
76 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
77
78 #===Determine function return code===
79 if [ "$returnState" = "true" ]; then
80 #echo "DEBUG:checkapp returns true for $arg";
81 return 0;
82 else
83 #echo "DEBUG:checkapp returns false for $arg";
84 return 1;
85 fi
86} # Check that file exists
87checkdir() {
88 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
89 # Usage: checkdir arg1 arg2 arg3 ...
90 # Input: global assoc. array 'dirRollCall'
91 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
92 # Output: returns 0 if app found, 1 otherwise
93 local returnState
94
95 #===Process Args===
96 for arg in "$@"; do
97 #echo "DEBUG:processing arg:$arg"
98 if [ -d "$arg" ]; then
99 dirRollCall["$arg"]="true";
100 #echo "DEBUG:dirRollCall[\"$arg\"]:"${dirRollCall["$arg"]}
101 if ! [ "$returnState" = "false" ]; then returnState="true"; fi
102 elif [ "$arg" = "" ]; then
103 dirRollCall["$arg"]="false"; returnState="false";
104 else
105 returnState="false";
106 fi
107 done
108
109 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:dirRollCall key [$key] is:${dirRollCall[$key]}"; done
110 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
111
112 #===Determine function return code===
113 if [ "$returnState" = "true" ]; then
114 #echo "DEBUG:checkapp returns true for $arg";
115 return 0;
116 else
117 #echo "DEBUG:checkapp returns false for $arg";
118 return 1;
119 fi
120} # Check that dir exists
121
122# Yell, Die, Try Three-Fingered Claw technique
123# Ref/Attrib: https://stackoverflow.com/a/25515370
124yell() { echo "$0: $*" >&2; }
125die() { yell "$*"; exit 111; }
126try() { "$@" || die "cannot $*"; }
127
128echoerr() {
129 echo "$@" 1>&2; # Define stderr echo function.
130} # Define stderr message function.
131showUsage() {
132 echoerr "USAGE:"
133 echoerr " bkgpslog [ options ]"
134 echoerr
135 echoerr "OPTIONS:"
136 echoerr " -h, --help"
137 echoerr " Display help information."
138 echoerr
139 echoerr " --version"
140 echoerr " Display script version."
141 echoerr
142 echoerr " -v, --verbose"
143 echoerr " Display debugging info."
144 echoerr
145 echoerr " -e, --encrypt"
146 echoerr " Encrypt output."
147 echoerr
148 echoerr " -r, --recipient [ pubkey string ]"
149 echoerr " Specify recipient."
150 echoerr
151 echoerr " -o, --output [ directory ]"
152 echoerr " Specify output directory to save logs."
153 echoerr
154 echoerr " -c, --compress"
155 echoerr " Compress output with gzip (before encryption if enabled)."
156 echoerr
157 echoerr "EXAMPLE: (bash script lines)"
158 echoerr "/bin/bash bkgpslog -e -c \\"
159 echoerr "-r age1mrmfnwhtlprn4jquex0ukmwcm7y2nxlphuzgsgv8ew2k9mewy3rs8u7su5 \\"
160 echoerr "-r age1ala848kqrvxc88rzaauc6vc5v0fqrvef9dxyk79m0vjea3hagclswu0lgq \\"
161 echoerr "-o ~/Sync/Location"
162} # Display information on how to use this script.
163showVersion() {
164 echoerr "$SCRIPT_VERSION"
165} # Display script version.
166vbm() {
167 # Usage: vbm "DEBUG:verbose message here"
168 # Description: Prints verbose message ("vbm") to stderr if OPTION_VERBOSE is set to "true".
169 # Input:
170 # - OPTION_VERBOSE variable set by processArguments function. (ex: "true", "false")
171 # - "$@" positional arguments fed to this function.
172 # Output: stderr
173 # Script function dependencies: echoerr
174 # External function dependencies: echo
175 # Last modified: 2020-04-11T23:57Z
176 # Last modified by: Steven Baltakatei Sandoval
177 # License: GPLv3+
178 # Ref./Attrib:
179
180 if [ "$OPTION_VERBOSE" = "true" ]; then
181 FUNCTION_TIME=$(date --iso-8601=ns); # Save current time in nano seconds.
182 echoerr "[$FUNCTION_TIME] ""$*"; # Display argument text.
183 fi
184
185 # End function
186 return 0; # Function finished.
187} # Verbose message display function.
188processArguments() {
189 while [ ! $# -eq 0 ]; do # While number of arguments ($#) is not (!) equal to (-eq) zero (0).
190 #echoerr "DEBUG:Starting processArguments while loop."
191 #echoerr "DEBUG:Provided arguments are:""$*"
192 case "$1" in
193 -h | --help) showUsage; exit 1;; # Display usage.
194 --version) showVersion; exit 1;; # Show version
195 -v | --verbose) OPTION_VERBOSE="true"; vbm "DEBUG:Verbose mode enabled.";; # Enable verbose mode.
196 -o | --output) if [ -d "$2" ]; then DIROUT="$2"; vbm "DEBUG:DIROUT:$DIROUT"; shift; fi ;; # Define output directory.
197 -e | --encrypt) OPTION_ENCRYPT="true"; vbm "DEBUG:Encrypted output mode enabled.";;
198 -r | --recipient) # Add 'age' recipient via public key string
199 recPubKeys+=("$2"); vbm "pubkey added:""$2"; shift;;
200 -c | --compress) OPTION_COMPRESS="true"; vbm "DEBUG:Compressed output mode enabled.";;
201 *) echoerr "ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options.
202 esac
203 shift
204 done
205} # Argument Processing
206timeUntilNextDay(){
207 # Desc: Report seconds until next day.
208 # Output: stdout: integer seconds until next day
209 # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0
210 # Usage: timeUntilNextDay
211 # Usage: if ! myTTL="$(timeUntilNextDay)"; then yell "ERROR in if statement"; exit 1; fi
212 local returnState TIME_CURRENT TIME_NEXT_DAY SECONDS_UNTIL_NEXT_DAY
213 TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
214 TIME_NEXT_DAY="$(date -d "$TIME_CURRENT next day" --iso-8601=date)"; # Produce timestamp of beginning of tomorrow with resolution of 1 second.
215 SECONDS_UNTIL_NEXT_DAY="$(( $(date +%s -d "$TIME_NEXT_DAY") - $(date +%s -d "$TIME_CURRENT") ))" ; # Calculate seconds until closest future midnight (res. 1 second).
216 if [[ "$SECONDS_UNTIL_NEXT_DAY" -gt 0 ]]; then
217 returnState="true";
218 elif [[ "$SECONDS_UNTIL_NEXT_DAY" -eq 0 ]]; then
219 returnState="WARNING_ZERO";
220 yell "WARNING:Reported time until next day exactly zero.";
221 elif [[ "$SECONDS_UNTIL_NEXT_DAY" -lt 0 ]]; then
222 returnState="WARNING_NEGATIVE";
223 yell "WARNING:Reported time until next day is negative.";
224 fi
225
226 try echo "$SECONDS_UNTIL_NEXT_DAY"; # Report
227
228 #===Determine function return code===
229 if [[ "$returnState" = "true" ]]; then
230 return 0;
231 elif [[ "$returnState" = "WARNING_ZERO" ]]; then
232 return 1;
233 elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then
234 return 2;
235 fi
236} # Report seconds until next day
237timeUntilNextHour(){
238 # Desc: Report seconds until next hour
239 # Output: stdout: integer seconds until next hour
240 # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0
241 # Usage: timeUntilNextHour
242 # Usage: if ! myTTL="$(timeUntilNextHour)"; then yell "ERROR in if statement"; exit 1; fi
243 local returnState TIME_CURRENT TIME_NEXT_HOUR SECONDS_UNTIL_NEXT_HOUR
244 TIME_CURRENT="$(date --iso-8601=seconds)"; # Produce `date`-parsable current timestamp with resolution of 1 second.
245 TIME_NEXT_HOUR="$(date -d "$TIME_CURRENT next hour" --iso-8601=hours)"; # Produce `date`-parsable current time stamp with resolution of 1 second.
246 SECONDS_UNTIL_NEXT_HOUR="$(( $(date +%s -d "$TIME_NEXT_HOUR") - $(date +%s -d "$TIME_CURRENT") ))"; # Calculate seconds until next hour (res. 1 second).
247 if [[ "$SECONDS_UNTIL_NEXT_HOUR" -gt 0 ]]; then
248 returnState="true";
249 elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -eq 0 ]]; then
250 returnState="WARNING_ZERO";
251 yell "WARNING:Reported time until next hour exactly zero.";
252 elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -lt 0 ]]; then
253 returnState="WARNING_NEGATIVE";
254 yell "WARNING:Reported time until next hour is negative.";
255 fi
256
257 try echo "$SECONDS_UNTIL_NEXT_HOUR"; # Report
258
259 #===Determine function return code===
260 if [[ "$returnState" = "true" ]]; then
261 return 0;
262 elif [[ "$returnState" = "WARNING_ZERO" ]]; then
263 return 1;
264 elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then
265 return 2;
266 fi
267} # Report seconds until next hour
268dateTimeShort(){
269 # Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz)
270 # Usage: dateTimeShort
271 # Output: stdout: timestamp (ISO-8601, no separators)
272 local TIME_CURRENT TIME_CURRENT_SHORT
273 TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
274 TIME_CURRENT_SHORT="$(date -d "$TIME_CURRENT" +%Y%m%dT%H%M%S%z)"; # Produce separator-less current timestamp with resolution 1 second.
275 echo "$TIME_CURRENT_SHORT";
276} # Get date&time without separators
277main() {
278 processArguments "$@" # Process arguments.
279 if [[ "$OPTION_ENCRYPT" = "true" ]]; then # Check if encryption option active.
280 if checkapp age; then # Check that age is available.
281 for pubkey in "${recPubKeys[@]}"; do # Validate recipient pubkey strings by forming test message
282 vbm "DEBUG:Testing pubkey string:$pubkey"
283 if echo "butts" | age -a -r "$pubkey" 1>/dev/null; then
284 # Form age recipient string
285 recipients="$recipients""-r $pubkey ";
286 vbm "Added pubkey for forming age recipient string:""$pubkey";
287 vbm "DEBUG:recipients:""$recipients";
288 else
289 yell "ERROR:Exit code ""$?"". Invalid recipient pubkey string. Exiting."; exit 1;
290 fi
291 done
292 vbm "DEBUG:Finished processing recPubKeys array";
293 # Form age command string
294 CMD_ENCRYPT=" | age ""$recipients ";
295 CMD_ENCRYPT_SUFFIX=".age";
296 else
297 yell "ERROR:Encryption enabled but \"age\" not found. Exiting."; exit 1;
298 fi
299 else
300 CMD_ENCRYPT="";
301 CMD_ENCRYPT_SUFFIX="";
302 vbm "DEBUG:Encryption not enabled."
303 fi
304
305 if [[ "$OPTION_COMPRESS" = "true" ]]; then # Check if compression option active
306 if checkapp gzip; then # Check if gzip available
307 CMD_COMPRESS=" | gzip ";
308 CMD_COMPRESS_SUFFIX=".gz";
309 else
310 yell "ERROR:Compression enabled but \"gzip\" not found. Exiting."; exit 1;
311 fi
312 else
313 CMD_COMPRESS="";
314 CMD_COMPRESS_SUFFIX="";
315 vbm "DEBUG:Compression not enabled."
316 fi
317
318
319 if checkapp gpspipe && checkdir "$DIROUT"; then
320
321 # # Set script lifespan to end at start of next day
322 # if ! scriptTTL="$(timeUntilNextDay)"; then
323 # if [[ "$scriptTTL" -eq 0 ]]; then
324 # ((scriptTTL++)); # Add 1 because 0 would cause 'timeout' to never timeout.
325 # else
326 # yell "ERROR: timeUntilNextDay exit code $?"; exit 1;
327 # fi;
328 # fi;
329
330 # Set script lifespan to end at start of next hour
331 if ! scriptTTL="$(timeUntilNextHour)"; then
332 if [[ "$scriptTTL" -eq 0 ]]; then
333 ((scriptTTL++)); # Add 1 because 0 would cause 'timeout' to never timeout.
334 else
335 yell "ERROR: timeUntilNextHour exit code $?"; exit 1;
336 fi;
337 fi;
338
339 # Determine buffer lifespan
340 bufferTTL="60";
341
342 # Record gps data until script lifespan ends
343 declare debugCounter; debugCounter="0"
344 while [[ "$SECONDS" -lt "$scriptTTL" ]]; do
345 ((debugCounter++))
346 # Determine output file paths (time is start of buffer period)
347 FILEOUT_BASENAME="$(dateTimeShort)""--P""$bufferTTL""S..""$SCRIPT_HOSTNAME""_location" ; # ISO-8601 YYYYmmddTHHMMSS+zzP[$bufferTTL]S
348 FILEOUT_NMEA="$FILEOUT_BASENAME".nmea"$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX" ;
349 FILEOUT_GPX="$FILEOUT_BASENAME".gpx"$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX" ;
350 FILEOUT_KML="$FILEOUT_BASENAME".kml"$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX" ;
351 # Fill buffer
352 buffer="$(timeout "$bufferTTL""s" gpspipe -r)"; # Record gpspipe nmea data to buffer for bufferTTL seconds
353 # Process and save buffers
354 echo "$buffer" "$CMD_COMPRESS" "$CMD_ENCRYPT" > "$DIROUT"/"$FILEOUT_NMEA" & # Save NMEA format
355 echo "$buffer" | gpsbabel -i nmea -f - -o gpx -F - "$CMD_COMPRESS" "$CMD_ENCRYPT" > "$DIROUT"/"$FILEOUT_GPX" & # Save GPX format
356 echo "$buffer" | gpsbabel -i nmea -f - -o kml -F - "$CMD_COMPRESS" "$CMD_ENCRYPT" > "$DIROUT"/"$FILEOUT_KML" & # Save KML format
357 vbm "DEBUG:Completed buffer session $debugCounter ." 1>&2;
358 # Reset buffer and filenames
359 unset buffer FILEOUT_BASENAME FILEOUT_NMEA FILEOUT_GPX FILEOUT_KML;
360 done
361 fi
362} # Main function.
363#===END Declare local script functions===
364#==END Define script parameters==
365
366
367#==BEGIN Perform work and exit==
368main "$@" # Run main function.
369exit 0;
370#==END Perform work and exit==
371
372
373
374 #gpspipe -r -d -l -o "$DIROUT1"/"$FILEOUT1" ;