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.
13 DIROUT
="$1" # Define output directory.
15 declare -Ag appRollCall
# Associative array for storing app status
16 declare -Ag fileRollCall
# Associative array for storing file status
17 declare -Ag dirRollCall
# Associative array for storing dir status
19 #===BEGIN Declare local script functions===
21 # Desc: If arg is a command, save result in assoc array 'appRollCall'
22 # Usage: checkapp arg1 arg2 arg3 ...
23 # Input: global assoc. array 'appRollCall'
24 # Output: adds/updates key(value) to global assoc array 'appRollCall'
26 #echo "DEBUG:$(date +%S.%N)..Starting checkapp function."
27 #echo "DEBUG:args: $@"
28 #echo "DEBUG:returnState:$returnState"
32 #echo "DEBUG:processing arg:$arg"
33 if command -v $arg 1>/dev
/null
2>&1; then # Check if arg is a valid command
34 appRollCall
[$arg]="true";
35 #echo "DEBUG:appRollCall[$arg]:"${appRollCall[$arg]}
36 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi
38 appRollCall
[$arg]="false"; returnState
="false";
42 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
43 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
45 #===Determine function return code===
46 if [ "$returnState" = "true" ]; then
47 #echo "DEBUG:checkapp returns true for $arg";
50 #echo "DEBUG:checkapp returns false for $arg";
53 } # Check that app exists
55 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
56 # Usage: checkfile arg1 arg2 arg3 ...
57 # Input: global assoc. array 'fileRollCall'
58 # Output: adds/updates key(value) to global assoc array 'fileRollCall';
59 # Output: returns 0 if app found, 1 otherwise
64 #echo "DEBUG:processing arg:$arg"
65 if [ -f "$arg" ]; then
66 fileRollCall
["$arg"]="true";
67 #echo "DEBUG:fileRollCall[\"$arg\"]:"${fileRollCall["$arg"]}
68 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi
70 fileRollCall
["$arg"]="false"; returnState
="false";
74 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:fileRollCall key [$key] is:${fileRollCall[$key]}"; done
75 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
77 #===Determine function return code===
78 if [ "$returnState" = "true" ]; then
79 #echo "DEBUG:checkapp returns true for $arg";
82 #echo "DEBUG:checkapp returns false for $arg";
85 } # Check that file exists
87 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
88 # Usage: checkdir arg1 arg2 arg3 ...
89 # Input: global assoc. array 'dirRollCall'
90 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
91 # Output: returns 0 if app found, 1 otherwise
96 #echo "DEBUG:processing arg:$arg"
97 if [ -d "$arg" ]; then
98 dirRollCall
["$arg"]="true";
99 #echo "DEBUG:dirRollCall[\"$arg\"]:"${dirRollCall["$arg"]}
100 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi
102 dirRollCall
["$arg"]="false"; returnState
="false";
106 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:dirRollCall key [$key] is:${dirRollCall[$key]}"; done
107 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
109 #===Determine function return code===
110 if [ "$returnState" = "true" ]; then
111 #echo "DEBUG:checkapp returns true for $arg";
114 #echo "DEBUG:checkapp returns false for $arg";
117 } # Check that dir exists
119 # Yell, Die, Try Three-Fingered Claw technique
120 # Ref/Attrib: https://stackoverflow.com/a/25515370
121 yell
() { echo "$0: $*" >&2; }
122 die
() { yell
"$*"; exit 111; }
123 try
() { "$@" || die
"cannot $*"; }
126 echo "$@" 1>&2; # Define stderr echo function.
127 } # Define stderr message function.
130 echoerr
" bkgpslog.sh [ options ]"
133 echoerr
" -h, --help"
134 echoerr
" Display help information."
137 echoerr
" Display script version."
139 echoerr
" -v, --verbose"
140 echoerr
" Display debugging info."
142 echoerr
" -o, --output [ directory ]"
143 echoerr
" Specify output directory to save logs."
144 } # Display information on how to use this script.
146 echoerr
"$SCRIPT_VERSION"
147 } # Display script version.
149 if [ $OPTION_VERBOSE -eq "true" ]; then
152 } # Verbose message display function.
154 while [ ! $# -eq 0 ]; do # While number of arguments ($#) is not (!) equal to (-eq) zero (0).
155 echoerr
"DEBUG:Starting processArguments while loop."
156 echoerr
"DEBUG:Provided arguments are:""$@"
158 --h |
--help) showUsage
; exit 1;; # Display usage.
159 --version) showVersion
; exit 1;; # Show version
160 --v |
--verbose) OPTION_VERBOSE
="true"; vbm
"DEBUG:Verbose mode enabled.";; # Enable verbose mode.
161 --o |
--output) if [ -d "$2" ]; then DIROUT
="$2"; fi ;; # Define output directory.
162 *) echoerr
"ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options.
166 } # Argument Processing
168 # Desc: Report seconds until midnight
169 # Output: stdout: integer seconds until midnight
170 # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0
171 # Usage: timeUntilMidnight
172 # Usage: if ! myTTL="$(timeUntilMidnight)"; then yell "ERROR in if statement"; exit 1; fi
174 TIME_CURRENT
="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
175 TIME_CURRENT_SHORT
="$(date -d "$TIME_CURRENT" +%Y%m%dT%H%M%S%z)" # Produce separator-less current timestamp with resolution 1 second.
176 DATE_CURRENT
="$(date -d "$TIME_CURRENT" --iso-8601=date)" ; # Produce `date`-parsable current timestamp with resolution of 1 day.
177 DATE_CURRENT_SHORT
="$(date -d "$TIME_CURRENT" +%Y%m%d)" ; # Produce separator-less current timestamp with resolution 1 day.
178 DATE_TOMORROW
="$(date -d "$TIME_CURRENT next day
" --iso-8601=date)" ; # Produce timestamp of tomorrow's date (res. 1 day).
179 TIME_NEXT_MIDNIGHT
="$(date -d "$DATE_TOMORROW" --iso-8601=seconds)" ; # Produce `date`-parsable timestamp of closest future midnight (res. 1 second).
180 SECONDS_UNTIL_NEXT_MIDNIGHT
="$(( $(date +%s -d "$TIME_NEXT_MIDNIGHT") - $(date +%s -d "$TIME_CURRENT") ))" ; # Calculate seconds until closest future midnight (res. 1 second).
181 if [[ "$SECONDS_UNTIL_NEXT_MIDNIGHT" -gt 0 ]]; then
183 elif [[ "$SECONDS_UNTIL_NEXT_MIDNIGHT" -eq 0 ]]; then
184 returnState
="WARNING_ZERO";
185 yell
"WARNING:Reported time until midnight exactly zero.";
186 elif [[ "$SECONDS_UNTIL_NEXT_MIDNIGHT" -lt 0 ]]; then
187 returnState
="WARNING_NEGATIVE";
188 yell
"WARNING:Reported time until midnight is negative.";
191 try
echo "$SECONDS_UNTIL_NEXT_MIDNIGHT"; # Report
193 #===Determine function return code===
194 if [[ "$returnState" = "true" ]]; then
196 elif [[ "$returnState" = "WARNING_ZERO" ]]; then
198 elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then
201 } # Report seconds until next midnight
203 # Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz)
204 # Usage: dateTimeShort
205 # Output: stdout: timestamp (ISO-8601, no separators)
206 TIME_CURRENT
="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
207 TIME_CURRENT_SHORT
="$(date -d "$TIME_CURRENT" +%Y%m%dT%H%M%S%z)"; # Produce separator-less current timestamp with resolution 1 second.
208 echo "$TIME_CURRENT_SHORT";
209 } # Get date&time without separators
211 processArguments
# Process arguments.
212 if checkapp gpspipe
&& checkdir
"$DIROUT"; then
213 # Determine output file paths
214 FILEOUT_NMEA
="$(dateTimeShort)"..
"$SCRIPT_HOSTNAME"_location.nmea
;
218 # Determine script lifespan (note: exit if <= 0 since 'timeout' runs forever if provided "0s".
219 #if ! scriptTTL="$(timeUntilMidnight)"; then yell "ERROR: timeUntilMidnight exit code $?"; exit 1; fi
220 scriptTTL
="60"; #DEBUG DEBUG DEBUG DEBUG
222 # Determine buffer lifespan
225 # Record gps data until life ends
226 declare debugCounter
; debugCounter
="0"
227 while [[ "$SECONDS" -lt "$scriptTTL" ]]; do
229 timeout
"$bufferTTL""s" gpspipe
-r 1>> "$DIROUT"/"$FILEOUT_NMEA""$debugCounter"
230 echo "do stuff" 1>&2;
234 #===END Declare local script functions===
235 #==END Define script parameters==
238 #==BEGIN Perform work and exit==
239 main
"$@" # Run main function.
241 #==END Perform work and exit==
245 #gpspipe -r -d -l -o "$DIROUT1"/"$FILEOUT1" ;