900b8c46355415c97a3a62f5d520ef0ef98ea4e2
[EVA-2020-02.git] / exec / bkgpslog
1 #!/bin/bash
2
3 # Desc: Records gps data until midnight
4 # Author: Steven Baltakatei Sandoval; License: GPLv3+
5 # Usage: bkgpslog --output [output dir]
6
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.
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
17
18 #===BEGIN Declare local script functions===
19 checkapp() {
20 # Desc: If arg is a command, save result in assoc array 'appRollCall'
21 # Usage: checkapp arg1 arg2 arg3 ...
22 # Input: global assoc. array 'appRollCall'
23 # Output: adds/updates key(value) to global assoc array 'appRollCall'
24 local returnState
25 #echo "DEBUG:$(date +%S.%N)..Starting checkapp function."
26 #echo "DEBUG:args: $@"
27 #echo "DEBUG:returnState:$returnState"
28
29 #===Process Args===
30 for arg in "$@"; do
31 #echo "DEBUG:processing arg:$arg"
32 if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command
33 appRollCall[$arg]="true";
34 #echo "DEBUG:appRollCall[$arg]:"${appRollCall[$arg]}
35 if ! [ "$returnState" = "false" ]; then returnState="true"; fi
36 else
37 appRollCall[$arg]="false"; returnState="false";
38 fi
39 done
40
41 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
42 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
43
44 #===Determine function return code===
45 if [ "$returnState" = "true" ]; then
46 #echo "DEBUG:checkapp returns true for $arg";
47 return 0;
48 else
49 #echo "DEBUG:checkapp returns false for $arg";
50 return 1;
51 fi
52 } # Check that app exists
53 checkfile() {
54 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
55 # Usage: checkfile arg1 arg2 arg3 ...
56 # Input: global assoc. array 'fileRollCall'
57 # Output: adds/updates key(value) to global assoc array 'fileRollCall';
58 # Output: returns 0 if app found, 1 otherwise
59 local returnState
60
61 #===Process Args===
62 for arg in "$@"; do
63 #echo "DEBUG:processing arg:$arg"
64 if [ -f "$arg" ]; then
65 fileRollCall["$arg"]="true";
66 #echo "DEBUG:fileRollCall[\"$arg\"]:"${fileRollCall["$arg"]}
67 if ! [ "$returnState" = "false" ]; then returnState="true"; fi
68 else
69 fileRollCall["$arg"]="false"; returnState="false";
70 fi
71 done
72
73 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:fileRollCall key [$key] is:${fileRollCall[$key]}"; done
74 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
75
76 #===Determine function return code===
77 if [ "$returnState" = "true" ]; then
78 #echo "DEBUG:checkapp returns true for $arg";
79 return 0;
80 else
81 #echo "DEBUG:checkapp returns false for $arg";
82 return 1;
83 fi
84 } # Check that file exists
85 checkdir() {
86 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
87 # Usage: checkdir arg1 arg2 arg3 ...
88 # Input: global assoc. array 'dirRollCall'
89 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
90 # Output: returns 0 if app found, 1 otherwise
91 local returnState
92
93 #===Process Args===
94 for arg in "$@"; do
95 #echo "DEBUG:processing arg:$arg"
96 if [ -d "$arg" ]; then
97 dirRollCall["$arg"]="true";
98 #echo "DEBUG:dirRollCall[\"$arg\"]:"${dirRollCall["$arg"]}
99 if ! [ "$returnState" = "false" ]; then returnState="true"; fi
100 elif [ "$arg" = "" ]; then
101 dirRollCall["$arg"]="false"; returnState="false";
102 else
103 returnState="false";
104 fi
105 done
106
107 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:dirRollCall key [$key] is:${dirRollCall[$key]}"; done
108 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
109
110 #===Determine function return code===
111 if [ "$returnState" = "true" ]; then
112 #echo "DEBUG:checkapp returns true for $arg";
113 return 0;
114 else
115 #echo "DEBUG:checkapp returns false for $arg";
116 return 1;
117 fi
118 } # Check that dir exists
119
120 # Yell, Die, Try Three-Fingered Claw technique
121 # Ref/Attrib: https://stackoverflow.com/a/25515370
122 yell() { echo "$0: $*" >&2; }
123 die() { yell "$*"; exit 111; }
124 try() { "$@" || die "cannot $*"; }
125
126 echoerr() {
127 echo "$@" 1>&2; # Define stderr echo function.
128 } # Define stderr message function.
129 showUsage() {
130 echoerr "USAGE:"
131 echoerr " bkgpslog [ options ]"
132 echoerr
133 echoerr "OPTIONS:"
134 echoerr " -h, --help"
135 echoerr " Display help information."
136 echoerr
137 echoerr " --version"
138 echoerr " Display script version."
139 echoerr
140 echoerr " -v, --verbose"
141 echoerr " Display debugging info."
142 echoerr
143 echoerr " -o, --output [ directory ]"
144 echoerr " Specify output directory to save logs."
145 } # Display information on how to use this script.
146 showVersion() {
147 echoerr "$SCRIPT_VERSION"
148 } # Display script version.
149 vbm() {
150 # Usage: vbm "DEBUG:verbose message here"
151 # Description: Prints verbose message ("vbm") to stderr if OPTION_VERBOSE is set to "true".
152 # Input:
153 # - OPTION_VERBOSE variable set by processArguments function. (ex: "true", "false")
154 # - "$@" positional arguments fed to this function.
155 # Output: stderr
156 # Script function dependencies: echoerr
157 # External function dependencies: echo
158 # Last modified: 2020-04-11T23:57Z
159 # Last modified by: Steven Baltakatei Sandoval
160 # License: GPLv3+
161 # Ref./Attrib:
162
163 if [ "$OPTION_VERBOSE" = "true" ]; then
164 FUNCTION_TIME=$(date --iso-8601=ns); # Save current time in nano seconds.
165 echoerr "[$FUNCTION_TIME] ""$*"; # Display argument text.
166 fi
167
168 # End function
169 return 0; # Function finished.
170 } # Verbose message display function.
171 processArguments() {
172 while [ ! $# -eq 0 ]; do # While number of arguments ($#) is not (!) equal to (-eq) zero (0).
173 #echoerr "DEBUG:Starting processArguments while loop."
174 #echoerr "DEBUG:Provided arguments are:""$*"
175 case "$1" in
176 --h | --help) showUsage; exit 1;; # Display usage.
177 --version) showVersion; exit 1;; # Show version
178 --v | --verbose) OPTION_VERBOSE="true"; vbm "DEBUG:Verbose mode enabled.";; # Enable verbose mode.
179 --o | --output) if [ -d "$2" ]; then DIROUT="$2"; vbm "DEBUG:DIROUT:$DIROUT"; shift; fi ;; # Define output directory.
180 *) echoerr "ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options.
181 esac
182 shift
183 done
184 } # Argument Processing
185 timeUntilNextDay(){
186 # Desc: Report seconds until next day.
187 # Output: stdout: integer seconds until next day
188 # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0
189 # Usage: timeUntilNextDay
190 # Usage: if ! myTTL="$(timeUntilNextDay)"; then yell "ERROR in if statement"; exit 1; fi
191 local returnState TIME_CURRENT TIME_NEXT_DAY SECONDS_UNTIL_NEXT_DAY
192 TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
193 TIME_NEXT_DAY="$(date -d "$TIME_CURRENT next day" --iso-8601=date)"; # Produce timestamp of beginning of tomorrow with resolution of 1 second.
194 SECONDS_UNTIL_NEXT_DAY="$(( $(date +%s -d "$TIME_NEXT_DAY") - $(date +%s -d "$TIME_CURRENT") ))" ; # Calculate seconds until closest future midnight (res. 1 second).
195 if [[ "$SECONDS_UNTIL_NEXT_DAY" -gt 0 ]]; then
196 returnState="true";
197 elif [[ "$SECONDS_UNTIL_NEXT_DAY" -eq 0 ]]; then
198 returnState="WARNING_ZERO";
199 yell "WARNING:Reported time until next day exactly zero.";
200 elif [[ "$SECONDS_UNTIL_NEXT_DAY" -lt 0 ]]; then
201 returnState="WARNING_NEGATIVE";
202 yell "WARNING:Reported time until next day is negative.";
203 fi
204
205 try echo "$SECONDS_UNTIL_NEXT_DAY"; # Report
206
207 #===Determine function return code===
208 if [[ "$returnState" = "true" ]]; then
209 return 0;
210 elif [[ "$returnState" = "WARNING_ZERO" ]]; then
211 return 1;
212 elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then
213 return 2;
214 fi
215 } # Report seconds until next day
216 timeUntilNextHour(){
217 # Desc: Report seconds until next hour
218 # Output: stdout: integer seconds until next hour
219 # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0
220 # Usage: timeUntilNextHour
221 # Usage: if ! myTTL="$(timeUntilNextHour)"; then yell "ERROR in if statement"; exit 1; fi
222 local returnState TIME_CURRENT TIME_NEXT_HOUR SECONDS_UNTIL_NEXT_HOUR
223 TIME_CURRENT="$(date --iso-8601=seconds)"; # Produce `date`-parsable current timestamp with resolution of 1 second.
224 TIME_NEXT_HOUR="$(date -d "$TIME_CURRENT next hour" --iso-8601=hours)"; # Produce `date`-parsable current time stamp with resolution of 1 second.
225 SECONDS_UNTIL_NEXT_HOUR="$(( $(date +%s -d "$TIME_NEXT_HOUR") - $(date +%s -d "$TIME_CURRENT") ))"; # Calculate seconds until next hour (res. 1 second).
226 if [[ "$SECONDS_UNTIL_NEXT_HOUR" -gt 0 ]]; then
227 returnState="true";
228 elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -eq 0 ]]; then
229 returnState="WARNING_ZERO";
230 yell "WARNING:Reported time until next hour exactly zero.";
231 elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -lt 0 ]]; then
232 returnState="WARNING_NEGATIVE";
233 yell "WARNING:Reported time until next hour is negative.";
234 fi
235
236 try echo "$SECONDS_UNTIL_NEXT_HOUR"; # Report
237
238 #===Determine function return code===
239 if [[ "$returnState" = "true" ]]; then
240 return 0;
241 elif [[ "$returnState" = "WARNING_ZERO" ]]; then
242 return 1;
243 elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then
244 return 2;
245 fi
246 } # Report seconds until next hour
247 dateTimeShort(){
248 # Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz)
249 # Usage: dateTimeShort
250 # Output: stdout: timestamp (ISO-8601, no separators)
251 local TIME_CURRENT TIME_CURRENT_SHORT
252 TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
253 TIME_CURRENT_SHORT="$(date -d "$TIME_CURRENT" +%Y%m%dT%H%M%S%z)"; # Produce separator-less current timestamp with resolution 1 second.
254 echo "$TIME_CURRENT_SHORT";
255 } # Get date&time without separators
256 main() {
257 processArguments "$@" # Process arguments.
258 if checkapp gpspipe && checkdir "$DIROUT"; then
259
260 # # Set script lifespan to end at start of next day
261 # if ! scriptTTL="$(timeUntilNextDay)"; then
262 # if [[ "$scriptTTL" -eq 0 ]]; then
263 # ((scriptTTL++)); # Add 1 because 0 would cause 'timeout' to never timeout.
264 # else
265 # yell "ERROR: timeUntilNextDay exit code $?"; exit 1;
266 # fi;
267 # fi;
268
269 # Set script lifespan to end at start of next hour
270 if ! scriptTTL="$(timeUntilNextHour)"; then
271 if [[ "$scriptTTL" -eq 0 ]]; then
272 ((scriptTTL++)); # Add 1 because 0 would cause 'timeout' to never timeout.
273 else
274 yell "ERROR: timeUntilNextHour exit code $?"; exit 1;
275 fi;
276 fi;
277
278 # Determine buffer lifespan
279 bufferTTL="60";
280
281 # Record gps data until script lifespan ends
282 declare debugCounter; debugCounter="0"
283 while [[ "$SECONDS" -lt "$scriptTTL" ]]; do
284 ((debugCounter++))
285 # Determine output file paths (time is start of buffer period)
286 FILEOUT_BASENAME="$(dateTimeShort)""--P""$bufferTTL""S..""$SCRIPT_HOSTNAME""_location" ; # ISO-8601 YYYYmmddTHHMMSS+zzP[$bufferTTL]S
287 FILEOUT_NMEA="$FILEOUT_BASENAME".nmea ;
288 FILEOUT_GPX="$FILEOUT_BASENAME".gpx ;
289 FILEOUT_KML="$FILEOUT_BASENAME".kml ;
290 # Fill buffer
291 buffer="$(timeout "$bufferTTL""s" gpspipe -r)"; # Record gpspipe nmea data to buffer for bufferTTL seconds
292 # Process and save buffers
293 echo "$buffer" > "$DIROUT"/"$FILEOUT_NMEA" & # Save NMEA format
294 echo "$buffer" | gpsbabel -i nmea -f - -o gpx -F - > "$DIROUT"/"$FILEOUT_GPX" & # Save GPX format
295 echo "$buffer" | gpsbabel -i nmea -f - -o kml -F - > "$DIROUT"/"$FILEOUT_KML" & # Save KML format
296 vbm "DEBUG:Completed buffer session $debugCounter ." 1>&2;
297 # Reset buffer and filenames
298 unset buffer FILEOUT_BASENAME FILEOUT_NMEA FILEOUT_GPX FILEOUT_KML;
299 done
300 fi
301 } # Main function.
302 #===END Declare local script functions===
303 #==END Define script parameters==
304
305
306 #==BEGIN Perform work and exit==
307 main "$@" # Run main function.
308 exit 0;
309 #==END Perform work and exit==
310
311
312
313 #gpspipe -r -d -l -o "$DIROUT1"/"$FILEOUT1" ;