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