feat(bkgpslog):Add yell, die, try functions
[EVA-2020-02.git] / exec / bkgpslog
CommitLineData
032f4b05
SBS
1#!/bin/bash
2
032f4b05
SBS
3# Author: Steven Baltakatei Sandoval
4
5#== Variable Initialization ==
6
7#== Global constants ==
8PATH="/usr/local/bin/:$PATH" # Add default OpenTimestamps path to PATH (necessary if cron used to call this script)
9PATH="/opt/bktei:$PATH" # Add default baltakatei script install directory to PATH (necessary for other bk scripts)
10SCRIPT_HOSTNAME=$(hostname) # Save hostname of system running this script.
11SCRIPT_VERSION="bkgpslog 0.0.1" # Define version of script.
12SCRIPT_TIME_SHORT="$(date +%Y%m%dT%H%M%S%z)" # Save current date & time in ISO-8601 format.
13SCRIPT_DATE_SHORT="$(date +%Y%m%d)" # Save current date in ISO-8601 format.
14#DIROUT1="/home/pi/Sync/Evanescent_Location" # Define output directory.
15
c609b9c8
SBS
16#== Script Function Definitions ==
17# Yell, Die, Try Three-Fingered Claw technique
18# Ref/Attrib: https://stackoverflow.com/a/25515370
19yell() { echo "$0: $*" >&2; }
20die() { yell "$*"; exit 111; }
21try() { "$@" || die "cannot $*"; }
22
032f4b05
SBS
23echoerr() {
24 echo "$@" 1>&2; # Define stderr echo function.
25} # Define stderr message function.
26showUsage() {
27 echoerr "USAGE:"
28 echoerr " bkgpslog.sh [ options ]"
29 echoerr
30 echoerr "OPTIONS:"
31 echoerr " -h, --help"
32 echoerr " Display help information."
33 echoerr
34 echoerr " --version"
35 echoerr " Display script version."
36 echoerr
37 echoerr " -v, --verbose"
38 echoerr " Display debugging info."
39 echoerr
40 echoerr " -o, --output [ directory ]"
41 echoerr " Specify output directory to save logs."
42} # Display information on how to use this script.
43showVersion() {
44 echoerr "$SCRIPT_VERSION"
45} # Display script version.
46vbm() {
47 if [ $OPTION_VERBOSE -eq "true" ]; then
48 echoerr "$@"
49 fi
50} # Verbose message display function.
51processArguments() {
52 while [ ! $# -eq 0 ]; do # While number of arguments ($#) is not (!) equal to (-eq) zero (0).
53 echoerr "DEBUG:Starting processArguments while loop."
54 echoerr "DEBUG:Provided arguments are:""$@"
55 case "$1" in
56 --h | --help) showUsage; exit 1;; # Display usage.
57 --version) showVersion; exit 1;; # Show version
58 --v | --verbose) OPTION_VERBOSE="true"; vbm "DEBUG:Verbose mode enabled.";; # Enable verbose mode.
59 --o | --output) if [ -d "$2" ]; then DIROUT1="$2"; fi ;; # Define output directory.
60 *) echoerr "ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options.
61 esac
62 shift
63 done
64} # Argument Processing
65checkExecutables() {
66
67} # Check that certain executables exist.
68
69main() {
70 processArguments # Process arguments.
71 checkExecutables "gpspipe" # Confirm that executables necessary to run are available.
72} # Main function.
73
74
75
76
77
78
79if [[ -d "$DIROUT1" ]] && command -v "gpspipe" 1>/dev/null 2>/dev/null ; then
80 while true; do
81 #echoerr "DEBUG:Starting while loop."
82 # Update time constants
83 TIME_CURRENT="$(date --iso-8601=seconds)" ;
84 #echoerr "DEBUG:TIME_CURRENT is:""$TIME_CURRENT" ;
85 TIME_CURRENT_SHORT="$(date -d "$TIME_CURRENT" +%Y%m%dT%H%M%S%z)"
86 #echoerr "DEBUG:TIME_CURRENT_SHORT is""$TIME_CURRENT_SHORT"
87 DATE_CURRENT="$(date -d "$TIME_CURRENT" --iso-8601=date)" ;
88 #echoerr "DEBUG:DATE_CURRENT is:""$DATE_CURRENT" ;
89 DATE_CURRENT_SHORT="$(date -d "$TIME_CURRENT" +%Y%m%d)" ;
90 #echoerr "DEBUG:DATE_CURRENT is:""$DATE_CURRENT_SHORT" ;
91 DATE_TOMORROW="$(date -d "$TIME_CURRENT next day" --iso-8601=date)" ;
92 #echoerr "DEBUG:DATE_TOMORROW is:""$DATE_TOMORROW" ;
93 TIME_NEXT_MIDNIGHT="$(date -d "$DATE_TOMORROW" --iso-8601=seconds)" ;
94 #echoerr "DEBUG:TIME_NEXT_MIDNIGHT is:""$TIME_NEXT_MIDNIGHT" ;
95 SECONDS_UNTIL_NEXT_MIDNIGHT="$(( $(date +%s -d "$TIME_NEXT_MIDNIGHT") - $(date +%s -d "$TIME_CURRENT") ))" ;
96 #echoerr "DEBUG:SECONDS_UNTIL_NEXT_MIDNIGHT is:""$SECONDS_UNTIL_NEXT_MIDNIGHT" ;
97 if [ "$SECONDS_UNTIL_NEXT_MIDNIGHT" -eq 0 ]; then echoerr "WARNING:Is it exactly midnight?" ; continue; fi ; # Exit loop early if equal to 0 because "timeout 0s" never ends.
98 if [ "$SECONDS_UNTIL_NEXT_MIDNIGHT" -lt 0 ]; then echoerr "ERROR:Time until midnight error." ; exit 1; fi ; # Exit script if equal to 0 because "timeout 0s" never ends.
99
100 # Update output file names.
101 DIROUTN="$DIROUT1" ;
102 FILEOUTN="$DATE_CURRENT_SHORT""..""$(hostname)""_location.nmea" ;
103
104 # Append location log until midnight.
105 timeout "$SECONDS_UNTIL_NEXT_MIDNIGHT""s" gpspipe -r 1>> "$DIROUTN"/"$FILEOUTN" 2>>/dev/null ; # log raw nmea data
106 done
107
108 #gpspipe -r -d -l -o "$DIROUT1"/"$FILEOUT1" ;
109
110else
111 echo "$SCRIPT_TIME_SHORT"">gpspipe or $DIROUT1 not found." >> ~/"$SCRIPT_DATE_SHORT"..error.log ;
112fi