+ # Define input types
+ ## ssh_pubkey
+ ### Check for alnum/dash base64 (ex: "ssh-rsa AAAAB3NzaC1yc2EAAA")
+ if [[ "$argType" = "ssh_pubkey" ]]; then
+ if [[ "$argInput" =~ ^[[:alnum:]-]*[\ ]*[[:alnum:]+/=]*$ ]]; then
+ return 0; fi; fi;
+
+ ## age_pubkey
+ ### Check for age1[:bech32:]
+ if [[ "$argType" = "age_pubkey" ]]; then
+ if [[ "$argInput" =~ ^age1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]*$ ]]; then
+ return 0; fi; fi
+
+ # Return error if no condition matched.
+ return 1;
+} # Validates strings
+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
+ # Output: appends tar PATHOUT_TAR
+ # Depends: dateTimeShort, appendArgTar
+ local CONTENT_VERSION pubKeyIndex
+
+ # Set VERSION file name
+ FILEOUT_VERSION="$(dateTimeShort)..VERSION";
+
+ # Gather VERSION data in CONTENT_VERSION
+ CONTENT_VERSION="SCRIPT_VERSION=$SCRIPT_VERSION";
+ #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""DATE=$(date --iso-8601=seconds)";
+ CONTENT_VERSION="$CONTENT_VERSION""\\n""HOSTNAME=$SCRIPT_HOSTNAME";
+ ## Add list of recipient pubkeys
+ for pubkey in "${recPubKeysValid[@]}"; do
+ ((pubKeyIndex++))
+ CONTENT_VERSION="$CONTENT_VERSION""\\n""PUBKEY_$pubKeyIndex=$pubkey";
+ done
+ ## 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: PATHOUT_TAR FILEOUT_{NMEA,GPX,KML} CMD_CONV_{NMEA,GPX,KML} CMD_{COMPRESS,ENCRYPT} DIR_TMP,
+ # Inputs: BUFFER_TTL bufferTTL_STR SCRIPT_HOSTNAME CMD_COMPRESS_SUFFIX CMD_ENCRYPT_SUFFIX
+ # Depends: yell, try, vbm, appendArgTar, tar
+ local FN="${FUNCNAME[0]}";
+ wait; # Wait to avoid collision with older magicWriteBuffer() instances (see https://www.tldp.org/LDP/abs/html/x9644.html )
+ # Create buffer file with unique name
+ PATHOUT_BUFFER="$DIR_TMP/buffer$SECONDS";
+ # 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().";
+ # 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: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";
+
+
+ # 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)
+ if [[ $? -eq 1 ]] || [[ $? -eq 2 ]]; then magicWriteVersion; fi
+
+ # Write bufferBash to PATHOUT_TAR
+ 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 magicWriteBuffer().";
+} # write buffer to disk
+magicParseRecipientDir() {
+ # Desc: Updates recPubKeysValid with pubkeys in dir specified by '-R' option ("recipient directory")
+ # Inputs: vars: OPTION_RECDIR, argRecDir, OPTION_ENCRYPTION
+ # arry: recPubKeysValid
+ # Outputs: arry: recPubKeysValid
+ # Depends: processArguments,
+ local recFileLine updateRecipients recipientDir
+ declare -a candRecPubKeysValid
+
+ # Check that '-e' and '-R' set
+ if [[ "$OPTION_ENCRYPTION" = "true" ]] && [[ "$OPTION_RECDIR" = "true" ]]; then
+ ### Check that argRecDir is a directory.
+ if [[ -d "$argRecDir" ]]; then
+ recipientDir="$argRecDir";
+ #### Initialize variable indicating outcome of pubkey review
+ unset updateRecipients
+ #### Add existing recipients
+ candRecPubKeysValid=(${recPubKeysValid[@]});
+ #### Parse files in recipientDir
+ for file in "$recipientDir"/*; do
+ ##### Read first line of each file
+ recFileLine="$(cat "$file" | head -n1)";
+ ##### 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");
+ 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[@]});
+ fi;
+ else
+ yell "ERROR:$0:Recipient directory $argRecDir does not exist. Exiting."; exit 1;
+ fi;
+ fi;
+ # Handle case if '-e' set but '-R' not set
+ if [[ "$OPTION_ENCRYPTION" = "true" ]] && [[ ! "$OPTION_RECDIR" = "true" ]]; then
+ yell "ERROR: \'-e\' set but \'-R\' is not set."; fi;
+ # Handle case if '-R' set but '-e' not set
+ if [[ ! "$OPTION_ENCRYPTION" = "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
+ # Depends: checkapp(), checkAgePubkey(), validateInput(), processArguments()
+ local recipients
+
+ # Check if encryption option active.
+ if [[ "$OPTION_ENCRYPT" = "true" ]] && [[ "$OPTION_RECIPIENTS" = "true" ]]; then