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