fix(bkgpslog):Remove error in how output dir is specified
[EVA-2020-02.git] / exec / bkgpslog
CommitLineData
032f4b05
SBS
1#!/bin/bash
2
8fbca23d
SBS
3# Desc: Records gps data until midnight
4# Author: Steven Baltakatei Sandoval; License: GPLv3+
5# Usage: bkgpslog --output [output dir]
032f4b05 6
8fbca23d 7#==BEGIN Define script parameters==
032f4b05
SBS
8PATH="/opt/bktei:$PATH" # Add default baltakatei script install directory to PATH (necessary for other bk scripts)
9SCRIPT_HOSTNAME=$(hostname) # Save hostname of system running this script.
10SCRIPT_VERSION="bkgpslog 0.0.1" # Define version of script.
11SCRIPT_TIME_SHORT="$(date +%Y%m%dT%H%M%S%z)" # Save current date & time in ISO-8601 format.
12SCRIPT_DATE_SHORT="$(date +%Y%m%d)" # Save current date in ISO-8601 format.
8fbca23d
SBS
13
14declare -Ag appRollCall # Associative array for storing app status
15declare -Ag fileRollCall # Associative array for storing file status
16declare -Ag dirRollCall # Associative array for storing dir status
17
18#===BEGIN Declare local script functions===
19checkapp() {
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
53checkfile() {
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
85checkdir() {
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 else
101 dirRollCall["$arg"]="false"; returnState="false";
102 fi
103 done
104
105 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:dirRollCall key [$key] is:${dirRollCall[$key]}"; done
106 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
107
108 #===Determine function return code===
109 if [ "$returnState" = "true" ]; then
110 #echo "DEBUG:checkapp returns true for $arg";
111 return 0;
112 else
113 #echo "DEBUG:checkapp returns false for $arg";
114 return 1;
115 fi
116} # Check that dir exists
032f4b05 117
c609b9c8
SBS
118# Yell, Die, Try Three-Fingered Claw technique
119# Ref/Attrib: https://stackoverflow.com/a/25515370
120yell() { echo "$0: $*" >&2; }
121die() { yell "$*"; exit 111; }
122try() { "$@" || die "cannot $*"; }
123
032f4b05
SBS
124echoerr() {
125 echo "$@" 1>&2; # Define stderr echo function.
126} # Define stderr message function.
127showUsage() {
128 echoerr "USAGE:"
129 echoerr " bkgpslog.sh [ options ]"
130 echoerr
131 echoerr "OPTIONS:"
132 echoerr " -h, --help"
133 echoerr " Display help information."
134 echoerr
135 echoerr " --version"
136 echoerr " Display script version."
137 echoerr
138 echoerr " -v, --verbose"
139 echoerr " Display debugging info."
140 echoerr
141 echoerr " -o, --output [ directory ]"
142 echoerr " Specify output directory to save logs."
143} # Display information on how to use this script.
144showVersion() {
145 echoerr "$SCRIPT_VERSION"
146} # Display script version.
147vbm() {
148 if [ $OPTION_VERBOSE -eq "true" ]; then
149 echoerr "$@"
150 fi
151} # Verbose message display function.
152processArguments() {
153 while [ ! $# -eq 0 ]; do # While number of arguments ($#) is not (!) equal to (-eq) zero (0).
154 echoerr "DEBUG:Starting processArguments while loop."
155 echoerr "DEBUG:Provided arguments are:""$@"
156 case "$1" in
157 --h | --help) showUsage; exit 1;; # Display usage.
158 --version) showVersion; exit 1;; # Show version
159 --v | --verbose) OPTION_VERBOSE="true"; vbm "DEBUG:Verbose mode enabled.";; # Enable verbose mode.
8fbca23d 160 --o | --output) if [ -d "$2" ]; then DIROUT="$2"; fi ;; # Define output directory.
032f4b05
SBS
161 *) echoerr "ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options.
162 esac
163 shift
164 done
165} # Argument Processing
8fbca23d
SBS
166timeUntilMidnight(){
167 # Desc: Report seconds until midnight
168 # Output: stdout: integer seconds until midnight
169 # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0
170 # Usage: timeUntilMidnight
171 # Usage: if ! myTTL="$(timeUntilMidnight)"; then yell "ERROR in if statement"; exit 1; fi
172 local returnState
173 TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
174 TIME_CURRENT_SHORT="$(date -d "$TIME_CURRENT" +%Y%m%dT%H%M%S%z)" # Produce separator-less current timestamp with resolution 1 second.
175 DATE_CURRENT="$(date -d "$TIME_CURRENT" --iso-8601=date)" ; # Produce `date`-parsable current timestamp with resolution of 1 day.
176 DATE_CURRENT_SHORT="$(date -d "$TIME_CURRENT" +%Y%m%d)" ; # Produce separator-less current timestamp with resolution 1 day.
177 DATE_TOMORROW="$(date -d "$TIME_CURRENT next day" --iso-8601=date)" ; # Produce timestamp of tomorrow's date (res. 1 day).
178 TIME_NEXT_MIDNIGHT="$(date -d "$DATE_TOMORROW" --iso-8601=seconds)" ; # Produce `date`-parsable timestamp of closest future midnight (res. 1 second).
179 SECONDS_UNTIL_NEXT_MIDNIGHT="$(( $(date +%s -d "$TIME_NEXT_MIDNIGHT") - $(date +%s -d "$TIME_CURRENT") ))" ; # Calculate seconds until closest future midnight (res. 1 second).
180 if [[ "$SECONDS_UNTIL_NEXT_MIDNIGHT" -gt 0 ]]; then
181 returnState="true";
182 elif [[ "$SECONDS_UNTIL_NEXT_MIDNIGHT" -eq 0 ]]; then
183 returnState="WARNING_ZERO";
184 yell "WARNING:Reported time until midnight exactly zero.";
185 elif [[ "$SECONDS_UNTIL_NEXT_MIDNIGHT" -lt 0 ]]; then
186 returnState="WARNING_NEGATIVE";
187 yell "WARNING:Reported time until midnight is negative.";
188 fi
032f4b05 189
8fbca23d
SBS
190 try echo "$SECONDS_UNTIL_NEXT_MIDNIGHT"; # Report
191
192 #===Determine function return code===
193 if [[ "$returnState" = "true" ]]; then
194 return 0;
195 elif [[ "$returnState" = "WARNING_ZERO" ]]; then
196 return 1;
197 elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then
198 return 2;
199 fi
200} # Report seconds until next midnight
201dateTimeShort(){
202 # Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz)
203 # Usage: dateTimeShort
204 # Output: stdout: timestamp (ISO-8601, no separators)
205 TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
206 TIME_CURRENT_SHORT="$(date -d "$TIME_CURRENT" +%Y%m%dT%H%M%S%z)"; # Produce separator-less current timestamp with resolution 1 second.
207 echo "$TIME_CURRENT_SHORT";
208} # Get date&time without separators
032f4b05
SBS
209main() {
210 processArguments # Process arguments.
8fbca23d
SBS
211 if checkapp gpspipe && checkdir "$DIROUT"; then
212 # Determine output file paths
213 FILEOUT_NMEA="$(dateTimeShort)".."$SCRIPT_HOSTNAME"_location.nmea ;
214 #FILEOUT_GPX="" ;
215 #FILEOUT_KML="" ;
032f4b05 216
8fbca23d
SBS
217 # Determine script lifespan (note: exit if <= 0 since 'timeout' runs forever if provided "0s".
218 #if ! scriptTTL="$(timeUntilMidnight)"; then yell "ERROR: timeUntilMidnight exit code $?"; exit 1; fi
219 scriptTTL="60"; #DEBUG DEBUG DEBUG DEBUG
220
221 # Determine buffer lifespan
222 bufferTTL="15";
032f4b05 223
8fbca23d
SBS
224 # Record gps data until life ends
225 declare debugCounter; debugCounter="0"
226 while [[ "$SECONDS" -lt "$scriptTTL" ]]; do
227 ((debugCounter++))
228 timeout "$bufferTTL""s" gpspipe -r 1>> "$DIROUT"/"$FILEOUT_NMEA""$debugCounter"
229 echo "do stuff" 1>&2;
230 done
231 fi
232} # Main function.
233#===END Declare local script functions===
234#==END Define script parameters==
032f4b05
SBS
235
236
8fbca23d
SBS
237#==BEGIN Perform work and exit==
238main "$@" # Run main function.
239exit 0;
240#==END Perform work and exit==
032f4b05
SBS
241
242
032f4b05
SBS
243
244 #gpspipe -r -d -l -o "$DIROUT1"/"$FILEOUT1" ;