#!/bin/bash
-
-# Desc: Records gps data until midnight
-# Author: Steven Baltakatei Sandoval; License: GPLv3+
-# Usage: bkgpslog -o [output dir]
+# Desc: Records gps data
#==BEGIN Define script parameters==
## Logging Behavior parameters
-BUFFER_TTL="300"; # time between file writes
-SCRIPT_TTL_TE="day"; # (day|hour)
+bufferTTL="300"; # time between file writes
+scriptTTL_TE="day"; # (day|hour)
#### TZ="UTC"; export TZ; # Default time zone; overridden by '--time-zone=[str]' option
-DIR_TMP_DEFAULT="/dev/shm"; # Default parent of working directory
+dirTmpDefault="/dev/shm"; # Default parent of working directory
-SCRIPT_TIME_START=$(date +%Y%m%dT%H%M%S.%N);
+scriptTimeStart=$(date +%Y%m%dT%H%M%S.%N);
PATH="$HOME/.local/bin:$PATH"; # Add "$(systemd-path user-binaries)" path in case apps saved there
-SCRIPT_HOSTNAME=$(hostname); # Save hostname of system running this script.
-SCRIPT_VERSION="0.4.4-alpha2"; # Define version of script.
-SCRIPT_NAME="bkgpslog"; # Define basename of script file.
-SCRIPT_URL="https://gitlab.com/baltakatei/ninfacyzga-01"; # Define wesite hosting this script.
-AGE_VERSION="1.0.0-beta2"; # Define version of age (encryption program)
-AGE_URL="https://github.com/FiloSottile/age/releases/tag/v1.0.0-beta2"; # Define website hosting age.
+scriptHostname=$(hostname); # Save hostname of system running this script.
+scriptVersion="0.5.7"; # Define version of script.
+scriptName="bkgpslog"; # Define basename of script file.
+scriptURL="https://gitlab.com/baltakatei/ninfacyzga-01"; # Define wesite hosting this script.
+ageVersion="1.0.0-beta2"; # Define version of age (encryption program)
+ageURL="https://github.com/FiloSottile/age/releases/tag/v1.0.0-beta2"; # Define website hosting age.
declare -Ag appRollCall # Associative array for storing app status
declare -Ag fileRollCall # Associative array for storing file status
echoerr "-o ~/Sync/Location"
} # Display information on how to use this script.
showVersion() {
- echoerr "$SCRIPT_VERSION"
+ echoerr "$scriptVersion"
} # Display script version.
vbm() {
# Usage: vbm "DEBUG:verbose message here"
# Input: arg1: 'date'-parsable timestamp string (optional)
# Output: stdout: timestamp (ISO-8601, no separators)
# Depends: yell
- local TIME_CURRENT TIME_CURRENT_SHORT
+ local TIME_CURRENT TIME_CURRENT_SHORT argTime
argTime="$1";
# Get Current Time
TIME_INPUT="$argTime";
else
### F: Time argument not valid; exit
- yell "ERROR:Invalid time argument supplied. Exiting."; exit 1;
+ yell "ERROR:Invalid time argument supplied: \"$argTime\""; yell "Exiting."; exit 1;
fi
fi
# Construct and deliver separator-les date string
TIME_INPUT="$argTime";
else
### F: Time argument not valid; exit
- yell "ERROR:Invalid time argument supplied. Exiting."; exit 1;
+ yell "ERROR:Invalid time argument supplied: \"$argTime\""; yell "Exiting."; exit 1;
fi
fi
# Construct and deliver separator-les date string
magicSetScriptTTL() {
#Desc: Sets script_TTL seconds from provided time_element string argument
#Usage: magicSetScriptTTL [str time_element]
- #Input: arg1: string (Ex: SCRIPT_TTL_TE; "day" or "hour")
+ #Input: arg1: string (Ex: scriptTTL_TE; "day" or "hour")
#Output: var: SCRIPT_TTL (integer seconds)
#Depends: timeUntilNextHour, timeUntilNextDay
local argTimeElement
echo "$TIME_EPOCH_NS";
} # Nanoseconds since 1970-01-01
magicBufferSleepPID() {
- # Desc: Compensates for lag so buffer rounds start every BUFFER_TTL seconds
- # Input: vars: BUFFER_TTL, errResetx10e3, K_P, T_I, T_D
+ # Desc: Compensates for lag so buffer rounds start every bufferTTL seconds
+ # Input: vars: bufferTTL, errResetx10e3, K_P, T_I, T_D
# # Input: array: errorHistory
# Output: vars: BUFFER_TTL_ADJ_FLOAT
# Re/Attrib: https://en.wikipedia.org/wiki/PID_controller#Standard_versus_parallel_(ideal)_form
## Form float BUFFER_TTL_ADJ_FLOAT
BUFFER_TTL_ADJ_FLOAT="$BUFFER_TTL_ADJ_INT"."$BUFFER_TTL_ADJ_FLOATFRAC" && vbm "BUFFER_TTL_ADJ_FLOAT:$BUFFER_TTL_ADJ_FLOAT";
vbm "STATUS:Calculated adjusted BUFFER_TTL (seconds):$BUFFER_TTL_ADJ_FLOAT";
- sleep 1 # Debug
} # Calc BUFFER_TTL_ADJ_FLOAT so buffer starts every BUFFER_TTL seconds
magicWriteVersion() {
# Desc: Appends time-stamped VERSION to PATHOUT_TAR
# Usage: magicWriteVersion
# Version: 0.1.0
# Input: CONTENT_VERSION, FILEOUT_VERSION, PATHOUT_TAR, DIR_TMP
- # Input: SCRIPT_VERSION, SCRIPT_URL, AGE_VERSION, AGE_URL, SCRIPT_HOSTNAME
+ # Input: scriptVersion, scriptURL, ageVersion, ageURL, scriptHostname
# Output: appends tar PATHOUT_TAR
# Depends: dateTimeShort, appendArgTar
local CONTENT_VERSION pubKeyIndex
FILEOUT_VERSION="$(dateTimeShort)..VERSION";
# Gather VERSION data in CONTENT_VERSION
- CONTENT_VERSION="SCRIPT_VERSION=$SCRIPT_VERSION";
+ CONTENT_VERSION="scriptVersion=$scriptVersion";
#CONTENT_VERSION="$CONTENT_VERSION""\\n";
- CONTENT_VERSION="$CONTENT_VERSION""\\n""SCRIPT_NAME=$SCRIPT_NAME";
- CONTENT_VERSION="$CONTENT_VERSION""\\n""SCRIPT_URL=$SCRIPT_URL";
- CONTENT_VERSION="$CONTENT_VERSION""\\n""AGE_VERSION=$AGE_VERSION";
- CONTENT_VERSION="$CONTENT_VERSION""\\n""AGE_URL=$AGE_URL";
+ CONTENT_VERSION="$CONTENT_VERSION""\\n""scriptName=$scriptName";
+ CONTENT_VERSION="$CONTENT_VERSION""\\n""scriptURL=$scriptURL";
+ CONTENT_VERSION="$CONTENT_VERSION""\\n""ageVersion=$ageVersion";
+ CONTENT_VERSION="$CONTENT_VERSION""\\n""ageURL=$ageURL";
CONTENT_VERSION="$CONTENT_VERSION""\\n""DATE=$(date --iso-8601=seconds)";
- CONTENT_VERSION="$CONTENT_VERSION""\\n""HOSTNAME=$SCRIPT_HOSTNAME";
+ CONTENT_VERSION="$CONTENT_VERSION""\\n""HOSTNAME=$scriptHostname";
## Add list of recipient pubkeys
for pubkey in "${recPubKeysValid[@]}"; do
((pubKeyIndex++))
magicGatherWriteBuffer() {
# Desc: bkgpslog-specific meta function for writing data to DIR_TMP then appending each file to PATHOUT_TAR
# Inputs: vars: PATHOUT_TAR FILEOUT_{NMEA,GPX,KML} CMD_CONV_{NMEA,GPX,KML} CMD_{COMPRESS,ENCRYPT} DIR_TMP,
- # Inputs: vars: BUFFER_TTL bufferTTL_STR SCRIPT_HOSTNAME CMD_COMPRESS_SUFFIX CMD_ENCRYPT_SUFFIX
+ # Inputs: vars: BUFFER_TTL bufferTTL_STR scriptHostname CMD_COMPRESS_SUFFIX CMD_ENCRYPT_SUFFIX
# Output: file: (PATHOUT_TAR)
# Depends: yell(), try(), vbm(), appendArgTar(), tar 1, sleep 8, checkMakeTar()
# Depends: magicWriteVersion(), appendFileTar()
local FN
+ vbm "DEBUG:STATUS:$FN:Started magicGatherWriteBuffer().";
# Debug:Get function name
FN="${FUNCNAME[0]}";
# Create buffer file with unique name
- PATHOUT_BUFFER="$DIR_TMP/buffer$SECONDS";
+ PATHOUT_BUFFER="$DIR_TMP/buffer$SECONDS" && vbm "PATHOUT_BUFFER:$PATHOUT_BUFFER";
# Fill buffer
timeout "$BUFFER_TTL"s gpspipe -r -o "$PATHOUT_BUFFER" ;
- timeBufferStart="$(dateTimeShort "$(date --date="$BUFFER_TTL seconds ago")")"; # Note start time
- vbm "DEBUG:STATUS:$FN:Started magicWriteBuffer().";
+ timeBufferStartLong="$(date --date="$BUFFER_TTL seconds ago" --iso-8601=seconds)" && vbm "timeBufferStartLong:$timeBufferStartLong" || yell "ERROR:timeBufferStartLong fail";
+ timeBufferStart="$(dateTimeShort "$timeBufferStartLong" )" && vbm "timeBufferStart:$timeBufferStart" || yell "ERROR:timeBufferStart fail"; # Note start time
# Determine file paths (time is start of buffer period)
- FILEOUT_BASENAME="$timeBufferStart""--""$bufferTTL_STR""..""$SCRIPT_HOSTNAME""_location" && vbm "STATUS:Set FILEOUT_BASENAME to:$FILEOUT_BASENAME";
+ FILEOUT_BASENAME="$timeBufferStart""--""$bufferTTL_STR""..""$scriptHostname""_location" && vbm "STATUS:Set FILEOUT_BASENAME to:$FILEOUT_BASENAME";
## Files saved to DIR_TMP
FILEOUT_NMEA="$FILEOUT_BASENAME".nmea"$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX" && vbm "STATUS:Set FILEOUT_NMEA to:$FILEOUT_NMEA";
FILEOUT_GPX="$FILEOUT_BASENAME".gpx"$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX" && vbm "STATUS:Set FILEOUT_GPX to:$FILEOUT_GPX";
PATHOUT_KML="$DIR_TMP"/"$FILEOUT_KML" && vbm "STATUS:Set PATHOUT_KML to:$PATHOUT_KML";
## Files saved to disk (DIR_OUT)
### one file per day (Ex: "20200731..hostname_location.[.gpx.gz].tar")
- PATHOUT_TAR="$DIR_OUT"/"$(dateShort "$(date --date="$BUFFER_TTL seconds ago")")".."$SCRIPT_HOSTNAME""_location""$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX".tar && \
+ PATHOUT_TAR="$DIR_OUT"/"$(dateShort "$(date --date="$BUFFER_TTL seconds ago" --iso-8601=seconds)")".."$scriptHostname""_location""$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX".tar && \
vbm "STATUS:Set PATHOUT_TAR to:$PATHOUT_TAR";
# DEBUG: check vars
- vbm "STATUS:DIR_TMP :$DIR_TMP";
- vbm "STATUS:PATHOUT_TAR :$PATHOUT_TAR";
- vbm "STATUS:PATHOUT_NMEA:$PATHOUT_NMEA";
- vbm "STATUS:PATHOUT_GPX:$PATHOUT_GPX";
- vbm "STATUS:PATHOUT_KML:$PATHOUT_KML";
+ vbm "STATUS:FN :$FN";
+ vbm "STATUS:DIR_TMP :$DIR_TMP";
+ vbm "STATUS:PATHOUT_TAR :$PATHOUT_TAR";
+ vbm "STATUS:PATHOUT_NMEA :$PATHOUT_NMEA";
+ vbm "STATUS:PATHOUT_GPX :$PATHOUT_GPX";
+ vbm "STATUS:PATHOUT_KML :$PATHOUT_KML";
+ vbm "STATUS:BUFFER_TTL :$BUFFER_TTL";
+ vbm "STATUS:PATHOUT_BUFFER :$PATHOUT_BUFFER";
+ vbm "STATUS:timeBufferStart:$timeBufferStart";
+ vbm "FILEOUT_BASENAME :$FILEOUT_BASENAME";
# Validate PATHOUT_TAR as tar.
checkMakeTar "$PATHOUT_TAR";
## Add VERSION file if checkMakeTar had to create a tar (exited 1) or replace one (exited 2)
+ vbm "exit status before magicWriteVersion:$?"
if [[ $? -eq 1 ]] || [[ $? -eq 2 ]]; then magicWriteVersion; fi
# Write bufferBash to PATHOUT_TAR
# Remove secured chunks from DIR_TMP
rm "$PATHOUT_BUFFER" "$PATHOUT_NMEA" "$PATHOUT_GPX" "$PATHOUT_KML";
- vbm "DEBUG:STATUS:$FN:Finished magicWriteBuffer().";
+ vbm "DEBUG:STATUS:$FN:Finished magicGatherWriteBuffer().";
} # write buffer to disk
magicParseRecipientDir() {
# Desc: Updates recPubKeysValid with pubkeys in dir specified by '-R' option ("recipient directory")
magicInitWorkingDir() {
# Desc: Determine temporary working directory from defaults or user input
# Usage: magicInitWorkignDir
- # Input: vars: OPTION_TEMPDIR, argTempDirPriority, DIR_TMP_DEFAULT
- # Input: vars: SCRIPT_TIME_START
+ # Input: vars: OPTION_TEMPDIR, argTempDirPriority, dirTmpDefault
+ # Input: vars: scriptTimeStart
# Output: vars: DIR_TMP
# Depends: processArguments(), vbm(), yell()
# Parse '-t' option (user-specified temporary working dir)
fi;
else
## Set DIR_TMP_PARENT to default or fallback otherwise
- if [[ -d "$DIR_TMP_DEFAULT" ]]; then
- DIR_TMP_PARENT="$DIR_TMP_DEFAULT";
+ if [[ -d "$dirTmpDefault" ]]; then
+ DIR_TMP_PARENT="$dirTmpDefault";
elif [[ -d /tmp ]]; then
- yell "WARNING:$DIR_TMP_DEFAULT not available. Falling back to /tmp .";
+ yell "WARNING:$dirTmpDefault not available. Falling back to /tmp .";
DIR_TMP_PARENT="/tmp";
else
yell "ERROR:No valid working directory available. Exiting.";
exit 1;
fi
fi;
- ## Set DIR_TMP using DIR_TMP_PARENT and nonce (SCRIPT_TIME_START)
- DIR_TMP="$DIR_TMP_PARENT"/"$SCRIPT_TIME_START""..bkgpslog" && vbm "DEBUG:Set DIR_TMP to:$DIR_TMP"; # Note: removed at end of main().
+ ## Set DIR_TMP using DIR_TMP_PARENT and nonce (scriptTimeStart)
+ DIR_TMP="$DIR_TMP_PARENT"/"$scriptTimeStart""..bkgpslog" && vbm "DEBUG:Set DIR_TMP to:$DIR_TMP"; # Note: removed at end of main().
} # Sets working dir
magicParseCustomTTL() {
# Desc: Set user-specified TTLs for buffer and script
# Input: vars: argCustomBufferTTL (integer), argCustomScriptTTL_TE (string)
# Input: vars: OPTION_CUSTOM_BUFFERTTL, OPTION_CUSTOM_SCRIPTTTL
- # Input: vars: BUFFER_TTL (integer), SCRIPT_TTL_TE (string)
- # Output: BUFFER_TTL (integer), SCRIPT_TTL_TE (string)
+ # Input: vars: BUFFER_TTL (integer), scriptTTL_TE (string)
+ # Output: BUFFER_TTL (integer), scriptTTL_TE (string)
# Depends validateInput(), showUsage(), yell
# React to '-b, --buffer-ttl' option
## T: Check if argCustomBufferTTL is an integer
if validateInput "$argCustomBufferTTL" "integer"; then
### T: argCustomBufferTTL is an integer
- BUFFER_TTL="$argCustomBufferTTL";
+ BUFFER_TTL="$argCustomBufferTTL" && vbm "Custom BUFFER_TTL from -b:$BUFFER_TTL";
else
### F: argcustomBufferTTL is not an integer
yell "ERROR:Invalid integer argument for custom buffer time-to-live."; showUsage; exit 1;
## T: Check if argCustomScriptTTL is a time element (ex: "day", "hour")
if validateInput "$argCustomScriptTTL" "time_element"; then
### T: argCustomScriptTTL is a time element
- SCRIPT_TTL_TE="$argCustomScriptTTL";
+ scriptTTL_TE="$argCustomScriptTTL" && vbm "Custom scriptTTL_TE from -B:$scriptTTL_TE";
else
### F: argcustomScriptTTL is not a time element
yell "ERROR:Invalid time element argument for custom script time-to-live."; showUsage; exit 1;
fi;
- ## F: do not change SCRIPT_TTL_TE
+ ## F: do not change scriptTTL_TE
fi;
} # Sets custom script or buffer TTL if specified
main() {
+ # DEBUG: Print environment variables
+ vbm "echo $(printenv)";
# Process arguments
processArguments "$@";
## Act upon arguments
#### React to "-R" ("recipient directory") option
magicParseRecipientDir; # Updates recPubKeysValid
#### React to custom buffer and script TTL options ("-b", "-B")
- magicParseCustomTTL; # Sets custom SCRIPT_TTL_TE and/or BUFFER_TTL if specified
+ magicParseCustomTTL; # Sets custom scriptTTL_TE and/or bufferTTL if specified
# Check that critical apps and dirs are available, display missing ones.
if ! checkapp gpspipe tar && ! checkdir "$DIR_OUT" "DIR_TMP"; then
yell "ERROR:Critical components missing.";
displayMissing; yell "Exiting."; exit 1; fi
- # Set script lifespan (SCRIPT_TTL from SCRIPT_TTL_TE)
- magicSetScriptTTL "$SCRIPT_TTL_TE";
- ## Note: SCRIPT_TTL_TE is time element string (ex: "day") while SCRIPT_TTL is integer seconds
+ # Set script lifespan (SCRIPT_TTL from scriptTTL_TE)
+ magicSetScriptTTL "$scriptTTL_TE";
+ ## Note: scriptTTL_TE is time element string (ex: "day") while SCRIPT_TTL is integer seconds
# File name substring (ISO-8601 duration from BUFFER_TTL)
- bufferTTL_STR="$(timeDuration "$BUFFER_TTL")";
+ bufferTTL_STR="$(timeDuration "$bufferTTL")" && vbm "DEBUG:bufferTTL_STR:$bufferTTL_STR";
# Init temp working dir
- try mkdir "$DIR_TMP" && vbm "DEBUG:Working dir creatd at:$DIR_TMP";
+ try mkdir "$DIR_TMP" && vbm "DEBUG:Working dir created at DIR_TMP:$DIR_TMP";
# Initialize 'tar' archive
## Define output tar path (note: each day gets *one* tar file (Ex: "20200731..hostname_location.[.gpx.gz].tar"))
- PATHOUT_TAR="$DIR_OUT"/"$(dateShort)".."$SCRIPT_HOSTNAME""_location""$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX".tar && \
+ PATHOUT_TAR="$DIR_OUT"/"$(dateShort)".."$scriptHostname""_location""$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX".tar && \
vbm "STATUS:Set PATHOUT_TAR to:$PATHOUT_TAR";
## Check that PATHOUT_TAR is a tar. Rename old and create empty one otherwise.
checkMakeTar "$PATHOUT_TAR" && vbm "DEBUG:Confirmed or Created to be a tar:$PATHOUT_TAR";
CMD_CONV_KML="gpsbabel -i nmea -f - -o kml -F - " && vbm "STATUS:Set CMD_CONV_KML to:$CMD_CONV_KML"; # convert NMEA to KML
# MAIN LOOP:Record gps data until script lifespan ends
- timeBufferFirstNS="$(timeEpochNS)"; bufferRound=0; BUFFER_TTL_ADJ_FLOAT="10.0";
+ timeBufferFirstNS="$(timeEpochNS)"; bufferRound=0; BUFFER_TTL_ADJ_FLOAT="$BUFFER_TTL";
while [[ "$SECONDS" -lt "$SCRIPT_TTL" ]]; do
+ if ! [[ -d "$DIR_TMP" ]]; then yell "ERROR:DIR_TMP existence failure:$DIR_TMP"; try mkdir "$DIR_TMP" && vbm "DEBUG:Working dir recreated DIR_TMP:$DIR_TMP"; fi
magicParseRecipientDir;
magicGatherWriteBuffer &
- sleep "$BUFFER_TTL_ADJ_FLOAT";
+ sleep "$BUFFER_TTL_ADJ_FLOAT"; # adjusted by magicBufferSleepPID
((bufferRound++));
- magicBufferSleepPID; # Calculates BUFFER_TTL_ADJ from BUFFER_TTL given buffer expected start time vs. actual
+ magicBufferSleepPID; # Calculates BUFFER_TTL_ADJ_FLOAT from BUFFER_TTL given buffer expected start time vs. actual
done
# Cleanup
## Remove DIR_TMP
- try rm -r "$DIR_TMP";
+ try rm -r "$DIR_TMP" && vbm "Removed DIR_TMP:$DIR_TMP";
vbm "STATUS:Main function finished.";
} # Main function.
main "$@" # Run main function.
exit 0;
#==END Perform work and exit==
+
+# Author: Steven Baltakatei Sandoval;
+# License: GPLv3+