3 # Author: Steven Baltakatei Sandoval
5 #== Variable Initialization ==
7 #== Global constants ==
8 PATH
="/usr/local/bin/:$PATH" # Add default OpenTimestamps path to PATH (necessary if cron used to call this script)
9 PATH
="/opt/bktei:$PATH" # Add default baltakatei script install directory to PATH (necessary for other bk scripts)
10 SCRIPT_HOSTNAME
=$
(hostname
) # Save hostname of system running this script.
11 SCRIPT_VERSION
="bkgpslog 0.0.1" # Define version of script.
12 SCRIPT_TIME_SHORT
="$(date +%Y%m%dT%H%M%S%z)" # Save current date & time in ISO-8601 format.
13 SCRIPT_DATE_SHORT
="$(date +%Y%m%d)" # Save current date in ISO-8601 format.
14 #DIROUT1="/home/pi/Sync/Evanescent_Location" # Define output directory.
16 #== Script Function Definitions ==
17 # Yell, Die, Try Three-Fingered Claw technique
18 # Ref/Attrib: https://stackoverflow.com/a/25515370
19 yell
() { echo "$0: $*" >&2; }
20 die
() { yell
"$*"; exit 111; }
21 try
() { "$@" || die
"cannot $*"; }
24 echo "$@" 1>&2; # Define stderr echo function.
25 } # Define stderr message function.
28 echoerr
" bkgpslog.sh [ options ]"
32 echoerr
" Display help information."
35 echoerr
" Display script version."
37 echoerr
" -v, --verbose"
38 echoerr
" Display debugging info."
40 echoerr
" -o, --output [ directory ]"
41 echoerr
" Specify output directory to save logs."
42 } # Display information on how to use this script.
44 echoerr
"$SCRIPT_VERSION"
45 } # Display script version.
47 if [ $OPTION_VERBOSE -eq "true" ]; then
50 } # Verbose message display function.
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:""$@"
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.
64 } # Argument Processing
67 } # Check that certain executables exist.
70 processArguments
# Process arguments.
71 checkExecutables
"gpspipe" # Confirm that executables necessary to run are available.
79 if [[ -d "$DIROUT1" ]] && command -v "gpspipe" 1>/dev
/null
2>/dev
/null
; then
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.
100 # Update output file names.
102 FILEOUTN
="$DATE_CURRENT_SHORT""..""$(hostname)""_location.nmea" ;
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
108 #gpspipe -r -d -l -o "$DIROUT1"/"$FILEOUT1" ;
111 echo "$SCRIPT_TIME_SHORT"">gpspipe or $DIROUT1 not found." >> ~
/"$SCRIPT_DATE_SHORT"..error.log
;