+ ## Process newline escapes
+ CONTENT_VERSION="$(echo -e "$CONTENT_VERSION")"
+
+ # Write CONTENT_VERSION as file FILEOUT_VERSION and write-append to PATHOUT_TAR
+ appendArgTar "$CONTENT_VERSION" "$FILEOUT_VERSION" "$PATHOUT_TAR" "$DIR_TMP";
+
+} # bkgpslog: write version data to PATHOUT_TAR via appendArgTar()
+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
+ # 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" && vbm "PATHOUT_BUFFER:$PATHOUT_BUFFER";
+ # Fill buffer
+ timeout "$BUFFER_TTL"s gpspipe -r -o "$PATHOUT_BUFFER" ;
+ timeBufferStartLong="$(date --date="$BUFFER_TTL seconds ago")" && 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";
+ ## 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";
+ FILEOUT_KML="$FILEOUT_BASENAME".kml"$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX" && vbm "STATUS:Set FILEOUT_KML to:$FILEOUT_KML";
+ PATHOUT_NMEA="$DIR_TMP"/"$FILEOUT_NMEA" && vbm "STATUS:Set PATHOUT_NMEA to:$PATHOUT_NMEA";
+ PATHOUT_GPX="$DIR_TMP"/"$FILEOUT_GPX" && vbm "STATUS:Set PATHOUT_GPX to:$PATHOUT_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 && \
+ vbm "STATUS:Set PATHOUT_TAR to:$PATHOUT_TAR";
+ # DEBUG: check vars
+ 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
+ wait; # Wait to avoid collision with older magicWriteBuffer() instances (see https://www.tldp.org/LDP/abs/html/x9644.html )
+ appendFileTar "$PATHOUT_BUFFER" "$FILEOUT_NMEA" "$PATHOUT_TAR" "$DIR_TMP" "$CMD_CONV_NMEA" "$CMD_COMPRESS" "$CMD_ENCRYPT"; # Write NMEA data
+ appendFileTar "$PATHOUT_BUFFER" "$FILEOUT_GPX" "$PATHOUT_TAR" "$DIR_TMP" "$CMD_CONV_GPX" "$CMD_COMPRESS" "$CMD_ENCRYPT"; # Write GPX file
+ appendFileTar "$PATHOUT_BUFFER" "$FILEOUT_KML" "$PATHOUT_TAR" "$DIR_TMP" "$CMD_CONV_KML" "$CMD_COMPRESS" "$CMD_ENCRYPT"; # Write KML file
+
+ # Remove secured chunks from DIR_TMP
+ rm "$PATHOUT_BUFFER" "$PATHOUT_NMEA" "$PATHOUT_GPX" "$PATHOUT_KML";
+ vbm "DEBUG:STATUS:$FN:Finished magicGatherWriteBuffer().";
+} # write buffer to disk
+magicParseRecipientDir() {
+ # Desc: Updates recPubKeysValid with pubkeys in dir specified by '-R' option ("recipient directory")
+ # Inputs: vars: OPTION_RECDIR, argRecDir, OPTION_ENCRYPT
+ # arry: recPubKeysValid
+ # Outputs: arry: recPubKeysValid
+ # Depends: processArguments,
+ local recFileLine updateRecipients recipientDir
+ declare -a candRecPubKeysValid
+
+ # Check that '-e' and '-R' set
+ if [[ "$OPTION_ENCRYPT" = "true" ]] && [[ "$OPTION_RECDIR" = "true" ]]; then
+ ### Check that argRecDir is a directory.
+ if [[ -d "$argRecDir" ]]; then
+ recipientDir="$argRecDir" && vbm "STATUS:Recipient watch directory detected:\"$recipientDir\"";
+ #### Initialize variable indicating outcome of pubkey review
+ unset updateRecipients
+ #### Add existing recipients
+ candRecPubKeysValid=("${recPubKeysValidStatic[@]}");
+ #### Parse files in recipientDir
+ for file in "$recipientDir"/*; do
+ ##### Read first line of each file
+ recFileLine="$(head -n1 "$file")" && vbm "STATUS:Checking if pubkey:\"$recFileLine\"";
+ ##### check if first line is a valid pubkey
+ if checkAgePubkey "$recFileLine" && \
+ ( validateInput "$recFileLine" "ssh_pubkey" || validateInput "$recFileLine" "age_pubkey"); then
+ ###### T: add candidate pubkey to candRecPubKeysValid
+ candRecPubKeysValid+=("$recFileLine") && vbm "STATUS:RecDir pubkey is valid pubkey:\"$recFileLine\"";
+ else
+ ###### F: throw warning;
+ yell "ERROR:Invalid recipient file detected. Not modifying recipient list."
+ updateRecipients="false";
+ fi;
+ done
+ #### Write updated recPubKeysValid array to recPubKeysValid if no failure detected
+ if ! [[ "$updateRecipients" = "false" ]]; then
+ recPubKeysValid=("${candRecPubKeysValid[@]}") && vbm "STATUS:Wrote candRecPubkeysValid to recPubKeysValid:\"${recPubKeysValid[*]}\"";
+ fi;
+ else
+ yell "ERROR:$0:Recipient directory $argRecDir does not exist. Exiting."; exit 1;
+ fi;
+ fi;
+ # Handle case if '-R' set but '-e' not set
+ if [[ ! "$OPTION_ENCRYPT" = "true" ]] && [[ "$OPTION_RECDIR" = "true" ]]; then
+ yell "ERROR: \\'-R\\' is set but \\'-e\\' is not set."; fi;
+} # Update recPubKeysValid with argRecDir
+magicParseRecipientArgs() {
+ # Desc: Parses recipient arguments specified by '-r' option
+ # Input: vars: OPTION_ENCRYPT from processArguments()
+ # arry: argRecPubKeys from processArguments()
+ # Output: vars: CMD_ENCRYPT, CMD_ENCRYPT_SUFFIX
+ # arry: recPubKeysValid, recPubKeysValidStatic
+ # Depends: checkapp(), checkAgePubkey(), validateInput(), processArguments()
+ local recipients
+
+ # Check if encryption option active.
+ if [[ "$OPTION_ENCRYPT" = "true" ]] && [[ "$OPTION_RECIPIENTS" = "true" ]]; then
+ if checkapp age; then # Check that age is available.
+ for pubkey in "${argRecPubKeys[@]}"; do # Validate recipient pubkey strings by forming test message
+ vbm "DEBUG:Testing pubkey string:$pubkey";
+ if checkAgePubkey "$pubkey" && \
+ ( validateInput "$pubkey" "ssh_pubkey" || validateInput "$pubkey" "age_pubkey"); then
+ #### Form age recipient string
+ recipients="$recipients""-r '$pubkey' ";
+ vbm "STATUS:Added pubkey for forming age recipient string:""$pubkey";
+ vbm "DEBUG:recipients:""$recipients";
+ #### Add validated pubkey to recPubKeysValid array
+ recPubKeysValid+=("$pubkey") && vbm "DEBUG:recPubkeysValid:pubkey added:$pubkey";
+ else
+ yell "ERROR:Exit code ""$?"". Invalid recipient pubkey string. Exiting."; exit 1;
+ fi;
+ done
+ vbm "DEBUG:Finished processing argRecPubKeys array";
+ vbm "STATUS:Array of validated pubkeys:${recPubKeysValid[*]}";
+ recPubKeysValidStatic=("${recPubKeysValid[@]}"); # Save static image of pubkeys validated by this function
+
+ ## Form age command string
+ CMD_ENCRYPT="age ""$recipients " && vbm "CMD_ENCRYPT:$CMD_ENCRYPT";
+ CMD_ENCRYPT_SUFFIX=".age" && vbm "CMD_ENCRYPT_SUFFIX:$CMD_ENCRYPT_SUFFIX";
+ else
+ yell "ERROR:Encryption enabled but \"age\" not found. Exiting."; exit 1;
+ fi;
+ else
+ CMD_ENCRYPT="tee /dev/null " && vbm "CMD_ENCRYPT:$CMD_ENCRYPT";
+ CMD_ENCRYPT_SUFFIX="" && vbm "CMD_ENCRYPT_SUFFIX:$CMD_ENCRYPT_SUFFIX";
+ vbm "DEBUG:Encryption not enabled."
+ fi;
+ # Catch case if '-e' is set but '-r' or '-R' is not
+ if [[ "$OPTION_ENCRYPT" = "true" ]] && [[ ! "$OPTION_RECIPIENTS" = "true" ]]; then
+ yell "ERROR:\\'-e\\' set but no \\'-r\\' or \\'-R\\' set."; exit 1; fi;
+ # Catch case if '-r' or '-R' set but '-e' is not
+ if [[ ! "$OPTION_ENCRYPT" = "true" ]] && [[ "$OPTION_RECIPIENTS" = "true" ]]; then
+ yell "ERROR:\\'-r\\' or \\'-R\\' set but \\'-e\\' is not set."; exit 1; fi;
+} # Populate recPubKeysValid with argRecPubKeys; form encryption cmd string and filename suffix
+magicParseCompressionArg() {
+ # Desc: Parses compression arguments specified by '-c' option
+ # Input: vars: OPTION_COMPRESS
+ # Output: CMD_COMPRESS, CMD_COMPRESS_SUFFIX
+ # Depends: checkapp(), vbm(), gzip,
+ if [[ "$OPTION_COMPRESS" = "true" ]]; then # Check if compression option active
+ if checkapp gzip; then # Check if gzip available
+ CMD_COMPRESS="gzip " && vbm "CMD_COMPRESS:$CMD_COMPRESS";
+ CMD_COMPRESS_SUFFIX=".gz" && vbm "CMD_COMPRESS_SUFFIX:$CMD_COMPRESS_SUFFIX";
+ else
+ yell "ERROR:Compression enabled but \"gzip\" not found. Exiting."; exit 1;
+ fi
+ else
+ CMD_COMPRESS="tee /dev/null " && vbm "CMD_COMPRESS:$CMD_COMPRESS";
+ CMD_COMPRESS_SUFFIX="" && vbm "CMD_COMPRESS_SUFFIX:$CMD_COMPRESS_SUFFIX";
+ vbm "DEBUG:Compression not enabled.";
+ fi
+} # Form compression cmd string and filename suffix
+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
+ # Output: vars: DIR_TMP
+ # Depends: processArguments(), vbm(), yell()
+ # Parse '-t' option (user-specified temporary working dir)
+ ## Set DIR_TMP_PARENT to user-specified value if specified
+ local DIR_TMP_PARENT