3 # Desc: Records gps data until midnight
4 # Author: Steven Baltakatei Sandoval; License: GPLv3+
5 # Usage: bkgpslog --output [output dir]
7 #==BEGIN Define script parameters==
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.
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
17 declare -a recPubKeys
# for processArguments function
18 declare recipients
# for main function
20 #===BEGIN Declare local script functions===
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'
27 #echo "DEBUG:$(date +%S.%N)..Starting checkapp function."
28 #echo "DEBUG:args: $@"
29 #echo "DEBUG:returnState:$returnState"
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
39 appRollCall
[$arg]="false"; returnState
="false";
43 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
44 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
46 #===Determine function return code===
47 if [ "$returnState" = "true" ]; then
48 #echo "DEBUG:checkapp returns true for $arg";
51 #echo "DEBUG:checkapp returns false for $arg";
54 } # Check that app exists
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
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
71 fileRollCall
["$arg"]="false"; returnState
="false";
75 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:fileRollCall key [$key] is:${fileRollCall[$key]}"; done
76 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
78 #===Determine function return code===
79 if [ "$returnState" = "true" ]; then
80 #echo "DEBUG:checkapp returns true for $arg";
83 #echo "DEBUG:checkapp returns false for $arg";
86 } # Check that file exists
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
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";
109 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:dirRollCall key [$key] is:${dirRollCall[$key]}"; done
110 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
112 #===Determine function return code===
113 if [ "$returnState" = "true" ]; then
114 #echo "DEBUG:checkapp returns true for $arg";
117 #echo "DEBUG:checkapp returns false for $arg";
120 } # Check that dir exists
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 $*"; }
129 echo "$@" 1>&2; # Define stderr echo function.
130 } # Define stderr message function.
133 echoerr
" bkgpslog [ options ]"
136 echoerr
" -h, --help"
137 echoerr
" Display help information."
140 echoerr
" Display script version."
142 echoerr
" -v, --verbose"
143 echoerr
" Display debugging info."
145 echoerr
" -e, --encrypt"
146 echoerr
" Encrypt output."
148 echoerr
" -r, --recipient [ pubkey string ]"
149 echoerr
" Specify recipient."
151 echoerr
" -o, --output [ directory ]"
152 echoerr
" Specify output directory to save logs."
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"
159 } # Display information on how to use this script.
161 echoerr
"$SCRIPT_VERSION"
162 } # Display script version.
164 # Usage: vbm "DEBUG:verbose message here"
165 # Description: Prints verbose message ("vbm") to stderr if OPTION_VERBOSE is set to "true".
167 # - OPTION_VERBOSE variable set by processArguments function. (ex: "true", "false")
168 # - "$@" positional arguments fed to this function.
170 # Script function dependencies: echoerr
171 # External function dependencies: echo
172 # Last modified: 2020-04-11T23:57Z
173 # Last modified by: Steven Baltakatei Sandoval
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.
183 return 0; # Function finished.
184 } # Verbose message display function.
186 while [ ! $# -eq 0 ]; do # While number of arguments ($#) is not (!) equal to (-eq) zero (0).
187 #echoerr "DEBUG:Starting processArguments while loop."
188 #echoerr "DEBUG:Provided arguments are:""$*"
190 -h |
--help) showUsage
; exit 1;; # Display usage.
191 --version) showVersion
; exit 1;; # Show version
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.
194 -e |
--encrypt) OPTION_ENCRYPT
="true"; vbm
"DEBUG:Encrypted output mode enabled.";;
195 -r |
--recipient) # Add 'age' recipient via public key string
196 recPubKeys
+=("$2"); vbm
"pubkey added:""$2"; shift;;
197 *) echoerr
"ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options.
201 } # Argument Processing
203 # Desc: Report seconds until next day.
204 # Output: stdout: integer seconds until next day
205 # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0
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
209 TIME_CURRENT
="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
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
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.";
222 try
echo "$SECONDS_UNTIL_NEXT_DAY"; # Report
224 #===Determine function return code===
225 if [[ "$returnState" = "true" ]]; then
227 elif [[ "$returnState" = "WARNING_ZERO" ]]; then
229 elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then
232 } # Report seconds until next day
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
245 elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -eq 0 ]]; then
246 returnState
="WARNING_ZERO";
247 yell
"WARNING:Reported time until next hour exactly zero.";
248 elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -lt 0 ]]; then
249 returnState
="WARNING_NEGATIVE";
250 yell
"WARNING:Reported time until next hour is negative.";
253 try
echo "$SECONDS_UNTIL_NEXT_HOUR"; # Report
255 #===Determine function return code===
256 if [[ "$returnState" = "true" ]]; then
258 elif [[ "$returnState" = "WARNING_ZERO" ]]; then
260 elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then
263 } # Report seconds until next hour
265 # Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz)
266 # Usage: dateTimeShort
267 # Output: stdout: timestamp (ISO-8601, no separators)
268 local TIME_CURRENT TIME_CURRENT_SHORT
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
274 processArguments
"$@" # Process arguments.
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
280 yell
"ERROR:Exit code ""$?"". Invalid recipient pubkey string. Exiting."; exit 1;
282 # Form age recipient string
283 recipients
="$recipients""-r $pubkey ";
284 vbm
"Added pubkey:""$pubkey";
285 vbm
"DEBUG:recipients:""$recipients";
288 vbm
"DEBUG:Finished processing recPubKeys array";
290 yell
"ERROR:Encryption enabled but \"age\" not found. Exiting."; exit 1;
294 if checkapp gpspipe
&& checkdir
"$DIROUT"; then
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.
301 # yell "ERROR: timeUntilNextDay exit code $?"; exit 1;
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.
310 yell
"ERROR: timeUntilNextHour exit code $?"; exit 1;
314 # Determine buffer lifespan
317 # Record gps data until script lifespan ends
318 declare debugCounter
; debugCounter
="0"
319 while [[ "$SECONDS" -lt "$scriptTTL" ]]; do
321 # Determine output file paths (time is start of buffer period)
322 FILEOUT_BASENAME
="$(dateTimeShort)""--P""$bufferTTL""S..""$SCRIPT_HOSTNAME""_location" ; # ISO-8601 YYYYmmddTHHMMSS+zzP[$bufferTTL]S
323 FILEOUT_NMEA
="$FILEOUT_BASENAME".nmea
;
324 FILEOUT_GPX
="$FILEOUT_BASENAME".gpx
;
325 FILEOUT_KML
="$FILEOUT_BASENAME".kml
;
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
332 vbm
"DEBUG:Completed buffer session $debugCounter ." 1>&2;
333 # Reset buffer and filenames
334 unset buffer FILEOUT_BASENAME FILEOUT_NMEA FILEOUT_GPX FILEOUT_KML
;
338 #===END Declare local script functions===
339 #==END Define script parameters==
342 #==BEGIN Perform work and exit==
343 main
"$@" # Run main function.
345 #==END Perform work and exit==
349 #gpspipe -r -d -l -o "$DIROUT1"/"$FILEOUT1" ;