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+ | |
5 | # Usage: bkgpslog --output [output dir] | |
032f4b05 | 6 | |
8fbca23d | 7 | #==BEGIN Define script parameters== |
032f4b05 SBS |
8 | PATH="/opt/bktei:$PATH" # Add default baltakatei script install directory to PATH (necessary for other bk scripts) |
9 | SCRIPT_HOSTNAME=$(hostname) # Save hostname of system running this script. | |
10 | SCRIPT_VERSION="bkgpslog 0.0.1" # Define version of script. | |
11 | SCRIPT_TIME_SHORT="$(date +%Y%m%dT%H%M%S%z)" # Save current date & time in ISO-8601 format. | |
12 | SCRIPT_DATE_SHORT="$(date +%Y%m%d)" # Save current date in ISO-8601 format. | |
8fbca23d SBS |
13 | |
14 | declare -Ag appRollCall # Associative array for storing app status | |
15 | declare -Ag fileRollCall # Associative array for storing file status | |
16 | declare -Ag dirRollCall # Associative array for storing dir status | |
17d49005 | 17 | declare -a recPubKeys # for processArguments function |
77361307 | 18 | declare recipients # for main function |
8fbca23d SBS |
19 | |
20 | #===BEGIN Declare local script functions=== | |
21 | checkapp() { | |
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" | |
0b3dde05 | 34 | if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command |
8fbca23d SBS |
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 | |
55 | checkfile() { | |
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 | |
87 | checkdir() { | |
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 | |
bcf09dcc | 102 | elif [ "$arg" = "" ]; then |
8fbca23d | 103 | dirRollCall["$arg"]="false"; returnState="false"; |
bcf09dcc SBS |
104 | else |
105 | returnState="false"; | |
8fbca23d SBS |
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 | |
032f4b05 | 121 | |
c609b9c8 SBS |
122 | # Yell, Die, Try Three-Fingered Claw technique |
123 | # Ref/Attrib: https://stackoverflow.com/a/25515370 | |
124 | yell() { echo "$0: $*" >&2; } | |
125 | die() { yell "$*"; exit 111; } | |
126 | try() { "$@" || die "cannot $*"; } | |
127 | ||
032f4b05 SBS |
128 | echoerr() { |
129 | echo "$@" 1>&2; # Define stderr echo function. | |
130 | } # Define stderr message function. | |
131 | showUsage() { | |
132 | echoerr "USAGE:" | |
94e094d1 | 133 | echoerr " bkgpslog [ options ]" |
032f4b05 SBS |
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 | |
17d49005 SBS |
145 | echoerr " -e, --encrypt" |
146 | echoerr " Encrypt output." | |
147 | echoerr | |
148 | echoerr " -r, --recipient [ pubkey string ]" | |
149 | echoerr " Specify recipient." | |
150 | echoerr | |
032f4b05 SBS |
151 | echoerr " -o, --output [ directory ]" |
152 | echoerr " Specify output directory to save logs." | |
17d49005 | 153 | echoerr |
408a342b SBS |
154 | echoerr " -c, --compress" |
155 | echoerr " Compress output with gzip (before encryption if enabled)." | |
156 | echoerr | |
17d49005 | 157 | echoerr "EXAMPLE: (bash script lines)" |
408a342b | 158 | echoerr "/bin/bash bkgpslog -e -c \\" |
17d49005 SBS |
159 | echoerr "-r age1mrmfnwhtlprn4jquex0ukmwcm7y2nxlphuzgsgv8ew2k9mewy3rs8u7su5 \\" |
160 | echoerr "-r age1ala848kqrvxc88rzaauc6vc5v0fqrvef9dxyk79m0vjea3hagclswu0lgq \\" | |
161 | echoerr "-o ~/Sync/Location" | |
032f4b05 SBS |
162 | } # Display information on how to use this script. |
163 | showVersion() { | |
164 | echoerr "$SCRIPT_VERSION" | |
165 | } # Display script version. | |
166 | vbm() { | |
0b3dde05 SBS |
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. | |
032f4b05 | 183 | fi |
0b3dde05 SBS |
184 | |
185 | # End function | |
186 | return 0; # Function finished. | |
032f4b05 SBS |
187 | } # Verbose message display function. |
188 | processArguments() { | |
189 | while [ ! $# -eq 0 ]; do # While number of arguments ($#) is not (!) equal to (-eq) zero (0). | |
d6896d3b SBS |
190 | #echoerr "DEBUG:Starting processArguments while loop." |
191 | #echoerr "DEBUG:Provided arguments are:""$*" | |
032f4b05 | 192 | case "$1" in |
c1bbf9f7 | 193 | -h | --help) showUsage; exit 1;; # Display usage. |
032f4b05 | 194 | --version) showVersion; exit 1;; # Show version |
c1bbf9f7 SBS |
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. | |
17d49005 SBS |
197 | -e | --encrypt) OPTION_ENCRYPT="true"; vbm "DEBUG:Encrypted output mode enabled.";; |
198 | -r | --recipient) # Add 'age' recipient via public key string | |
77361307 | 199 | recPubKeys+=("$2"); vbm "pubkey added:""$2"; shift;; |
408a342b | 200 | -c | --compress) OPTION_COMPRESS="true"; vbm "DEBUG:Compressed output mode enabled.";; |
032f4b05 SBS |
201 | *) echoerr "ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options. |
202 | esac | |
203 | shift | |
204 | done | |
205 | } # Argument Processing | |
c2aaff78 SBS |
206 | timeUntilNextDay(){ |
207 | # Desc: Report seconds until next day. | |
208 | # Output: stdout: integer seconds until next day | |
8fbca23d | 209 | # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0 |
c2aaff78 SBS |
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 | |
8fbca23d | 213 | TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second. |
c2aaff78 SBS |
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 | |
237 | timeUntilNextHour(){ | |
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 | |
8fbca23d | 248 | returnState="true"; |
c2aaff78 | 249 | elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -eq 0 ]]; then |
8fbca23d | 250 | returnState="WARNING_ZERO"; |
c2aaff78 SBS |
251 | yell "WARNING:Reported time until next hour exactly zero."; |
252 | elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -lt 0 ]]; then | |
8fbca23d | 253 | returnState="WARNING_NEGATIVE"; |
c2aaff78 | 254 | yell "WARNING:Reported time until next hour is negative."; |
8fbca23d | 255 | fi |
032f4b05 | 256 | |
c2aaff78 | 257 | try echo "$SECONDS_UNTIL_NEXT_HOUR"; # Report |
8fbca23d SBS |
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 | |
c2aaff78 | 267 | } # Report seconds until next hour |
8fbca23d SBS |
268 | dateTimeShort(){ |
269 | # Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz) | |
270 | # Usage: dateTimeShort | |
271 | # Output: stdout: timestamp (ISO-8601, no separators) | |
c2aaff78 | 272 | local TIME_CURRENT TIME_CURRENT_SHORT |
8fbca23d SBS |
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 | |
032f4b05 | 277 | main() { |
0b3dde05 | 278 | processArguments "$@" # Process arguments. |
17d49005 SBS |
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" | |
408a342b | 283 | if echo "butts" | age -a -r "$pubkey" 1>/dev/null; then |
77361307 SBS |
284 | # Form age recipient string |
285 | recipients="$recipients""-r $pubkey "; | |
408a342b | 286 | vbm "Added pubkey for forming age recipient string:""$pubkey"; |
77361307 | 287 | vbm "DEBUG:recipients:""$recipients"; |
408a342b SBS |
288 | else |
289 | yell "ERROR:Exit code ""$?"". Invalid recipient pubkey string. Exiting."; exit 1; | |
77361307 | 290 | fi |
17d49005 | 291 | done |
77361307 | 292 | vbm "DEBUG:Finished processing recPubKeys array"; |
408a342b SBS |
293 | # Form age command string |
294 | CMD_ENCRYPT=" | age ""$recipients "; | |
295 | CMD_ENCRYPT_SUFFIX=".age"; | |
17d49005 SBS |
296 | else |
297 | yell "ERROR:Encryption enabled but \"age\" not found. Exiting."; exit 1; | |
298 | fi | |
408a342b SBS |
299 | else |
300 | CMD_ENCRYPT=""; | |
301 | CMD_ENCRYPT_SUFFIX=""; | |
302 | vbm "DEBUG:Encryption not enabled." | |
17d49005 | 303 | fi |
408a342b SBS |
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; | |
286d9825 | 311 | fi |
408a342b SBS |
312 | else |
313 | CMD_COMPRESS=""; | |
314 | CMD_COMPRESS_SUFFIX=""; | |
315 | vbm "DEBUG:Compression not enabled." | |
316 | fi | |
317 | ||
17d49005 | 318 | |
8fbca23d | 319 | if checkapp gpspipe && checkdir "$DIROUT"; then |
17d49005 | 320 | |
c2aaff78 SBS |
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; | |
032f4b05 | 329 | |
c2aaff78 SBS |
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; | |
8fbca23d SBS |
338 | |
339 | # Determine buffer lifespan | |
94e094d1 | 340 | bufferTTL="60"; |
032f4b05 | 341 | |
5b601ed5 | 342 | # Record gps data until script lifespan ends |
8fbca23d SBS |
343 | declare debugCounter; debugCounter="0" |
344 | while [[ "$SECONDS" -lt "$scriptTTL" ]]; do | |
345 | ((debugCounter++)) | |
b9a85f63 | 346 | # Determine output file paths (time is start of buffer period) |
fde89ac0 | 347 | FILEOUT_BASENAME="$(dateTimeShort)""--P""$bufferTTL""S..""$SCRIPT_HOSTNAME""_location" ; # ISO-8601 YYYYmmddTHHMMSS+zzP[$bufferTTL]S |
408a342b SBS |
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" ; | |
b9a85f63 SBS |
351 | # Fill buffer |
352 | buffer="$(timeout "$bufferTTL""s" gpspipe -r)"; # Record gpspipe nmea data to buffer for bufferTTL seconds | |
353 | # Process and save buffers | |
4e95d56c SBS |
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 | |
94e094d1 | 357 | vbm "DEBUG:Completed buffer session $debugCounter ." 1>&2; |
f8ea736f | 358 | # Reset buffer and filenames |
d4ead4d1 | 359 | unset buffer FILEOUT_BASENAME FILEOUT_NMEA FILEOUT_GPX FILEOUT_KML; |
8fbca23d SBS |
360 | done |
361 | fi | |
362 | } # Main function. | |
363 | #===END Declare local script functions=== | |
364 | #==END Define script parameters== | |
032f4b05 SBS |
365 | |
366 | ||
8fbca23d SBS |
367 | #==BEGIN Perform work and exit== |
368 | main "$@" # Run main function. | |
369 | exit 0; | |
370 | #==END Perform work and exit== | |
032f4b05 SBS |
371 | |
372 | ||
032f4b05 SBS |
373 | |
374 | #gpspipe -r -d -l -o "$DIROUT1"/"$FILEOUT1" ; |