Commit | Line | Data |
---|---|---|
032f4b05 SBS |
1 | #!/bin/bash |
2 | ||
8fbca23d SBS |
3 | # Desc: Records gps data until midnight |
4 | # Author: Steven Baltakatei Sandoval; License: GPLv3+ | |
aa2a49f1 | 5 | # Usage: bkgpslog -o [output dir] |
032f4b05 | 6 | |
8fbca23d | 7 | #==BEGIN Define script parameters== |
6c30388f SBS |
8 | ## Logging Behavior parameters |
9 | BUFFER_TTL="60"; # time between file writes | |
10 | SCRIPT_TTL="day"; # (day|hour) | |
aa2a49f1 SBS |
11 | #### TZ="UTC"; export TZ; # Default time zone; overridden by '--time-zone=[str]' option |
12 | DIR_TMP_DEFAULT="/dev/shm"; # Default parent of working directory | |
6c30388f | 13 | |
aa2a49f1 SBS |
14 | SCRIPT_TIME_START=$(date +%Y%m%dT%H%M%S.%N); |
15 | PATH="$HOME/.local/bin:$PATH"; # Add "$(systemd-path user-binaries)" path in case apps saved there | |
16 | SCRIPT_HOSTNAME=$(hostname); # Save hostname of system running this script. | |
17 | SCRIPT_VERSION="bkgpslog 0.1.0"; # Define version of script. | |
8fbca23d SBS |
18 | |
19 | declare -Ag appRollCall # Associative array for storing app status | |
20 | declare -Ag fileRollCall # Associative array for storing file status | |
21 | declare -Ag dirRollCall # Associative array for storing dir status | |
17d49005 | 22 | declare -a recPubKeys # for processArguments function |
77361307 | 23 | declare recipients # for main function |
8fbca23d | 24 | |
cfc25c90 SBS |
25 | ## Initialize variables |
26 | OPTION_VERBOSE=""; OPTION_ENCRYPT=""; OPTION_COMPRESS=""; OPTION_TMPDIR=""; | |
27 | ||
8fbca23d SBS |
28 | #===BEGIN Declare local script functions=== |
29 | checkapp() { | |
30 | # Desc: If arg is a command, save result in assoc array 'appRollCall' | |
31 | # Usage: checkapp arg1 arg2 arg3 ... | |
32 | # Input: global assoc. array 'appRollCall' | |
33 | # Output: adds/updates key(value) to global assoc array 'appRollCall' | |
34 | local returnState | |
35 | #echo "DEBUG:$(date +%S.%N)..Starting checkapp function." | |
36 | #echo "DEBUG:args: $@" | |
37 | #echo "DEBUG:returnState:$returnState" | |
38 | ||
39 | #===Process Args=== | |
40 | for arg in "$@"; do | |
41 | #echo "DEBUG:processing arg:$arg" | |
0b3dde05 | 42 | if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command |
8fbca23d SBS |
43 | appRollCall[$arg]="true"; |
44 | #echo "DEBUG:appRollCall[$arg]:"${appRollCall[$arg]} | |
45 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi | |
46 | else | |
47 | appRollCall[$arg]="false"; returnState="false"; | |
48 | fi | |
49 | done | |
50 | ||
51 | #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done | |
52 | #echo "DEBUG:evaluating returnstate. returnState:"$returnState | |
53 | ||
54 | #===Determine function return code=== | |
55 | if [ "$returnState" = "true" ]; then | |
56 | #echo "DEBUG:checkapp returns true for $arg"; | |
57 | return 0; | |
58 | else | |
59 | #echo "DEBUG:checkapp returns false for $arg"; | |
60 | return 1; | |
61 | fi | |
62 | } # Check that app exists | |
63 | checkfile() { | |
64 | # Desc: If arg is a file path, save result in assoc array 'fileRollCall' | |
65 | # Usage: checkfile arg1 arg2 arg3 ... | |
66 | # Input: global assoc. array 'fileRollCall' | |
67 | # Output: adds/updates key(value) to global assoc array 'fileRollCall'; | |
68 | # Output: returns 0 if app found, 1 otherwise | |
69 | local returnState | |
70 | ||
71 | #===Process Args=== | |
72 | for arg in "$@"; do | |
73 | #echo "DEBUG:processing arg:$arg" | |
74 | if [ -f "$arg" ]; then | |
75 | fileRollCall["$arg"]="true"; | |
76 | #echo "DEBUG:fileRollCall[\"$arg\"]:"${fileRollCall["$arg"]} | |
77 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi | |
78 | else | |
79 | fileRollCall["$arg"]="false"; returnState="false"; | |
80 | fi | |
81 | done | |
82 | ||
83 | #for key in "${!fileRollCall[@]}"; do echo "DEBUG:fileRollCall key [$key] is:${fileRollCall[$key]}"; done | |
84 | #echo "DEBUG:evaluating returnstate. returnState:"$returnState | |
85 | ||
86 | #===Determine function return code=== | |
87 | if [ "$returnState" = "true" ]; then | |
88 | #echo "DEBUG:checkapp returns true for $arg"; | |
89 | return 0; | |
90 | else | |
91 | #echo "DEBUG:checkapp returns false for $arg"; | |
92 | return 1; | |
93 | fi | |
94 | } # Check that file exists | |
95 | checkdir() { | |
96 | # Desc: If arg is a dir path, save result in assoc array 'dirRollCall' | |
97 | # Usage: checkdir arg1 arg2 arg3 ... | |
98 | # Input: global assoc. array 'dirRollCall' | |
99 | # Output: adds/updates key(value) to global assoc array 'dirRollCall'; | |
100 | # Output: returns 0 if app found, 1 otherwise | |
101 | local returnState | |
102 | ||
103 | #===Process Args=== | |
104 | for arg in "$@"; do | |
105 | #echo "DEBUG:processing arg:$arg" | |
106 | if [ -d "$arg" ]; then | |
107 | dirRollCall["$arg"]="true"; | |
108 | #echo "DEBUG:dirRollCall[\"$arg\"]:"${dirRollCall["$arg"]} | |
109 | if ! [ "$returnState" = "false" ]; then returnState="true"; fi | |
bcf09dcc | 110 | elif [ "$arg" = "" ]; then |
8fbca23d | 111 | dirRollCall["$arg"]="false"; returnState="false"; |
bcf09dcc SBS |
112 | else |
113 | returnState="false"; | |
8fbca23d SBS |
114 | fi |
115 | done | |
116 | ||
117 | #for key in "${!dirRollCall[@]}"; do echo "DEBUG:dirRollCall key [$key] is:${dirRollCall[$key]}"; done | |
118 | #echo "DEBUG:evaluating returnstate. returnState:"$returnState | |
119 | ||
120 | #===Determine function return code=== | |
121 | if [ "$returnState" = "true" ]; then | |
122 | #echo "DEBUG:checkapp returns true for $arg"; | |
123 | return 0; | |
124 | else | |
125 | #echo "DEBUG:checkapp returns false for $arg"; | |
126 | return 1; | |
127 | fi | |
128 | } # Check that dir exists | |
032f4b05 | 129 | |
c609b9c8 SBS |
130 | # Yell, Die, Try Three-Fingered Claw technique |
131 | # Ref/Attrib: https://stackoverflow.com/a/25515370 | |
132 | yell() { echo "$0: $*" >&2; } | |
133 | die() { yell "$*"; exit 111; } | |
134 | try() { "$@" || die "cannot $*"; } | |
135 | ||
032f4b05 SBS |
136 | echoerr() { |
137 | echo "$@" 1>&2; # Define stderr echo function. | |
138 | } # Define stderr message function. | |
139 | showUsage() { | |
140 | echoerr "USAGE:" | |
94e094d1 | 141 | echoerr " bkgpslog [ options ]" |
032f4b05 SBS |
142 | echoerr |
143 | echoerr "OPTIONS:" | |
144 | echoerr " -h, --help" | |
145 | echoerr " Display help information." | |
146 | echoerr | |
147 | echoerr " --version" | |
148 | echoerr " Display script version." | |
149 | echoerr | |
150 | echoerr " -v, --verbose" | |
151 | echoerr " Display debugging info." | |
152 | echoerr | |
17d49005 SBS |
153 | echoerr " -e, --encrypt" |
154 | echoerr " Encrypt output." | |
155 | echoerr | |
156 | echoerr " -r, --recipient [ pubkey string ]" | |
f7f33d33 SBS |
157 | echoerr " Specify recipient. May be age or ssh pubkey." |
158 | echoerr " See https://github.com/FiloSottile/age" | |
17d49005 | 159 | echoerr |
032f4b05 SBS |
160 | echoerr " -o, --output [ directory ]" |
161 | echoerr " Specify output directory to save logs." | |
17d49005 | 162 | echoerr |
408a342b SBS |
163 | echoerr " -c, --compress" |
164 | echoerr " Compress output with gzip (before encryption if enabled)." | |
165 | echoerr | |
f7f33d33 SBS |
166 | echoerr " -z, --time-zone" |
167 | echoerr " Specify time zone. (ex: \"America/New_York\")" | |
168 | echoerr | |
169 | echoerr " -t, --temp-dir" | |
170 | echoerr " Specify parent directory for temporary working directory." | |
171 | echoerr " Default: \"/dev/shm\"" | |
172 | echoerr | |
17d49005 | 173 | echoerr "EXAMPLE: (bash script lines)" |
408a342b | 174 | echoerr "/bin/bash bkgpslog -e -c \\" |
17d49005 SBS |
175 | echoerr "-r age1mrmfnwhtlprn4jquex0ukmwcm7y2nxlphuzgsgv8ew2k9mewy3rs8u7su5 \\" |
176 | echoerr "-r age1ala848kqrvxc88rzaauc6vc5v0fqrvef9dxyk79m0vjea3hagclswu0lgq \\" | |
177 | echoerr "-o ~/Sync/Location" | |
032f4b05 SBS |
178 | } # Display information on how to use this script. |
179 | showVersion() { | |
180 | echoerr "$SCRIPT_VERSION" | |
181 | } # Display script version. | |
182 | vbm() { | |
0b3dde05 SBS |
183 | # Usage: vbm "DEBUG:verbose message here" |
184 | # Description: Prints verbose message ("vbm") to stderr if OPTION_VERBOSE is set to "true". | |
185 | # Input: | |
186 | # - OPTION_VERBOSE variable set by processArguments function. (ex: "true", "false") | |
187 | # - "$@" positional arguments fed to this function. | |
188 | # Output: stderr | |
189 | # Script function dependencies: echoerr | |
190 | # External function dependencies: echo | |
191 | # Last modified: 2020-04-11T23:57Z | |
192 | # Last modified by: Steven Baltakatei Sandoval | |
193 | # License: GPLv3+ | |
194 | # Ref./Attrib: | |
195 | ||
196 | if [ "$OPTION_VERBOSE" = "true" ]; then | |
197 | FUNCTION_TIME=$(date --iso-8601=ns); # Save current time in nano seconds. | |
198 | echoerr "[$FUNCTION_TIME] ""$*"; # Display argument text. | |
032f4b05 | 199 | fi |
0b3dde05 SBS |
200 | |
201 | # End function | |
202 | return 0; # Function finished. | |
032f4b05 SBS |
203 | } # Verbose message display function. |
204 | processArguments() { | |
205 | while [ ! $# -eq 0 ]; do # While number of arguments ($#) is not (!) equal to (-eq) zero (0). | |
d6896d3b SBS |
206 | #echoerr "DEBUG:Starting processArguments while loop." |
207 | #echoerr "DEBUG:Provided arguments are:""$*" | |
032f4b05 | 208 | case "$1" in |
c1bbf9f7 | 209 | -h | --help) showUsage; exit 1;; # Display usage. |
032f4b05 | 210 | --version) showVersion; exit 1;; # Show version |
c1bbf9f7 | 211 | -v | --verbose) OPTION_VERBOSE="true"; vbm "DEBUG:Verbose mode enabled.";; # Enable verbose mode. |
6c30388f | 212 | -o | --output) if [ -d "$2" ]; then DIR_OUT="$2"; vbm "DEBUG:DIR_OUT:$DIR_OUT"; shift; fi ;; # Define output directory. |
17d49005 SBS |
213 | -e | --encrypt) OPTION_ENCRYPT="true"; vbm "DEBUG:Encrypted output mode enabled.";; |
214 | -r | --recipient) # Add 'age' recipient via public key string | |
405ce7df | 215 | recPubKeys+=("$2"); vbm "STATUS:pubkey added:""$2"; shift;; |
408a342b | 216 | -c | --compress) OPTION_COMPRESS="true"; vbm "DEBUG:Compressed output mode enabled.";; |
6a17e8e7 SBS |
217 | -z | --time-zone) try setTimeZoneEV "$2"; shift;; |
218 | -t | --temp-dir) OPTION_TMPDIR="true" && TMP_DIR_PRIORITY="$2"; shift;; | |
b0da06ca | 219 | *) echoerr "ERROR: Unrecognized argument: $1"; echoerr "STATUS:All arguments:$*"; exit 1;; # Handle unrecognized options. |
032f4b05 SBS |
220 | esac |
221 | shift | |
222 | done | |
223 | } # Argument Processing | |
6c30388f SBS |
224 | setTimeZoneEV(){ |
225 | # Desc: Set time zone environment variable TZ | |
226 | # Usage: setTimeZoneEV arg1 | |
227 | # Input: arg1: 'date'-compatible timezone string (ex: "America/New_York") | |
228 | # TZDIR env var (optional; default: "/usr/share/zoneinfo") | |
229 | # Output: exports TZ | |
230 | # exit code 0 on success | |
231 | # exit code 1 on incorrect number of arguments | |
232 | # exit code 2 if unable to validate arg1 | |
233 | # Depends: yell, printenv, bash 5 | |
234 | # Tested on: Debian 10 | |
235 | ARG1="$1" | |
236 | local tzDir returnState | |
237 | if ! [[ $# -eq 1 ]]; then | |
238 | yell "ERROR:Invalid argument count."; | |
239 | return 1; | |
240 | fi | |
241 | ||
242 | # Read TZDIR env var if available | |
243 | if printenv TZDIR 1>/dev/null 2>&1; then | |
244 | tzDir="$(printenv TZDIR)"; | |
245 | else | |
246 | tzDir="/usr/share/zoneinfo"; | |
247 | fi | |
248 | ||
249 | # Validate TZ string | |
250 | if ! [[ -f "$tzDir"/"$ARG1" ]]; then | |
251 | yell "ERROR:Invalid time zone argument."; | |
252 | return 2; | |
253 | else | |
254 | # Export ARG1 as TZ environment variable | |
255 | TZ="$ARG1" && export TZ && returnState="true"; | |
256 | fi | |
257 | ||
258 | # Determine function return code | |
259 | if [ "$returnState" = "true" ]; then | |
260 | return 0; | |
261 | fi | |
262 | } # Exports TZ environment variable | |
c2aaff78 SBS |
263 | timeUntilNextDay(){ |
264 | # Desc: Report seconds until next day. | |
265 | # Output: stdout: integer seconds until next day | |
8fbca23d | 266 | # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0 |
c2aaff78 SBS |
267 | # Usage: timeUntilNextDay |
268 | # Usage: if ! myTTL="$(timeUntilNextDay)"; then yell "ERROR in if statement"; exit 1; fi | |
269 | local returnState TIME_CURRENT TIME_NEXT_DAY SECONDS_UNTIL_NEXT_DAY | |
8fbca23d | 270 | TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second. |
c2aaff78 SBS |
271 | TIME_NEXT_DAY="$(date -d "$TIME_CURRENT next day" --iso-8601=date)"; # Produce timestamp of beginning of tomorrow with resolution of 1 second. |
272 | SECONDS_UNTIL_NEXT_DAY="$(( $(date +%s -d "$TIME_NEXT_DAY") - $(date +%s -d "$TIME_CURRENT") ))" ; # Calculate seconds until closest future midnight (res. 1 second). | |
273 | if [[ "$SECONDS_UNTIL_NEXT_DAY" -gt 0 ]]; then | |
274 | returnState="true"; | |
275 | elif [[ "$SECONDS_UNTIL_NEXT_DAY" -eq 0 ]]; then | |
276 | returnState="WARNING_ZERO"; | |
277 | yell "WARNING:Reported time until next day exactly zero."; | |
278 | elif [[ "$SECONDS_UNTIL_NEXT_DAY" -lt 0 ]]; then | |
279 | returnState="WARNING_NEGATIVE"; | |
280 | yell "WARNING:Reported time until next day is negative."; | |
281 | fi | |
282 | ||
283 | try echo "$SECONDS_UNTIL_NEXT_DAY"; # Report | |
284 | ||
285 | #===Determine function return code=== | |
286 | if [[ "$returnState" = "true" ]]; then | |
287 | return 0; | |
288 | elif [[ "$returnState" = "WARNING_ZERO" ]]; then | |
289 | return 1; | |
290 | elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then | |
291 | return 2; | |
292 | fi | |
293 | } # Report seconds until next day | |
294 | timeUntilNextHour(){ | |
295 | # Desc: Report seconds until next hour | |
296 | # Output: stdout: integer seconds until next hour | |
297 | # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0 | |
298 | # Usage: timeUntilNextHour | |
299 | # Usage: if ! myTTL="$(timeUntilNextHour)"; then yell "ERROR in if statement"; exit 1; fi | |
300 | local returnState TIME_CURRENT TIME_NEXT_HOUR SECONDS_UNTIL_NEXT_HOUR | |
301 | TIME_CURRENT="$(date --iso-8601=seconds)"; # Produce `date`-parsable current timestamp with resolution of 1 second. | |
302 | TIME_NEXT_HOUR="$(date -d "$TIME_CURRENT next hour" --iso-8601=hours)"; # Produce `date`-parsable current time stamp with resolution of 1 second. | |
303 | SECONDS_UNTIL_NEXT_HOUR="$(( $(date +%s -d "$TIME_NEXT_HOUR") - $(date +%s -d "$TIME_CURRENT") ))"; # Calculate seconds until next hour (res. 1 second). | |
304 | if [[ "$SECONDS_UNTIL_NEXT_HOUR" -gt 0 ]]; then | |
8fbca23d | 305 | returnState="true"; |
c2aaff78 | 306 | elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -eq 0 ]]; then |
8fbca23d | 307 | returnState="WARNING_ZERO"; |
c2aaff78 SBS |
308 | yell "WARNING:Reported time until next hour exactly zero."; |
309 | elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -lt 0 ]]; then | |
8fbca23d | 310 | returnState="WARNING_NEGATIVE"; |
c2aaff78 | 311 | yell "WARNING:Reported time until next hour is negative."; |
8fbca23d | 312 | fi |
032f4b05 | 313 | |
c2aaff78 | 314 | try echo "$SECONDS_UNTIL_NEXT_HOUR"; # Report |
8fbca23d SBS |
315 | |
316 | #===Determine function return code=== | |
317 | if [[ "$returnState" = "true" ]]; then | |
318 | return 0; | |
319 | elif [[ "$returnState" = "WARNING_ZERO" ]]; then | |
320 | return 1; | |
321 | elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then | |
322 | return 2; | |
323 | fi | |
c2aaff78 | 324 | } # Report seconds until next hour |
8fbca23d SBS |
325 | dateTimeShort(){ |
326 | # Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz) | |
327 | # Usage: dateTimeShort | |
328 | # Output: stdout: timestamp (ISO-8601, no separators) | |
c2aaff78 | 329 | local TIME_CURRENT TIME_CURRENT_SHORT |
8fbca23d SBS |
330 | TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second. |
331 | TIME_CURRENT_SHORT="$(date -d "$TIME_CURRENT" +%Y%m%dT%H%M%S%z)"; # Produce separator-less current timestamp with resolution 1 second. | |
332 | echo "$TIME_CURRENT_SHORT"; | |
6c30388f SBS |
333 | } # Get YYYYmmddTHHMMSS±zzzz |
334 | dateShort(){ | |
335 | # Desc: Date without separators (YYYYmmdd) | |
336 | # Usage: dateShort | |
337 | # Output: stdout: date (ISO-8601, no separators) | |
338 | local TIME_CURRENT DATE_CURRENT_SHORT | |
339 | TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second. | |
340 | DATE_CURRENT_SHORT="$(date -d "$TIME_CURRENT" +%Y%m%d)"; # Produce separator-less current date with resolution 1 day. | |
341 | echo "$DATE_CURRENT_SHORT"; | |
342 | } # Get YYYYmmdd | |
343 | timeDuration(){ | |
344 | # Desc: Output approximate time duration string before given time (default:current date) | |
345 | # Ref/Attrib: ISO-8601:2004(E), §4.4.4.2 Representations of time intervals by duration and context information | |
346 | # Note: "1 month" ("P1M") is assumed to be "30 days" (see ISO-8601:2004(E), §2.2.1.2) | |
347 | # Usage: timeDuration [arg1] ([arg2]) | |
348 | # Input: arg1: seconds as base 10 integer >= 0 (ex: 3601) | |
349 | # arg2: precision level (optional; default=2) | |
350 | # Output: stdout: ISO-8601 duration string (ex: "P1H1S", "P2Y10M15DT10H30M20S") | |
351 | # Example: 'timeDuration 111111 3' yields 'P1DT6H51M' | |
352 | # Depends: date 8 (gnucoreutils) | |
353 | local returnState fullHours fullMinutes fullSeconds; | |
354 | ARG1="$1"; | |
355 | ARG2="$2"; | |
356 | precision=2; # set default precision | |
357 | returnState="true"; # set default return state | |
358 | ||
359 | # Check that between one and two arguments is supplied | |
360 | if ! { [[ $# -ge 1 ]] && [[ $# -le 2 ]]; }; then | |
361 | yell "ERROR:Invalid number of arguments:$# . Exiting."; | |
362 | returnState="ERROR_INPUT"; fi | |
363 | ||
364 | # Check that arg1 provided | |
365 | if [[ $# -ge 1 ]]; then | |
366 | # Check that arg1 is a positive integer | |
367 | if [[ "$ARG1" =~ ^[[:digit:]]+$ ]]; then | |
368 | arg1Valid="true"; | |
369 | else | |
370 | yell "ERROR:ARG1 not a digit."; | |
371 | returnState="ERROR_INPUT"; | |
372 | arg1Valid="false"; | |
373 | fi | |
374 | else | |
375 | yell "ERROR:No argument provided. Exiting."; | |
376 | exit 1; | |
377 | fi | |
378 | ||
379 | # Consider whether arg2 was provided | |
380 | if [[ $# -eq 2 ]]; then | |
381 | # Check that the second arg is a positive integer | |
382 | if [[ "$ARG2" =~ ^[[:digit:]]+$ ]] && [[ "ARG2" -gt 0 ]]; then | |
383 | arg2Valid="true"; | |
384 | precision="$ARG2"; | |
385 | else | |
386 | yell "ERROR:ARG2 not a positive integer. (is $ARG2 ). Leaving early."; | |
387 | returnState="ERROR_INPUT"; | |
388 | arg2Valid="false"; | |
389 | fi; | |
390 | else | |
391 | arg2Valid="false"; | |
392 | fi; | |
393 | ||
394 | remainder="$ARG1" ; # seconds | |
395 | ## Calculate full years Y, update remainder | |
396 | fullYears=$(( remainder / (365*24*60*60) )); | |
397 | remainder=$(( remainder - (fullYears*365*24*60*60) )); | |
398 | ## Calculate full months M, update remainder | |
399 | fullMonths=$(( remainder / (30*24*60*60) )); | |
400 | remainder=$(( remainder - (fullMonths*30*24*60*60) )); | |
401 | ## Calculate full days D, update remainder | |
402 | fullDays=$(( remainder / (24*60*60) )); | |
403 | remainder=$(( remainder - (fullDays*24*60*60) )); | |
404 | ## Calculate full hours H, update remainder | |
405 | fullHours=$(( remainder / (60*60) )); | |
406 | remainder=$(( remainder - (fullHours*60*60) )); | |
407 | ## Calculate full minutes M, update remainder | |
408 | fullMinutes=$(( remainder / (60) )); | |
409 | remainder=$(( remainder - (fullMinutes*60) )); | |
410 | ## Calculate full seconds S, update remainder | |
411 | fullSeconds=$(( remainder / (1) )); | |
412 | remainder=$(( remainder - (remainder*1) )); | |
413 | ## Check which fields filled | |
414 | if [[ $fullYears -gt 0 ]]; then hasYears="true"; else hasYears="false"; fi | |
415 | if [[ $fullMonths -gt 0 ]]; then hasMonths="true"; else hasMonths="false"; fi | |
416 | if [[ $fullDays -gt 0 ]]; then hasDays="true"; else hasDays="false"; fi | |
417 | if [[ $fullHours -gt 0 ]]; then hasHours="true"; else hasHours="false"; fi | |
418 | if [[ $fullMinutes -gt 0 ]]; then hasMinutes="true"; else hasMinutes="false"; fi | |
419 | if [[ $fullSeconds -gt 0 ]]; then hasSeconds="true"; else hasSeconds="false"; fi | |
420 | ||
421 | ## Determine which fields to display (see ISO-8601:2004 §4.4.3.2) | |
422 | witherPrecision="false" | |
423 | ||
424 | ### Years | |
425 | if $hasYears && [[ $precision -gt 0 ]]; then | |
426 | displayYears="true"; | |
427 | witherPrecision="true"; | |
428 | else | |
429 | displayYears="false"; | |
430 | fi; | |
431 | if $witherPrecision; then ((precision--)); fi; | |
432 | ||
433 | ### Months | |
434 | if $hasMonths && [[ $precision -gt 0 ]]; then | |
435 | displayMonths="true"; | |
436 | witherPrecision="true"; | |
437 | else | |
438 | displayMonths="false"; | |
439 | fi; | |
440 | if $witherPrecision && [[ $precision -gt 0 ]]; then | |
441 | displayMonths="true"; | |
442 | fi; | |
443 | if $witherPrecision; then ((precision--)); fi; | |
444 | ||
445 | ### Days | |
446 | if $hasDays && [[ $precision -gt 0 ]]; then | |
447 | displayDays="true"; | |
448 | witherPrecision="true"; | |
449 | else | |
450 | displayDays="false"; | |
451 | fi; | |
452 | if $witherPrecision && [[ $precision -gt 0 ]]; then | |
453 | displayDays="true"; | |
454 | fi; | |
455 | if $witherPrecision; then ((precision--)); fi; | |
456 | ||
457 | ### Hours | |
458 | if $hasHours && [[ $precision -gt 0 ]]; then | |
459 | displayHours="true"; | |
460 | witherPrecision="true"; | |
461 | else | |
462 | displayHours="false"; | |
463 | fi; | |
464 | if $witherPrecision && [[ $precision -gt 0 ]]; then | |
465 | displayHours="true"; | |
466 | fi; | |
467 | if $witherPrecision; then ((precision--)); fi; | |
468 | ||
469 | ### Minutes | |
470 | if $hasMinutes && [[ $precision -gt 0 ]]; then | |
471 | displayMinutes="true"; | |
472 | witherPrecision="true"; | |
473 | else | |
474 | displayMinutes="false"; | |
475 | fi; | |
476 | if $witherPrecision && [[ $precision -gt 0 ]]; then | |
477 | displayMinutes="true"; | |
478 | fi; | |
479 | if $witherPrecision; then ((precision--)); fi; | |
480 | ||
481 | ### Seconds | |
482 | ||
483 | if $hasSeconds && [[ $precision -gt 0 ]]; then | |
484 | displaySeconds="true"; | |
485 | witherPrecision="true"; | |
486 | else | |
487 | displaySeconds="false"; | |
488 | fi; | |
489 | if $witherPrecision && [[ $precision -gt 0 ]]; then | |
490 | displaySeconds="true"; | |
491 | fi; | |
492 | if $witherPrecision; then ((precision--)); fi; | |
493 | ||
494 | ||
495 | ||
496 | ## Determine whether or not the "T" separator is needed to separate date and time elements | |
497 | if ( $displayHours || $displayMinutes || $displaySeconds); then | |
498 | displayDateTime="true"; else displayDateTime="false"; fi | |
499 | ||
500 | ## Construct duration output string | |
501 | OUTPUT="P" | |
502 | if $displayYears; then | |
503 | OUTPUT=$OUTPUT$fullYears"Y"; fi | |
504 | if $displayMonths; then | |
505 | OUTPUT=$OUTPUT$fullMonths"M"; fi | |
506 | if $displayDays; then | |
507 | OUTPUT=$OUTPUT$fullDays"D"; fi | |
508 | if $displayDateTime; then | |
509 | OUTPUT=$OUTPUT"T"; fi | |
510 | if $displayHours; then | |
511 | OUTPUT=$OUTPUT$fullHours"H"; fi | |
512 | if $displayMinutes; then | |
513 | OUTPUT=$OUTPUT$fullMinutes"M"; fi | |
514 | if $displaySeconds; then | |
515 | OUTPUT=$OUTPUT$fullSeconds"S"; fi | |
516 | ||
517 | ## Output duration string to stdout | |
518 | if [[ "$returnState" = "true" ]]; then echo "$OUTPUT"; fi | |
519 | ||
520 | #===Determine function return code=== | |
521 | if [ "$returnState" = "true" ]; then | |
522 | return 0; | |
523 | else | |
524 | echo "$returnState" 1>&2; | |
525 | return 1; | |
526 | fi | |
527 | ||
528 | } # Get duration (ex: PT10M4S ) | |
529 | displayMissing() { | |
530 | # Desc: Displays missing apps, files, and dirs | |
531 | # Usage: displayMissing | |
532 | # Input: associative arrays: appRollCall, fileRollCall, dirRollCall | |
533 | # Output: stderr messages | |
534 | #==BEGIN Display errors== | |
535 | #===BEGIN Display Missing Apps=== | |
536 | missingApps="Missing apps :" | |
537 | #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done | |
538 | for key in "${!appRollCall[@]}"; do | |
539 | value="${appRollCall[$key]}" | |
540 | if [ "$value" = "false" ]; then | |
541 | #echo "DEBUG:Missing apps: $key => $value"; | |
542 | missingApps="$missingApps""$key " | |
543 | appMissing="true" | |
544 | fi | |
545 | done | |
546 | if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing. | |
547 | echo "$missingApps" 1>&2; | |
548 | fi | |
549 | #===END Display Missing Apps=== | |
550 | ||
551 | #===BEGIN Display Missing Files=== | |
552 | missingFiles="Missing files:" | |
553 | #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done | |
554 | for key in "${!fileRollCall[@]}"; do | |
555 | value="${fileRollCall[$key]}" | |
556 | if [ "$value" = "false" ]; then | |
557 | #echo "DEBUG:Missing files: $key => $value"; | |
558 | missingFiles="$missingFiles""$key " | |
559 | fileMissing="true" | |
560 | fi | |
561 | done | |
562 | if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing. | |
563 | echo "$missingFiles" 1>&2; | |
564 | fi | |
565 | #===END Display Missing Files=== | |
566 | ||
567 | #===BEGIN Display Missing Directories=== | |
568 | missingDirs="Missing dirs:" | |
569 | #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done | |
570 | for key in "${!dirRollCall[@]}"; do | |
571 | value="${dirRollCall[$key]}" | |
572 | if [ "$value" = "false" ]; then | |
573 | #echo "DEBUG:Missing dirs: $key => $value"; | |
574 | missingDirs="$missingDirs""$key " | |
575 | dirMissing="true" | |
576 | fi | |
577 | done | |
578 | if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing. | |
579 | echo "$missingDirs" 1>&2; | |
580 | fi | |
581 | #===END Display Missing Directories=== | |
582 | ||
583 | #==END Display errors== | |
584 | } # Display missing apps, files, dirs | |
585 | setScriptTTL() { | |
586 | #Desc: Sets script TTL | |
587 | #Usage: setScriptTTL arg1 | |
588 | #Input: arg1: "day" or "hour" | |
589 | #Output: scriptTTL | |
590 | #Depends: timeUntilNextHour or timeUntilNextDay | |
591 | local ARG1 | |
592 | ARG1="$1" | |
593 | if [[ "$ARG1" = "day" ]]; then | |
594 | # Set script lifespan to end at start of next day | |
595 | if ! scriptTTL="$(timeUntilNextDay)"; then | |
596 | if [[ "$scriptTTL" -eq 0 ]]; then | |
597 | ((scriptTTL++)); # Add 1 because 0 would cause 'timeout' to never timeout. | |
598 | else | |
599 | yell "ERROR: timeUntilNextDay exit code $?"; exit 1; | |
600 | fi; | |
601 | fi; | |
602 | elif [[ "$ARG1" = "hour" ]]; then | |
603 | # Set script lifespan to end at start of next hour | |
604 | if ! scriptTTL="$(timeUntilNextHour)"; then | |
605 | if [[ "$scriptTTL" -eq 0 ]]; then | |
606 | ((scriptTTL++)); # Add 1 because 0 would cause 'timeout' to never timeout. | |
607 | else | |
608 | yell "ERROR: timeUntilNextHour exit code $?"; exit 1; | |
609 | fi; | |
610 | fi; | |
611 | else | |
612 | yell "ERROR:Invalid argument for setScriptTTL function."; exit 1; | |
613 | fi | |
614 | } # Seconds until next (day|hour). | |
66af6225 SBS |
615 | appendArgTar(){ |
616 | # Desc: Writes first argument to temporary file with arguments as options, then appends file to tar | |
617 | # Usage: writeArg "$(echo "Data to be written.")" [name of file to be inserted] [tar path] [temp dir] ([cmd1] [cmd2] [cmd3] [cmd4]...) | |
618 | # Input: arg1: data to be written | |
619 | # arg2: file name of file to be inserted into tar | |
620 | # arg3: tar archive path (must exist first) | |
621 | # arg4: temporary working dir | |
622 | # arg5+: command strings (ex: "gpsbabel -i nmea -f - -o kml -F - ") | |
623 | # Output: file written to disk | |
624 | # Example: decrypt multiple large files in parallel | |
625 | # appendArgTar "$(cat /tmp/largefile1.gpg)" "largefile1" $HOME/archive.tar /tmp "gpg --decrypt" & | |
626 | # appendArgTar "$(cat /tmp/largefile2.gpg)" "largefile2" $HOME/archive.tar /tmp "gpg --decrypt" & | |
627 | # appendArgTar "$(cat /tmp/largefile3.gpg)" "largefile3" $HOME/archive.tar /tmp "gpg --decrypt" & | |
628 | ||
629 | # Depends: bash 5 | |
630 | ||
631 | # Set file name | |
632 | if ! [ -z "$2" ]; then FILENAME="$2"; else yell "ERROR:Not enough arguments."; exit 1; fi | |
633 | ||
634 | # Check tar path is a file | |
635 | if [ -f "$3" ]; then TAR_PATH="$3"; else yell "ERROR:Tar archive arg not a file."; exit 1; fi | |
636 | ||
637 | # Check temp dir arg | |
638 | if ! [ -z "$4" ]; then TMP_DIR="$3"; else yell "ERROR:No temporary working dir set."; exit 1; fi | |
639 | ||
640 | # Set command strings | |
641 | if ! [ -z "$5" ]; then CMD1="$5"; else CMD1="tee /dev/null "; fi # command string 1 | |
642 | if ! [ -z "$6" ]; then CMD2="$6"; else CMD2="tee /dev/null "; fi # command string 2 | |
643 | if ! [ -z "$7" ]; then CMD3="$7"; else CMD3="tee /dev/null "; fi # command string 3 | |
644 | if ! [ -z "$8" ]; then CMD4="$8"; else CMD4="tee /dev/null "; fi # command string 4 | |
645 | ||
646 | # Debug | |
647 | yell "CMD1:$CMD1" | |
648 | yell "CMD2:$CMD2" | |
649 | yell "CMD3:$CMD3" | |
650 | yell "CMD4:$CMD4" | |
651 | # Write to temporary working dir | |
652 | echo "$1" | $CMD1 | $CMD2 | $CMD3 | $CMD4 > "$TMP_DIR"/"$FILENAME"; | |
653 | ||
654 | # Append to tar | |
655 | try tar --append --directory="$TMP_DIR" --file="$TAR_PATH" "$FILENAME"; | |
656 | ||
657 | } # Append Bash var to file appended to Tar archive | |
658 | magicWriteBuffer() { | |
659 | # Desc: bkgpslog-specific meta function for writing data to DIR_TMP then appending each file to PATHOUT_TAR | |
660 | appendArgTar "$bufferBash" "$FILEOUT_NMEA" "$PATHOUT_TAR" "$DIR_TMP" "$CMD_CONV_NMEA" "$CMD_COMPRESS" "$CMD_ENCRYPT"; # Write NMEA data | |
661 | appendArgTar "$bufferBash" "$FILEOUT_GPX" "$PATHOUT_TAR" "$DIR_TMP" "$CMD_CONV_NMEA" "$CMD_COMPRESS" "$CMD_ENCRYPT"; # Write GPX file | |
662 | appendArgTar "$bufferBash" "$FILEOUT_KML" "$PATHOUT_TAR" "$DIR_TMP" "$CMD_CONV_NMEA" "$CMD_COMPRESS" "$CMD_ENCRYPT"; # Write KML file | |
663 | } # bkgpslog write function | |
664 | ||
665 | ||
032f4b05 | 666 | main() { |
0b3dde05 | 667 | processArguments "$@" # Process arguments. |
cfc25c90 | 668 | |
aa2a49f1 | 669 | # Determine working directory |
cfc25c90 SBS |
670 | ## Set DIR_TMP_PARENT to user-specified value if specified |
671 | if [[ "$OPTION_TMPDIR" = "true" ]]; then | |
672 | if [[ -d "$TMP_DIR_PRIORITY" ]]; then | |
673 | DIR_TMP_PARENT="$OPTION_TMPDIR"; | |
674 | else | |
66af6225 | 675 | yell "WARNING:Specified temporary working directory not valid:$OPTION_TMPDIR"; |
cfc25c90 SBS |
676 | exit 1; |
677 | fi | |
678 | fi | |
679 | ||
680 | ## Set DIR_TMP_PARENT to default or fallback otherwise | |
aa2a49f1 | 681 | if [[ -d "$DIR_TMP_DEFAULT" ]]; then |
cfc25c90 SBS |
682 | DIR_TMP_PARENT="$DIR_TMP_DEFAULT"; |
683 | elif [[ -d /tmp ]]; then | |
684 | yell "WARNING:/dev/shm not available. Falling back to /tmp ."; | |
685 | DIR_TMP_PARENT="/tmp"; | |
aa2a49f1 SBS |
686 | else |
687 | yell "ERROR:No valid working directory available. Exiting."; | |
688 | exit 1; | |
689 | fi | |
cfc25c90 SBS |
690 | |
691 | ## Set DIR_TMP using DIR_TMP_PARENT and nonce (SCRIPT_TIME_START) | |
0f4d2d90 | 692 | DIR_TMP="$DIR_TMP_PARENT"/"$SCRIPT_TIME_START""..bkgpslog" && vbm "DEBUG:Set DIR_TMP to:$DIR_TMP"; # Note: removed at end of main(). |
aa2a49f1 | 693 | |
6c30388f | 694 | # Set output encryption and compression option strings |
17d49005 SBS |
695 | if [[ "$OPTION_ENCRYPT" = "true" ]]; then # Check if encryption option active. |
696 | if checkapp age; then # Check that age is available. | |
697 | for pubkey in "${recPubKeys[@]}"; do # Validate recipient pubkey strings by forming test message | |
698 | vbm "DEBUG:Testing pubkey string:$pubkey" | |
408a342b | 699 | if echo "butts" | age -a -r "$pubkey" 1>/dev/null; then |
77361307 SBS |
700 | # Form age recipient string |
701 | recipients="$recipients""-r $pubkey "; | |
405ce7df | 702 | vbm "STATUS:Added pubkey for forming age recipient string:""$pubkey"; |
77361307 | 703 | vbm "DEBUG:recipients:""$recipients"; |
408a342b SBS |
704 | else |
705 | yell "ERROR:Exit code ""$?"". Invalid recipient pubkey string. Exiting."; exit 1; | |
77361307 | 706 | fi |
17d49005 | 707 | done |
77361307 | 708 | vbm "DEBUG:Finished processing recPubKeys array"; |
408a342b | 709 | # Form age command string |
c97313bf | 710 | CMD_ENCRYPT="age ""$recipients "; |
408a342b | 711 | CMD_ENCRYPT_SUFFIX=".age"; |
17d49005 SBS |
712 | else |
713 | yell "ERROR:Encryption enabled but \"age\" not found. Exiting."; exit 1; | |
714 | fi | |
408a342b | 715 | else |
c97313bf | 716 | CMD_ENCRYPT="tee /dev/null "; |
408a342b SBS |
717 | CMD_ENCRYPT_SUFFIX=""; |
718 | vbm "DEBUG:Encryption not enabled." | |
17d49005 | 719 | fi |
408a342b SBS |
720 | if [[ "$OPTION_COMPRESS" = "true" ]]; then # Check if compression option active |
721 | if checkapp gzip; then # Check if gzip available | |
c97313bf | 722 | CMD_COMPRESS="gzip "; |
408a342b SBS |
723 | CMD_COMPRESS_SUFFIX=".gz"; |
724 | else | |
725 | yell "ERROR:Compression enabled but \"gzip\" not found. Exiting."; exit 1; | |
286d9825 | 726 | fi |
408a342b | 727 | else |
c97313bf | 728 | CMD_COMPRESS="tee /dev/null "; |
408a342b SBS |
729 | CMD_COMPRESS_SUFFIX=""; |
730 | vbm "DEBUG:Compression not enabled." | |
731 | fi | |
032f4b05 | 732 | |
6c30388f SBS |
733 | # Check that critical apps and dirs are available, displag missing ones. |
734 | if ! checkapp gpspipe tar && ! checkdir "$DIR_OUT" "/dev/shm"; then | |
735 | yell "ERROR:Critical components missing."; | |
736 | displayMissing; yell "Exiting."; exit 1; fi | |
737 | ||
738 | # Set script lifespan | |
739 | setScriptTTL "$SCRIPT_TTL"; | |
740 | ||
741 | # File name substring: encoded bufferTTL | |
742 | bufferTTL_STR="$(timeDuration $BUFFER_TTL)"; | |
743 | ||
744 | # Init temp working dir | |
3a7b92f8 | 745 | try mkdir "$DIR_TMP" && vbm "DEBUG:Working dir creatd at:$DIR_TMP"; |
6c30388f SBS |
746 | |
747 | # Initialize 'tar' archive | |
748 | ## Define output tar path (note: each day gets *one* tar file (Ex: "20200731..hostname_location.[.gpx.gz].tar")) | |
9efeccb5 SBS |
749 | PATHOUT_TAR="$DIR_OUT"/"$(dateShort)".."$SCRIPT_HOSTNAME""_location""$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX".tar && \ |
750 | vbm "STATUS:Set PATHOUT_TAR to:$PATHOUT_TAR"; | |
6c30388f | 751 | ## Write bkgpslog version to DIR_TMP/VERSION |
93b52b83 SBS |
752 | FILEOUT_VERSION="$(dateTimeShort)..VERSION"; |
753 | PATHOUT_VERSION="$DIR_TMP"/"$FILEOUT_VERSION"; | |
754 | echo "$(dateTimeShort):$(basename "$0")"" Version:""$SCRIPT_VERSION" >> "$PATHOUT_VERSION" && vbm "DEBUG:VERSION created:$PATHOUT_VERSION"; | |
1f268128 SBS |
755 | ## Check if PATHOUT_TAR already exists. |
756 | if [[ -f "$PATHOUT_TAR" ]]; then | |
d760e83b | 757 | vbm "STATUS:Output tar already exists:$PATHOUT_TAR"; |
1f268128 | 758 | ### Check if preexisting tar is appendable. |
93b52b83 SBS |
759 | FILEOUT_APPENDTEST="$(dateTimeShort)..RESUMING_LOGGING_SESSION.txt"; |
760 | PATHOUT_APPENDTEST="$DIR_TMP"/"$FILEOUT_APPENDTEST" && \ | |
9efeccb5 SBS |
761 | vbm "DEBUG:Set PATHOUT_APPENDTEST to:$PATHOUT_APPENDTEST"; |
762 | echo "$(dateTimeShort):""$(basename "$0")"" version $SCRIPT_VERSION resuming logging session." >> "$PATHOUT_APPENDTEST" && \ | |
763 | vbm "DEBUG:""$PATHOUT_APPENDTEST"" created." | |
93b52b83 | 764 | if ! tar --append --directory="$DIR_TMP" --file="$PATHOUT_TAR" "$FILEOUT_APPENDTEST"; then |
1f268128 | 765 | ### If not appendable, label tar broken, move tar, proceed. |
9efeccb5 SBS |
766 | mv "$PATHOUT_TAR" "${PATHOUT_TAR%.*}""-broken$(dateTimeShort)".tar && \ |
767 | vbm "DEBUG:tar not writable, moving out of the way $PATHOUT_TAR"; | |
1f268128 SBS |
768 | fi |
769 | else | |
d760e83b | 770 | vbm "STATUS:Output tar does not already exist. Creating:$PATHOUT_TAR" |
1f268128 | 771 | ### If (no preexisting|appendable) tar found, create empty tar archive at PATHOUT_TAR |
9efeccb5 SBS |
772 | try tar --create --directory="$DIR_TMP" --file="$PATHOUT_TAR" --files-from=/dev/null && \ |
773 | vbm "DEBUG:Empty tar created at:$PATHOUT_TAR"; | |
1f268128 | 774 | fi |
6c30388f | 775 | ## Append VERSION file to PATHOUT_TAR |
93b52b83 | 776 | try tar --append --directory="$DIR_TMP" --file="$PATHOUT_TAR" "$FILEOUT_VERSION" && \ |
9efeccb5 | 777 | vbm "DEBUG:VERSION added to $PATHOUT_TAR"; |
6c30388f SBS |
778 | |
779 | # Record gps data until script lifespan ends | |
780 | declare debugCounter; debugCounter="0"; # set debug counter | |
6c30388f | 781 | while [[ "$SECONDS" -lt "$scriptTTL" ]]; do |
29ef133a | 782 | timeBufferStart="$(dateTimeShort)"; # Note start time |
6c30388f | 783 | # Determine file paths (time is start of buffer period) |
29ef133a | 784 | FILEOUT_BASENAME="$timeBufferStart""--""$bufferTTL_STR""..""$SCRIPT_HOSTNAME""_location" && vbm "STATUS:Set FILEOUT_BASENAME to:$FILEOUT_BASENAME"; |
aa2a49f1 | 785 | ## Files saved to DIR_TMP |
66af6225 SBS |
786 | FILEOUT_NMEA="$FILEOUT_BASENAME".nmea"$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX" && vbm "STATUS:Set FILEOUT_NMEA to:$FILEOUT_NMEA" |
787 | FILEOUT_GPX="$FILEOUT_BASENAME".gpx"$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX" && vbm "STATUS:Set FILEOUT_GPX to:$FILEOUT_GPX" | |
788 | FILEOUT_KML="$FILEOUT_BASENAME".kml"$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX" && vbm "STATUS:Set FILEOUT_KML to:$FILEOUT_KML" | |
789 | PATHOUT_NMEA="$DIR_TMP"/"$FILEOUT_NMEA" && vbm "STATUS:Set PATHOUT_NMEA to:$PATHOUT_NMEA"; | |
790 | PATHOUT_GPX="$DIR_TMP"/"$FILEOUT_GPX" && vbm "STATUS:Set PATHOUT_GPX to:$PATHOUT_GPX"; | |
791 | PATHOUT_KML="$DIR_TMP"/"$FILEOUT_KML" && vbm "STATUS:Set PATHOUT_KML to:$PATHOUT_KML"; | |
6c30388f SBS |
792 | ## Files saved to disk (DIR_OUT) |
793 | ### one file per day (Ex: "20200731..hostname_location.[.gpx.gz].tar") | |
9e75f849 | 794 | PATHOUT_TAR="$DIR_OUT"/"$(dateShort)".."$SCRIPT_HOSTNAME""_location""$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX".tar && \ |
405ce7df | 795 | vbm "STATUS:Set PATHOUT_TAR to:$PATHOUT_TAR" |
6c30388f | 796 | # Define GPS conversion commands |
405ce7df | 797 | CMD_CONV_NMEA="tee /dev/null " && vbm "STATUS:Set CMD_CONV_NMEA to:$CMD_CONV_NMEA"; # tee as passthrough |
66af6225 SBS |
798 | CMD_CONV_GPX="gpsbabel -i nmea -f - -o gpx -F - " && vbm "STATUS:Set CMD_CONV_GPX to:$CMD_CONV_GPX"; # convert NMEA to GPX |
799 | CMD_CONV_KML="gpsbabel -i nmea -f - -o kml -F - " && vbm "STATUS:Set CMD_CONV_KML to:$CMD_CONV_KML"; # convert NMEA to KML | |
6c30388f | 800 | # Fill Bash variable buffer |
18490e2b | 801 | bufferBash="$(timeout "$BUFFER_TTL""s" gpspipe -r)" && vbm "STATUS:Successfully filled bufferBash variable with gpspipe data."; # Record gpspipe nmea data to buffer for bufferTTL seconds |
6c30388f | 802 | # Process bufferBash, save secured chunk set to DIR_TMP |
18490e2b | 803 | vbm "STATUS:Beginning to save data to $DIR_TMP"; |
66af6225 SBS |
804 | magicWriteBuffer & |
805 | #echo "$bufferBash" | $CMD_CONV_NMEA | $CMD_COMPRESS | $CMD_ENCRYPT > "$PATHOUT_NMEA" & # Create NMEA file (secured if requested) | |
806 | #echo "$bufferBash" | $CMD_CONV_GPX | $CMD_COMPRESS | $CMD_ENCRYPT > "$PATHOUT_GPX" & # Create GPX file (secured if requested) | |
807 | #echo "$bufferBash" | $CMD_CONV_KML | $CMD_COMPRESS | $CMD_ENCRYPT > "$PATHOUT_KML" & # Create KML file (secured if requested) | |
6c30388f | 808 | # Append each secured chunk in memory dir (DIR_TMP) to file on disk (PATHOUT_TAR in DIR_OUT) |
405ce7df SBS |
809 | vbm "STATUS:DIR_TMP :$DIR_TMP"; |
810 | vbm "STATUS:PATHOUT_TAR :$PATHOUT_TAR"; | |
811 | vbm "STATUS:PATHOUT_NMEA:$PATHOUT_NMEA"; | |
66af6225 SBS |
812 | vbm "STATUS:PATHOUT_GPX:$PATHOUT_GPX"; |
813 | vbm "STATUS:PATHOUT_KML:$PATHOUT_KML"; | |
18490e2b | 814 | try tar --append --directory="$DIR_TMP" --file="$PATHOUT_TAR" "$(basename "$PATHOUT_NMEA")" && \ |
6c30388f | 815 | vbm "DEBUG:Appended NMEA location data $PATHOUT_NMEA to $PATHOUT_TAR"; |
18490e2b | 816 | try tar --append --directory="$DIR_TMP" --file="$PATHOUT_TAR" "$(basename "$PATHOUT_GPX")" && \ |
6c30388f | 817 | vbm "DEBUG:Appended GPX location data $PATHOUT_GPX to $PATHOUT_TAR"; |
18490e2b | 818 | try tar --append --directory="$DIR_TMP" --file="$PATHOUT_TAR" "$(basename "$PATHOUT_KML")" && \ |
6c30388f SBS |
819 | vbm "DEBUG:Appended KML location $PATHOUT_KML to $PATHOUT_TAR"; |
820 | # Remove secured chunks from DIR_TMP | |
8d0ec0dc | 821 | try rm "$PATHOUT_NMEA" "$PATHOUT_GPX" "$PATHOUT_KML"; |
6c30388f | 822 | # Reset buffer and filenames |
29ef133a | 823 | unset bufferBash FILEOUT_BASENAME PATHOUT_NMEA PATHOUT_GPX PATHOUT_KML PATHOUT_TAR timeBufferStart; |
18490e2b | 824 | vbm "DEBUG:Completed buffer session $debugCounter ." 1>&2; |
6c30388f SBS |
825 | ((debugCounter++)) |
826 | done | |
cfc25c90 SBS |
827 | |
828 | # Remove DIR_TMP | |
829 | try rm -r "$DIR_TMP"; | |
0aabe6a3 SBS |
830 | |
831 | vbm "STATUS:Main function finished." | |
8fbca23d SBS |
832 | } # Main function. |
833 | #===END Declare local script functions=== | |
834 | #==END Define script parameters== | |
032f4b05 SBS |
835 | |
836 | ||
8fbca23d SBS |
837 | #==BEGIN Perform work and exit== |
838 | main "$@" # Run main function. | |
839 | exit 0; | |
840 | #==END Perform work and exit== |