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
" -c, --compress"
155 echoerr
" Compress output with gzip (before encryption if enabled)."
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.
164 echoerr
"$SCRIPT_VERSION"
165 } # Display script version.
167 # Usage: vbm "DEBUG:verbose message here"
168 # Description: Prints verbose message ("vbm") to stderr if OPTION_VERBOSE is set to "true".
170 # - OPTION_VERBOSE variable set by processArguments function. (ex: "true", "false")
171 # - "$@" positional arguments fed to this function.
173 # Script function dependencies: echoerr
174 # External function dependencies: echo
175 # Last modified: 2020-04-11T23:57Z
176 # Last modified by: Steven Baltakatei Sandoval
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.
186 return 0; # Function finished.
187 } # Verbose message display function.
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:""$*"
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.
205 } # Argument Processing
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
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.";
226 try
echo "$SECONDS_UNTIL_NEXT_DAY"; # Report
228 #===Determine function return code===
229 if [[ "$returnState" = "true" ]]; then
231 elif [[ "$returnState" = "WARNING_ZERO" ]]; then
233 elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then
236 } # Report seconds until next day
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
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.";
257 try
echo "$SECONDS_UNTIL_NEXT_HOUR"; # Report
259 #===Determine function return code===
260 if [[ "$returnState" = "true" ]]; then
262 elif [[ "$returnState" = "WARNING_ZERO" ]]; then
264 elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then
267 } # Report seconds until next hour
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
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";
289 yell
"ERROR:Exit code ""$?"". Invalid recipient pubkey string. Exiting."; exit 1;
292 vbm
"DEBUG:Finished processing recPubKeys array";
293 # Form age command string
294 CMD_ENCRYPT
="age ""$recipients ";
295 CMD_ENCRYPT_SUFFIX
=".age";
297 yell
"ERROR:Encryption enabled but \"age\" not found. Exiting."; exit 1;
300 CMD_ENCRYPT
="tee /dev/null ";
301 CMD_ENCRYPT_SUFFIX
="";
302 vbm
"DEBUG:Encryption not enabled."
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";
310 yell
"ERROR:Compression enabled but \"gzip\" not found. Exiting."; exit 1;
313 CMD_COMPRESS
="tee /dev/null ";
314 CMD_COMPRESS_SUFFIX
="";
315 vbm
"DEBUG:Compression not enabled."
319 if checkapp gpspipe
&& checkdir
"$DIROUT"; then
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.
326 # yell "ERROR: timeUntilNextDay exit code $?"; exit 1;
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.
335 yell
"ERROR: timeUntilNextHour exit code $?"; exit 1;
339 # Determine buffer lifespan
342 # Record gps data until script lifespan ends
343 declare debugCounter
; debugCounter
="0"
344 while [[ "$SECONDS" -lt "$scriptTTL" ]]; do
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 # Define GPS conversion commands
352 CMD_CONV_NMEA
="tee /dev/null "
353 CMD_CONV_GPX
="gpsbabel -i nmea -f - -o gpx -F - "
354 CMD_CONV_KML
="gpsbabel -i nmea -f - -o kml -F - "
356 buffer
="$(timeout "$bufferTTL""s
" gpspipe -r)"; # Record gpspipe nmea data to buffer for bufferTTL seconds
357 # Execute processing and save command string
358 echo "$buffer" |
$CMD_CONV_NMEA |
$CMD_COMPRESS |
$CMD_ENCRYPT > "$DIROUT"/"$FILEOUT_NMEA" & # Save NMEA format
359 echo "$buffer" |
$CMD_CONV_GPX |
$CMD_COMPRESS |
$CMD_ENCRYPT > "$DIROUT"/"$FILEOUT_GPX" & # Save GPX format
360 echo "$buffer" |
$CMD_CONV_KML |
$CMD_COMPRESS |
$CMD_ENCRYPT > "$DIROUT"/"$FILEOUT_KML" & # Save KML format
361 vbm
"DEBUG:Completed buffer session $debugCounter ." 1>&2;
362 # Reset buffer and filenames
363 unset buffer FILEOUT_BASENAME FILEOUT_NMEA FILEOUT_GPX FILEOUT_KML
;
367 #===END Declare local script functions===
368 #==END Define script parameters==
371 #==BEGIN Perform work and exit==
372 main
"$@" # Run main function.
374 #==END Perform work and exit==
378 #gpspipe -r -d -l -o "$DIROUT1"/"$FILEOUT1" ;