test(bkgpslog):Construct command string as var before executing
[EVA-2020-02.git] / exec / bkgpslog
index b97f250b8b37f34048dfe6dcdfb986589bf70d61..a922a144ea46f6ab318c39904e52d855f8a77e64 100755 (executable)
@@ -15,6 +15,7 @@ declare -Ag appRollCall # Associative array for storing app status
 declare -Ag fileRollCall # Associative array for storing file status
 declare -Ag dirRollCall # Associative array for storing dir status
 declare -a recPubKeys # for processArguments function
 declare -Ag fileRollCall # Associative array for storing file status
 declare -Ag dirRollCall # Associative array for storing dir status
 declare -a recPubKeys # for processArguments function
+declare recipients # for main function
 
 #===BEGIN Declare local script functions===
 checkapp() {
 
 #===BEGIN Declare local script functions===
 checkapp() {
@@ -150,8 +151,11 @@ showUsage() {
     echoerr "    -o, --output [ directory ]"
     echoerr "            Specify output directory to save logs."
     echoerr
     echoerr "    -o, --output [ directory ]"
     echoerr "            Specify output directory to save logs."
     echoerr
+    echoerr "    -c, --compress"
+    echoerr "            Compress output with gzip (before encryption if enabled)."
+    echoerr
     echoerr "EXAMPLE: (bash script lines)"
     echoerr "EXAMPLE: (bash script lines)"
-    echoerr "/bin/bash bkgpslog -e \\"
+    echoerr "/bin/bash bkgpslog -e -c \\"
     echoerr "-r age1mrmfnwhtlprn4jquex0ukmwcm7y2nxlphuzgsgv8ew2k9mewy3rs8u7su5 \\"
     echoerr "-r age1ala848kqrvxc88rzaauc6vc5v0fqrvef9dxyk79m0vjea3hagclswu0lgq \\"
     echoerr "-o ~/Sync/Location"
     echoerr "-r age1mrmfnwhtlprn4jquex0ukmwcm7y2nxlphuzgsgv8ew2k9mewy3rs8u7su5 \\"
     echoerr "-r age1ala848kqrvxc88rzaauc6vc5v0fqrvef9dxyk79m0vjea3hagclswu0lgq \\"
     echoerr "-o ~/Sync/Location"
@@ -192,7 +196,8 @@ processArguments() {
            -o | --output) if [ -d "$2" ]; then DIROUT="$2"; vbm "DEBUG:DIROUT:$DIROUT"; shift; fi ;; # Define output directory.
            -e | --encrypt) OPTION_ENCRYPT="true"; vbm "DEBUG:Encrypted output mode enabled.";;
            -r | --recipient) # Add 'age' recipient via public key string
            -o | --output) if [ -d "$2" ]; then DIROUT="$2"; vbm "DEBUG:DIROUT:$DIROUT"; shift; fi ;; # Define output directory.
            -e | --encrypt) OPTION_ENCRYPT="true"; vbm "DEBUG:Encrypted output mode enabled.";;
            -r | --recipient) # Add 'age' recipient via public key string
-               recPubKeys+=("$2"); shift;;
+               recPubKeys+=("$2"); vbm "pubkey added:""$2"; shift;;
+           -c | --compress) OPTION_COMPRESS="true"; vbm "DEBUG:Compressed output mode enabled.";;
            *) echoerr "ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options.
        esac
        shift
            *) echoerr "ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options.
        esac
        shift
@@ -275,14 +280,42 @@ main() {
        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 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
-                   yell "ERROR:Exit code ""$?"". Invalid recipient pubkey string. Exiting."; exit 1; fi
+               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
            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
            yell "ERROR:Encryption enabled but \"age\" not found. Exiting."; exit 1;
        fi
+    else
+       CMD_ENCRYPT="";
+       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="";
+       CMD_COMPRESS_SUFFIX="";
+       vbm "DEBUG:Compression not enabled."
     fi
     
     fi
     
+    
     if checkapp gpspipe && checkdir "$DIROUT"; then
        
        # # Set script lifespan to end at start of next day
     if checkapp gpspipe && checkdir "$DIROUT"; then
        
        # # Set script lifespan to end at start of next day
@@ -312,15 +345,18 @@ main() {
            ((debugCounter++))
            # Determine output file paths (time is start of buffer period)
            FILEOUT_BASENAME="$(dateTimeShort)""--P""$bufferTTL""S..""$SCRIPT_HOSTNAME""_location" ; # ISO-8601 YYYYmmddTHHMMSS+zzP[$bufferTTL]S
            ((debugCounter++))
            # Determine output file paths (time is start of buffer period)
            FILEOUT_BASENAME="$(dateTimeShort)""--P""$bufferTTL""S..""$SCRIPT_HOSTNAME""_location" ; # ISO-8601 YYYYmmddTHHMMSS+zzP[$bufferTTL]S
-           FILEOUT_NMEA="$FILEOUT_BASENAME".nmea ;
-           FILEOUT_GPX="$FILEOUT_BASENAME".gpx ;
-           FILEOUT_KML="$FILEOUT_BASENAME".kml ;
+           FILEOUT_NMEA="$FILEOUT_BASENAME".nmea"$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX" ;
+           FILEOUT_GPX="$FILEOUT_BASENAME".gpx"$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX" ;
+           FILEOUT_KML="$FILEOUT_BASENAME".kml"$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX" ;
            # Fill buffer
            buffer="$(timeout "$bufferTTL""s" gpspipe -r)"; # Record gpspipe nmea data to buffer for bufferTTL seconds
            # Process and save buffers
            # Fill buffer
            buffer="$(timeout "$bufferTTL""s" gpspipe -r)"; # Record gpspipe nmea data to buffer for bufferTTL seconds
            # Process and save buffers
-           echo "$buffer"                                     > "$DIROUT"/"$FILEOUT_NMEA" & # Save NMEA format
-           echo "$buffer" | gpsbabel -i nmea -f - -o gpx -F - > "$DIROUT"/"$FILEOUT_GPX"  & # Save GPX format
-           echo "$buffer" | gpsbabel -i nmea -f - -o kml -F - > "$DIROUT"/"$FILEOUT_KML"  & # Save KML format
+           CMD_NMEA="                                   $CMD_COMPRESS $CMD_ENCRYPT > $DIROUT/$FILEOUT_NMEA"; vbm "DEBUG:CMD_NMEA:$CMD_NMEA"
+           CMD_GPX="  gpsbabel -i nmea -f - -o gpx -F - $CMD_COMPRESS $CMD_ENCRYPT > $DIROUT/$FILEOUT_GPX";  vbm "DEBUG:CMD_GPX:$CMD_GPX"
+           CMD_KML="  gpsbabel -i nmea -f - -o kml -F - $CMD_COMPRESS $CMD_ENCRYPT > $DIROUT/$FILEOUT_KML";  vbm "DEBUG:CMD_KML:$CMD_KML"
+           echo "$buffer" $CMD_NMEA & # Save NMEA format
+           echo "$buffer" $CMD_GPX & # Save GPX format
+           echo "$buffer" $CMD_KML & # Save KML format
            vbm "DEBUG:Completed buffer session $debugCounter ." 1>&2;
            # Reset buffer and filenames
            unset buffer FILEOUT_BASENAME FILEOUT_NMEA FILEOUT_GPX FILEOUT_KML;
            vbm "DEBUG:Completed buffer session $debugCounter ." 1>&2;
            # Reset buffer and filenames
            unset buffer FILEOUT_BASENAME FILEOUT_NMEA FILEOUT_GPX FILEOUT_KML;