feat(exec):Add initial draft of pressure logging script
[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
13DIROUT="$1" # Define output directory.
14
15declare -Ag appRollCall # Associative array for storing app status
16declare -Ag fileRollCall # Associative array for storing file status
17declare -Ag dirRollCall # Associative array for storing dir status
18
19#===BEGIN Declare local script functions===
20checkapp() {
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'
25 local returnState
26 #echo "DEBUG:$(date +%S.%N)..Starting checkapp function."
27 #echo "DEBUG:args: $@"
28 #echo "DEBUG:returnState:$returnState"
29
30 #===Process Args===
31 for arg in "$@"; do
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
37 else
38 appRollCall[$arg]="false"; returnState="false";
39 fi
40 done
41
42 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
43 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
44
45 #===Determine function return code===
46 if [ "$returnState" = "true" ]; then
47 #echo "DEBUG:checkapp returns true for $arg";
48 return 0;
49 else
50 #echo "DEBUG:checkapp returns false for $arg";
51 return 1;
52 fi
53} # Check that app exists
54checkfile() {
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
60 local returnState
61
62 #===Process Args===
63 for arg in "$@"; do
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
69 else
70 fileRollCall["$arg"]="false"; returnState="false";
71 fi
72 done
73
74 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:fileRollCall key [$key] is:${fileRollCall[$key]}"; done
75 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
76
77 #===Determine function return code===
78 if [ "$returnState" = "true" ]; then
79 #echo "DEBUG:checkapp returns true for $arg";
80 return 0;
81 else
82 #echo "DEBUG:checkapp returns false for $arg";
83 return 1;
84 fi
85} # Check that file exists
86checkdir() {
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
92 local returnState
93
94 #===Process Args===
95 for arg in "$@"; do
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
101 else
102 dirRollCall["$arg"]="false"; returnState="false";
103 fi
104 done
105
106 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:dirRollCall key [$key] is:${dirRollCall[$key]}"; done
107 #echo "DEBUG:evaluating returnstate. returnState:"$returnState
108
109 #===Determine function return code===
110 if [ "$returnState" = "true" ]; then
111 #echo "DEBUG:checkapp returns true for $arg";
112 return 0;
113 else
114 #echo "DEBUG:checkapp returns false for $arg";
115 return 1;
116 fi
117} # Check that dir exists
032f4b05 118
c609b9c8
SBS
119# Yell, Die, Try Three-Fingered Claw technique
120# Ref/Attrib: https://stackoverflow.com/a/25515370
121yell() { echo "$0: $*" >&2; }
122die() { yell "$*"; exit 111; }
123try() { "$@" || die "cannot $*"; }
124
032f4b05
SBS
125echoerr() {
126 echo "$@" 1>&2; # Define stderr echo function.
127} # Define stderr message function.
128showUsage() {
129 echoerr "USAGE:"
130 echoerr " bkgpslog.sh [ options ]"
131 echoerr
132 echoerr "OPTIONS:"
133 echoerr " -h, --help"
134 echoerr " Display help information."
135 echoerr
136 echoerr " --version"
137 echoerr " Display script version."
138 echoerr
139 echoerr " -v, --verbose"
140 echoerr " Display debugging info."
141 echoerr
142 echoerr " -o, --output [ directory ]"
143 echoerr " Specify output directory to save logs."
144} # Display information on how to use this script.
145showVersion() {
146 echoerr "$SCRIPT_VERSION"
147} # Display script version.
148vbm() {
149 if [ $OPTION_VERBOSE -eq "true" ]; then
150 echoerr "$@"
151 fi
152} # Verbose message display function.
153processArguments() {
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:""$@"
157 case "$1" in
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.
8fbca23d 161 --o | --output) if [ -d "$2" ]; then DIROUT="$2"; fi ;; # Define output directory.
032f4b05
SBS
162 *) echoerr "ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options.
163 esac
164 shift
165 done
166} # Argument Processing
8fbca23d
SBS
167timeUntilMidnight(){
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
173 local returnState
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
182 returnState="true";
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.";
189 fi
032f4b05 190
8fbca23d
SBS
191 try echo "$SECONDS_UNTIL_NEXT_MIDNIGHT"; # Report
192
193 #===Determine function return code===
194 if [[ "$returnState" = "true" ]]; then
195 return 0;
196 elif [[ "$returnState" = "WARNING_ZERO" ]]; then
197 return 1;
198 elif [[ "$returnState" = "WARNING_NEGATIVE" ]]; then
199 return 2;
200 fi
201} # Report seconds until next midnight
202dateTimeShort(){
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
032f4b05
SBS
210main() {
211 processArguments # Process arguments.
8fbca23d
SBS
212 if checkapp gpspipe && checkdir "$DIROUT"; then
213 # Determine output file paths
214 FILEOUT_NMEA="$(dateTimeShort)".."$SCRIPT_HOSTNAME"_location.nmea ;
215 #FILEOUT_GPX="" ;
216 #FILEOUT_KML="" ;
032f4b05 217
8fbca23d
SBS
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
221
222 # Determine buffer lifespan
223 bufferTTL="15";
032f4b05 224
8fbca23d
SBS
225 # Record gps data until life ends
226 declare debugCounter; debugCounter="0"
227 while [[ "$SECONDS" -lt "$scriptTTL" ]]; do
228 ((debugCounter++))
229 timeout "$bufferTTL""s" gpspipe -r 1>> "$DIROUT"/"$FILEOUT_NMEA""$debugCounter"
230 echo "do stuff" 1>&2;
231 done
232 fi
233} # Main function.
234#===END Declare local script functions===
235#==END Define script parameters==
032f4b05
SBS
236
237
8fbca23d
SBS
238#==BEGIN Perform work and exit==
239main "$@" # Run main function.
240exit 0;
241#==END Perform work and exit==
032f4b05
SBS
242
243
032f4b05
SBS
244
245 #gpspipe -r -d -l -o "$DIROUT1"/"$FILEOUT1" ;