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
18 #===BEGIN Declare local script functions===
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'
25 #echo "DEBUG:$(date +%S.%N)..Starting checkapp function."
26 #echo "DEBUG:args: $@"
27 #echo "DEBUG:returnState:$returnState"
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
37 appRollCall
[$arg]="false"; returnState
="false";
41 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
42 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
44 #===Determine function return code===
45 if [ "$returnState" = "true" ]; then
46 #echo "DEBUG:checkapp returns true for $arg";
49 #echo "DEBUG:checkapp returns false for $arg";
52 } # Check that app exists
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
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
69 fileRollCall
["$arg"]="false"; returnState
="false";
73 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:fileRollCall key [$key] is:${fileRollCall[$key]}"; done
74 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
76 #===Determine function return code===
77 if [ "$returnState" = "true" ]; then
78 #echo "DEBUG:checkapp returns true for $arg";
81 #echo "DEBUG:checkapp returns false for $arg";
84 } # Check that file exists
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
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";
107 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:dirRollCall key [$key] is:${dirRollCall[$key]}"; done
108 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
110 #===Determine function return code===
111 if [ "$returnState" = "true" ]; then
112 #echo "DEBUG:checkapp returns true for $arg";
115 #echo "DEBUG:checkapp returns false for $arg";
118 } # Check that dir exists
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 $*"; }
127 echo "$@" 1>&2; # Define stderr echo function.
128 } # Define stderr message function.
131 echoerr
" bkgpslog.sh [ options ]"
134 echoerr
" -h, --help"
135 echoerr
" Display help information."
138 echoerr
" Display script version."
140 echoerr
" -v, --verbose"
141 echoerr
" Display debugging info."
143 echoerr
" -o, --output [ directory ]"
144 echoerr
" Specify output directory to save logs."
145 } # Display information on how to use this script.
147 echoerr
"$SCRIPT_VERSION"
148 } # Display script version.
150 # Usage: vbm "DEBUG:verbose message here"
151 # Description: Prints verbose message ("vbm") to stderr if OPTION_VERBOSE is set to "true".
153 # - OPTION_VERBOSE variable set by processArguments function. (ex: "true", "false")
154 # - "$@" positional arguments fed to this function.
156 # Script function dependencies: echoerr
157 # External function dependencies: echo
158 # Last modified: 2020-04-11T23:57Z
159 # Last modified by: Steven Baltakatei Sandoval
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.
169 return 0; # Function finished.
170 } # Verbose message display function.
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:""$*"
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.
184 } # Argument Processing
186 # Desc: Report seconds until midnight
187 # Output: stdout: integer seconds until midnight
188 # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0
189 # Usage: timeUntilMidnight
190 # Usage: if ! myTTL="$(timeUntilMidnight)"; then yell "ERROR in if statement"; exit 1; fi
192 TIME_CURRENT
="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
193 TIME_CURRENT_SHORT
="$(date -d "$TIME_CURRENT" +%Y%m%dT%H%M%S%z)" # Produce separator-less current timestamp with resolution 1 second.
194 DATE_CURRENT
="$(date -d "$TIME_CURRENT" --iso-8601=date)" ; # Produce `date`-parsable current timestamp with resolution of 1 day.
195 DATE_CURRENT_SHORT
="$(date -d "$TIME_CURRENT" +%Y%m%d)" ; # Produce separator-less current timestamp with resolution 1 day.
196 DATE_TOMORROW
="$(date -d "$TIME_CURRENT next day
" --iso-8601=date)" ; # Produce timestamp of tomorrow's date (res. 1 day).
197 TIME_NEXT_MIDNIGHT
="$(date -d "$DATE_TOMORROW" --iso-8601=seconds)" ; # Produce `date`-parsable timestamp of closest future midnight (res. 1 second).
198 SECONDS_UNTIL_NEXT_MIDNIGHT
="$(( $(date +%s -d "$TIME_NEXT_MIDNIGHT") - $(date +%s -d "$TIME_CURRENT") ))" ; # Calculate seconds until closest future midnight (res. 1 second).
199 if [[ "$SECONDS_UNTIL_NEXT_MIDNIGHT" -gt 0 ]]; then
201 elif [[ "$SECONDS_UNTIL_NEXT_MIDNIGHT" -eq 0 ]]; then
202 returnState
="WARNING_ZERO";
203 yell
"WARNING:Reported time until midnight exactly zero.";
204 elif [[ "$SECONDS_UNTIL_NEXT_MIDNIGHT" -lt 0 ]]; then
205 returnState
="WARNING_NEGATIVE";
206 yell
"WARNING:Reported time until midnight is negative.";
209 try
echo "$SECONDS_UNTIL_NEXT_MIDNIGHT"; # Report
211 #===Determine function return code===
212 if [[ "$returnState" = "true" ]]; then
214 elif [[ "$returnState" = "WARNING_ZERO" ]]; then
216 elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then
219 } # Report seconds until next midnight
221 # Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz)
222 # Usage: dateTimeShort
223 # Output: stdout: timestamp (ISO-8601, no separators)
224 TIME_CURRENT
="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
225 TIME_CURRENT_SHORT
="$(date -d "$TIME_CURRENT" +%Y%m%dT%H%M%S%z)"; # Produce separator-less current timestamp with resolution 1 second.
226 echo "$TIME_CURRENT_SHORT";
227 } # Get date&time without separators
229 processArguments
"$@" # Process arguments.
230 if checkapp gpspipe
&& checkdir
"$DIROUT"; then
231 # Determine output file paths
232 FILEOUT_NMEA
="$(dateTimeShort)"..
"$SCRIPT_HOSTNAME"_location.nmea
;
236 # Determine script lifespan (note: exit if <= 0 since 'timeout' runs forever if provided "0s".
237 #if ! scriptTTL="$(timeUntilMidnight)"; then yell "ERROR: timeUntilMidnight exit code $?"; exit 1; fi
238 scriptTTL
="60"; #DEBUG DEBUG DEBUG DEBUG
240 # Determine buffer lifespan
243 # Record gps data until life ends
244 declare debugCounter
; debugCounter
="0"
245 while [[ "$SECONDS" -lt "$scriptTTL" ]]; do
247 timeout
"$bufferTTL""s" gpspipe
-r 1>> "$DIROUT"/"$FILEOUT_NMEA""$debugCounter"
248 vbm
"DEBUG:Completed buffer session." 1>&2;
252 #===END Declare local script functions===
253 #==END Define script parameters==
256 #==BEGIN Perform work and exit==
257 main
"$@" # Run main function.
259 #==END Perform work and exit==
263 #gpspipe -r -d -l -o "$DIROUT1"/"$FILEOUT1" ;