+ ## Construct duration output string
+ OUTPUT="P"
+ if $displayYears; then
+ OUTPUT=$OUTPUT$fullYears"Y"; fi
+ if $displayMonths; then
+ OUTPUT=$OUTPUT$fullMonths"M"; fi
+ if $displayDays; then
+ OUTPUT=$OUTPUT$fullDays"D"; fi
+ if $displayDateTime; then
+ OUTPUT=$OUTPUT"T"; fi
+ if $displayHours; then
+ OUTPUT=$OUTPUT$fullHours"H"; fi
+ if $displayMinutes; then
+ OUTPUT=$OUTPUT$fullMinutes"M"; fi
+ if $displaySeconds; then
+ OUTPUT=$OUTPUT$fullSeconds"S"; fi
+
+ ## Output duration string to stdout
+ if [[ "$returnState" = "true" ]]; then echo "$OUTPUT"; fi
+
+ #===Determine function return code===
+ if [ "$returnState" = "true" ]; then
+ return 0;
+ else
+ echo "$returnState" 1>&2;
+ return 1;
+ fi
+
+} # Get duration (ex: PT10M4S )
+displayMissing() {
+ # Desc: Displays missing apps, files, and dirs
+ # Usage: displayMissing
+ # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
+ # Output: stderr messages
+ #==BEGIN Display errors==
+ #===BEGIN Display Missing Apps===
+ missingApps="Missing apps :"
+ #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
+ for key in "${!appRollCall[@]}"; do
+ value="${appRollCall[$key]}"
+ if [ "$value" = "false" ]; then
+ #echo "DEBUG:Missing apps: $key => $value";
+ missingApps="$missingApps""$key "
+ appMissing="true"
+ fi
+ done
+ if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
+ echo "$missingApps" 1>&2;
+ fi
+ #===END Display Missing Apps===
+
+ #===BEGIN Display Missing Files===
+ missingFiles="Missing files:"
+ #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
+ for key in "${!fileRollCall[@]}"; do
+ value="${fileRollCall[$key]}"
+ if [ "$value" = "false" ]; then
+ #echo "DEBUG:Missing files: $key => $value";
+ missingFiles="$missingFiles""$key "
+ fileMissing="true"
+ fi
+ done
+ if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
+ echo "$missingFiles" 1>&2;
+ fi
+ #===END Display Missing Files===
+
+ #===BEGIN Display Missing Directories===
+ missingDirs="Missing dirs:"
+ #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
+ for key in "${!dirRollCall[@]}"; do
+ value="${dirRollCall[$key]}"
+ if [ "$value" = "false" ]; then
+ #echo "DEBUG:Missing dirs: $key => $value";
+ missingDirs="$missingDirs""$key "
+ dirMissing="true"
+ fi
+ done
+ if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
+ echo "$missingDirs" 1>&2;
+ fi
+ #===END Display Missing Directories===
+
+ #==END Display errors==
+} # Display missing apps, files, dirs
+setScriptTTL() {
+ #Desc: Sets script TTL
+ #Usage: setScriptTTL arg1
+ #Input: arg1: "day" or "hour"
+ #Output: scriptTTL
+ #Depends: timeUntilNextHour or timeUntilNextDay
+ local ARG1
+ ARG1="$1"
+ if [[ "$ARG1" = "day" ]]; then
+ # Set script lifespan to end at start of next day
+ if ! scriptTTL="$(timeUntilNextDay)"; then
+ if [[ "$scriptTTL" -eq 0 ]]; then
+ ((scriptTTL++)); # Add 1 because 0 would cause 'timeout' to never timeout.
+ else
+ yell "ERROR: timeUntilNextDay exit code $?"; exit 1;
+ fi;
+ fi;
+ elif [[ "$ARG1" = "hour" ]]; then
+ # Set script lifespan to end at start of next hour
+ if ! scriptTTL="$(timeUntilNextHour)"; then
+ if [[ "$scriptTTL" -eq 0 ]]; then
+ ((scriptTTL++)); # Add 1 because 0 would cause 'timeout' to never timeout.
+ else
+ yell "ERROR: timeUntilNextHour exit code $?"; exit 1;
+ fi;
+ fi;
+ else
+ yell "ERROR:Invalid argument for setScriptTTL function."; exit 1;
+ fi
+} # Seconds until next (day|hour).
+main() {
+ processArguments "$@" # Process arguments.
+
+ # Set output encryption and compression option strings
+ if [[ "$OPTION_ENCRYPT" = "true" ]]; then # Check if encryption option active.
+ if checkapp age; then # Check that age is available.
+ for pubkey in "${recPubKeys[@]}"; do # Validate recipient pubkey strings by forming test message
+ vbm "DEBUG:Testing pubkey string:$pubkey"
+ if echo "butts" | age -a -r "$pubkey" 1>/dev/null; then
+ # Form age recipient string
+ recipients="$recipients""-r $pubkey ";
+ vbm "Added pubkey for forming age recipient string:""$pubkey";
+ vbm "DEBUG:recipients:""$recipients";
+ else
+ yell "ERROR:Exit code ""$?"". Invalid recipient pubkey string. Exiting."; exit 1;
+ fi
+ done
+ vbm "DEBUG:Finished processing recPubKeys array";
+ # Form age command string
+ CMD_ENCRYPT="age ""$recipients ";
+ CMD_ENCRYPT_SUFFIX=".age";
+ else
+ yell "ERROR:Encryption enabled but \"age\" not found. Exiting."; exit 1;
+ fi
+ else
+ CMD_ENCRYPT="tee /dev/null ";
+ CMD_ENCRYPT_SUFFIX="";
+ vbm "DEBUG:Encryption not enabled."
+ fi
+ if [[ "$OPTION_COMPRESS" = "true" ]]; then # Check if compression option active
+ if checkapp gzip; then # Check if gzip available
+ CMD_COMPRESS="gzip ";
+ CMD_COMPRESS_SUFFIX=".gz";
+ else
+ yell "ERROR:Compression enabled but \"gzip\" not found. Exiting."; exit 1;
+ fi
+ else
+ CMD_COMPRESS="tee /dev/null ";
+ CMD_COMPRESS_SUFFIX="";
+ vbm "DEBUG:Compression not enabled."
+ fi
+
+ # Check that critical apps and dirs are available, displag missing ones.
+ if ! checkapp gpspipe tar && ! checkdir "$DIR_OUT" "/dev/shm"; then
+ yell "ERROR:Critical components missing.";
+ displayMissing; yell "Exiting."; exit 1; fi
+
+ # Set script lifespan
+ setScriptTTL "$SCRIPT_TTL";
+
+ # File name substring: encoded bufferTTL
+ bufferTTL_STR="$(timeDuration $BUFFER_TTL)";
+
+ # Init temp working dir
+ try mkdir "$DIR_TMP" && vbm "DEBUG:Working dir creatd at:$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
+ ## Write bkgpslog version to DIR_TMP/VERSION
+ echo "$0"" Version:""$SCRIPT_VERSION" >> "$DIR_TMP/VERSION" && vbm "DEBUG:VERSION created."
+ ## Create empty tar archive at PATHOUT_TAR
+ try tar --create --directory="$DIR_TMP" --file="$PATHOUT_TAR" --files-from=/dev/null && vbm "DEBUG:bufferRam.tar created."
+ ## Append VERSION file to PATHOUT_TAR
+ try tar --append --directory="$DIR_TMP" --file="$PATHOUT_TAR" "VERSION" && vbm "DEBUG:VERSION added to $PATHOUT_TAR"