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 SBS |
153 | echoerr |
154 | echoerr "EXAMPLE: (bash script lines)" | |
155 | echoerr "/bin/bash bkgpslog -e \\" | |
156 | echoerr "-r age1mrmfnwhtlprn4jquex0ukmwcm7y2nxlphuzgsgv8ew2k9mewy3rs8u7su5 \\" | |
157 | echoerr "-r age1ala848kqrvxc88rzaauc6vc5v0fqrvef9dxyk79m0vjea3hagclswu0lgq \\" | |
158 | echoerr "-o ~/Sync/Location" | |
032f4b05 SBS |
159 | } # Display information on how to use this script. |
160 | showVersion() { | |
161 | echoerr "$SCRIPT_VERSION" | |
162 | } # Display script version. | |
163 | vbm() { | |
0b3dde05 SBS |
164 | # Usage: vbm "DEBUG:verbose message here" |
165 | # Description: Prints verbose message ("vbm") to stderr if OPTION_VERBOSE is set to "true". | |
166 | # Input: | |
167 | # - OPTION_VERBOSE variable set by processArguments function. (ex: "true", "false") | |
168 | # - "$@" positional arguments fed to this function. | |
169 | # Output: stderr | |
170 | # Script function dependencies: echoerr | |
171 | # External function dependencies: echo | |
172 | # Last modified: 2020-04-11T23:57Z | |
173 | # Last modified by: Steven Baltakatei Sandoval | |
174 | # License: GPLv3+ | |
175 | # Ref./Attrib: | |
176 | ||
177 | if [ "$OPTION_VERBOSE" = "true" ]; then | |
178 | FUNCTION_TIME=$(date --iso-8601=ns); # Save current time in nano seconds. | |
179 | echoerr "[$FUNCTION_TIME] ""$*"; # Display argument text. | |
032f4b05 | 180 | fi |
0b3dde05 SBS |
181 | |
182 | # End function | |
183 | return 0; # Function finished. | |
032f4b05 SBS |
184 | } # Verbose message display function. |
185 | processArguments() { | |
186 | while [ ! $# -eq 0 ]; do # While number of arguments ($#) is not (!) equal to (-eq) zero (0). | |
d6896d3b SBS |
187 | #echoerr "DEBUG:Starting processArguments while loop." |
188 | #echoerr "DEBUG:Provided arguments are:""$*" | |
032f4b05 | 189 | case "$1" in |
c1bbf9f7 | 190 | -h | --help) showUsage; exit 1;; # Display usage. |
032f4b05 | 191 | --version) showVersion; exit 1;; # Show version |
c1bbf9f7 SBS |
192 | -v | --verbose) OPTION_VERBOSE="true"; vbm "DEBUG:Verbose mode enabled.";; # Enable verbose mode. |
193 | -o | --output) if [ -d "$2" ]; then DIROUT="$2"; vbm "DEBUG:DIROUT:$DIROUT"; shift; fi ;; # Define output directory. | |
17d49005 SBS |
194 | -e | --encrypt) OPTION_ENCRYPT="true"; vbm "DEBUG:Encrypted output mode enabled.";; |
195 | -r | --recipient) # Add 'age' recipient via public key string | |
77361307 | 196 | recPubKeys+=("$2"); vbm "pubkey added:""$2"; shift;; |
032f4b05 SBS |
197 | *) echoerr "ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options. |
198 | esac | |
199 | shift | |
200 | done | |
201 | } # Argument Processing | |
c2aaff78 SBS |
202 | timeUntilNextDay(){ |
203 | # Desc: Report seconds until next day. | |
204 | # Output: stdout: integer seconds until next day | |
8fbca23d | 205 | # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0 |
c2aaff78 SBS |
206 | # Usage: timeUntilNextDay |
207 | # Usage: if ! myTTL="$(timeUntilNextDay)"; then yell "ERROR in if statement"; exit 1; fi | |
208 | local returnState TIME_CURRENT TIME_NEXT_DAY SECONDS_UNTIL_NEXT_DAY | |
8fbca23d | 209 | TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second. |
c2aaff78 SBS |
210 | TIME_NEXT_DAY="$(date -d "$TIME_CURRENT next day" --iso-8601=date)"; # Produce timestamp of beginning of tomorrow with resolution of 1 second. |
211 | SECONDS_UNTIL_NEXT_DAY="$(( $(date +%s -d "$TIME_NEXT_DAY") - $(date +%s -d "$TIME_CURRENT") ))" ; # Calculate seconds until closest future midnight (res. 1 second). | |
212 | if [[ "$SECONDS_UNTIL_NEXT_DAY" -gt 0 ]]; then | |
213 | returnState="true"; | |
214 | elif [[ "$SECONDS_UNTIL_NEXT_DAY" -eq 0 ]]; then | |
215 | returnState="WARNING_ZERO"; | |
216 | yell "WARNING:Reported time until next day exactly zero."; | |
217 | elif [[ "$SECONDS_UNTIL_NEXT_DAY" -lt 0 ]]; then | |
218 | returnState="WARNING_NEGATIVE"; | |
219 | yell "WARNING:Reported time until next day is negative."; | |
220 | fi | |
221 | ||
222 | try echo "$SECONDS_UNTIL_NEXT_DAY"; # Report | |
223 | ||
224 | #===Determine function return code=== | |
225 | if [[ "$returnState" = "true" ]]; then | |
226 | return 0; | |
227 | elif [[ "$returnState" = "WARNING_ZERO" ]]; then | |
228 | return 1; | |
229 | elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then | |
230 | return 2; | |
231 | fi | |
232 | } # Report seconds until next day | |
233 | timeUntilNextHour(){ | |
234 | # Desc: Report seconds until next hour | |
235 | # Output: stdout: integer seconds until next hour | |
236 | # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0 | |
237 | # Usage: timeUntilNextHour | |
238 | # Usage: if ! myTTL="$(timeUntilNextHour)"; then yell "ERROR in if statement"; exit 1; fi | |
239 | local returnState TIME_CURRENT TIME_NEXT_HOUR SECONDS_UNTIL_NEXT_HOUR | |
240 | TIME_CURRENT="$(date --iso-8601=seconds)"; # Produce `date`-parsable current timestamp with resolution of 1 second. | |
241 | TIME_NEXT_HOUR="$(date -d "$TIME_CURRENT next hour" --iso-8601=hours)"; # Produce `date`-parsable current time stamp with resolution of 1 second. | |
242 | SECONDS_UNTIL_NEXT_HOUR="$(( $(date +%s -d "$TIME_NEXT_HOUR") - $(date +%s -d "$TIME_CURRENT") ))"; # Calculate seconds until next hour (res. 1 second). | |
243 | if [[ "$SECONDS_UNTIL_NEXT_HOUR" -gt 0 ]]; then | |
8fbca23d | 244 | returnState="true"; |
c2aaff78 | 245 | elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -eq 0 ]]; then |
8fbca23d | 246 | returnState="WARNING_ZERO"; |
c2aaff78 SBS |
247 | yell "WARNING:Reported time until next hour exactly zero."; |
248 | elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -lt 0 ]]; then | |
8fbca23d | 249 | returnState="WARNING_NEGATIVE"; |
c2aaff78 | 250 | yell "WARNING:Reported time until next hour is negative."; |
8fbca23d | 251 | fi |
032f4b05 | 252 | |
c2aaff78 | 253 | try echo "$SECONDS_UNTIL_NEXT_HOUR"; # Report |
8fbca23d SBS |
254 | |
255 | #===Determine function return code=== | |
256 | if [[ "$returnState" = "true" ]]; then | |
257 | return 0; | |
258 | elif [[ "$returnState" = "WARNING_ZERO" ]]; then | |
259 | return 1; | |
260 | elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then | |
261 | return 2; | |
262 | fi | |
c2aaff78 | 263 | } # Report seconds until next hour |
8fbca23d SBS |
264 | dateTimeShort(){ |
265 | # Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz) | |
266 | # Usage: dateTimeShort | |
267 | # Output: stdout: timestamp (ISO-8601, no separators) | |
c2aaff78 | 268 | local TIME_CURRENT TIME_CURRENT_SHORT |
8fbca23d SBS |
269 | TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second. |
270 | TIME_CURRENT_SHORT="$(date -d "$TIME_CURRENT" +%Y%m%dT%H%M%S%z)"; # Produce separator-less current timestamp with resolution 1 second. | |
271 | echo "$TIME_CURRENT_SHORT"; | |
272 | } # Get date&time without separators | |
032f4b05 | 273 | main() { |
0b3dde05 | 274 | processArguments "$@" # Process arguments. |
17d49005 SBS |
275 | if [[ "$OPTION_ENCRYPT" = "true" ]]; then # Check if encryption option active. |
276 | if checkapp age; then # Check that age is available. | |
277 | for pubkey in "${recPubKeys[@]}"; do # Validate recipient pubkey strings by forming test message | |
278 | vbm "DEBUG:Testing pubkey string:$pubkey" | |
279 | if ! echo "butts" | age -a -r "$pubkey" 1>/dev/null; then | |
77361307 SBS |
280 | yell "ERROR:Exit code ""$?"". Invalid recipient pubkey string. Exiting."; exit 1; |
281 | else | |
282 | # Form age recipient string | |
283 | recipients="$recipients""-r $pubkey "; | |
284 | vbm "Added pubkey:""$pubkey"; | |
285 | vbm "DEBUG:recipients:""$recipients"; | |
286 | fi | |
17d49005 | 287 | done |
77361307 | 288 | vbm "DEBUG:Finished processing recPubKeys array"; |
17d49005 SBS |
289 | else |
290 | yell "ERROR:Encryption enabled but \"age\" not found. Exiting."; exit 1; | |
291 | fi | |
292 | fi | |
293 | ||
8fbca23d | 294 | if checkapp gpspipe && checkdir "$DIROUT"; then |
17d49005 | 295 | |
c2aaff78 SBS |
296 | # # Set script lifespan to end at start of next day |
297 | # if ! scriptTTL="$(timeUntilNextDay)"; then | |
298 | # if [[ "$scriptTTL" -eq 0 ]]; then | |
299 | # ((scriptTTL++)); # Add 1 because 0 would cause 'timeout' to never timeout. | |
300 | # else | |
301 | # yell "ERROR: timeUntilNextDay exit code $?"; exit 1; | |
302 | # fi; | |
303 | # fi; | |
032f4b05 | 304 | |
c2aaff78 SBS |
305 | # Set script lifespan to end at start of next hour |
306 | if ! scriptTTL="$(timeUntilNextHour)"; then | |
307 | if [[ "$scriptTTL" -eq 0 ]]; then | |
308 | ((scriptTTL++)); # Add 1 because 0 would cause 'timeout' to never timeout. | |
309 | else | |
310 | yell "ERROR: timeUntilNextHour exit code $?"; exit 1; | |
311 | fi; | |
312 | fi; | |
8fbca23d SBS |
313 | |
314 | # Determine buffer lifespan | |
94e094d1 | 315 | bufferTTL="60"; |
032f4b05 | 316 | |
5b601ed5 | 317 | # Record gps data until script lifespan ends |
8fbca23d SBS |
318 | declare debugCounter; debugCounter="0" |
319 | while [[ "$SECONDS" -lt "$scriptTTL" ]]; do | |
320 | ((debugCounter++)) | |
b9a85f63 | 321 | # Determine output file paths (time is start of buffer period) |
fde89ac0 | 322 | FILEOUT_BASENAME="$(dateTimeShort)""--P""$bufferTTL""S..""$SCRIPT_HOSTNAME""_location" ; # ISO-8601 YYYYmmddTHHMMSS+zzP[$bufferTTL]S |
20dae002 SBS |
323 | FILEOUT_NMEA="$FILEOUT_BASENAME".nmea ; |
324 | FILEOUT_GPX="$FILEOUT_BASENAME".gpx ; | |
325 | FILEOUT_KML="$FILEOUT_BASENAME".kml ; | |
b9a85f63 SBS |
326 | # Fill buffer |
327 | buffer="$(timeout "$bufferTTL""s" gpspipe -r)"; # Record gpspipe nmea data to buffer for bufferTTL seconds | |
328 | # Process and save buffers | |
329 | echo "$buffer" > "$DIROUT"/"$FILEOUT_NMEA" & # Save NMEA format | |
330 | echo "$buffer" | gpsbabel -i nmea -f - -o gpx -F - > "$DIROUT"/"$FILEOUT_GPX" & # Save GPX format | |
331 | echo "$buffer" | gpsbabel -i nmea -f - -o kml -F - > "$DIROUT"/"$FILEOUT_KML" & # Save KML format | |
94e094d1 | 332 | vbm "DEBUG:Completed buffer session $debugCounter ." 1>&2; |
f8ea736f | 333 | # Reset buffer and filenames |
d4ead4d1 | 334 | unset buffer FILEOUT_BASENAME FILEOUT_NMEA FILEOUT_GPX FILEOUT_KML; |
8fbca23d SBS |
335 | done |
336 | fi | |
337 | } # Main function. | |
338 | #===END Declare local script functions=== | |
339 | #==END Define script parameters== | |
032f4b05 SBS |
340 | |
341 | ||
8fbca23d SBS |
342 | #==BEGIN Perform work and exit== |
343 | main "$@" # Run main function. | |
344 | exit 0; | |
345 | #==END Perform work and exit== | |
032f4b05 SBS |
346 | |
347 | ||
032f4b05 SBS |
348 | |
349 | #gpspipe -r -d -l -o "$DIROUT1"/"$FILEOUT1" ; |