#==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.5.7"; # 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"
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
# 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()
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" --iso-8601=seconds)")".."$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:FN :$FN";
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 argCustomScriptTTL is a time element (ex: "day", "hour")
if validateInput "$argCustomScriptTTL" "time_element"; then
### T: argCustomScriptTTL is a time element
- SCRIPT_TTL_TE="$argCustomScriptTTL" && vbm "Custom SCRIPT_TTL_TE from -B:$SCRIPT_TTL_TE";
+ 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
#### 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" && chmod +t "$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";
# MAIN LOOP:Record gps data until script lifespan ends
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"; # adjusted by magicBufferSleepPID