From 1708ca7d6df3e0b71addbf92b37219d698acc7f8 Mon Sep 17 00:00:00 2001
From: Elliu <elliu@hashi.re>
Date: Sun, 26 Feb 2023 21:39:41 +0100
Subject: [PATCH] stpkg: don't fail on spaces in filename

---
 stpkg | 50 ++++++++++++++++++++++++++++----------------------
 1 file changed, 28 insertions(+), 22 deletions(-)

diff --git a/stpkg b/stpkg
index 1995748..5bdfaba 100755
--- a/stpkg
+++ b/stpkg
@@ -205,7 +205,7 @@ __get_dest_dimensions() {
 file_get_type() { xdg-mime query filetype "$1"; }
 file_get_ext() { xdg-mime query filetype "$1" | sed 's+^.*/++'; }
 file_get_ext_from_type() { echo "$1" | sed 's+^.*/++'; }
-file_get_name() { echo -n "$1" | cut -f1 -d'.'; }
+file_get_name() { echo -n "$1" | sed 's/.[^.]*$//'; }
 
 __upload_file() {
     local TYPE=$1   # The type of file (png/gif)
@@ -781,72 +781,78 @@ __pack() {
     local -A hashes unchangedFiles updatedFiles deletedFiles
     local TEMP_CHECK=`mktemp --suffix=.stpkg`
     md5sum -c $SUM_FILE &>/dev/null >$TEMP_CHECK
-    while read -r _hash _file; do
+    while read -r _line; do
+        _hash="`echo "$_line" | sed 's+^\([^ ]*\)  \(.*\)$+\1+'`"
+        _file="`echo "$_line" | sed 's+^\([^ ]*\)  \(.*\)$+\2+'`"
         hashes["$_file"]="$_hash"
     done < $SUM_FILE
 
-    for i in `cat $TEMP_CHECK | grep ': OK$' | sed 's/\(.*\): OK/\1/'`; do
+    while read -r i; do
         if [ "x$i" = "x" ]; then continue; fi
         if [ "x$STPKG_REUPLOAD_SAME_FILE" = "xyes" ]; then
             updatedFiles["$i"]="${hashes["$i"]}"
         else
             unchangedFiles["$i"]="${hashes["$i"]}"
         fi
-    done
-    for i in `cat $TEMP_CHECK | grep ': FAILED$' | sed 's/\(.*\): FAILED/\1/'`; do
+    done < <(cat $TEMP_CHECK | grep ': OK$' | sed 's/\(.*\): OK/\1/')
+
+    while read -r i; do
         if [ "x$i" = "x" ]; then continue; fi
         updatedFiles["$i"]="${hashes["$i"]}"
-    done
-    for i in `cat $TEMP_CHECK | grep ': FAILED open or read$' | sed 's/\(.*\): FAILED open or read/\1/'`; do
+    done < <(cat $TEMP_CHECK | grep ': FAILED$' | sed 's/\(.*\): FAILED/\1/')
+
+    while read -r i; do
         if [ "x$i" == "x" ]; then continue; fi
         deletedFiles["$i"]="${hashes["$i"]}"
         echo "add $i to deleted"
-    done
+    done < <(cat $TEMP_CHECK | grep ': FAILED open or read$' | sed 's/\(.*\): FAILED open or read/\1/')
 
     local ST_JSON=""
     echo "----- Pack -----"
+
+    local COLUMN_SEP="|_|_|"
 {
     for FILE in *; do
         # Add a ',' only if it's not the first in the array
         echo -n "$FIRST_IN_ARRAY" >> $TEMP_JSON
         local STICKER_STATUS=""
-        local STICKER_NAME=$(file_get_name "$FILE")
+        local STICKER_NAME="`file_get_name "$FILE"`"
 
-        if [[ -v unchangedFiles[$FILE] ]]; then
+        if [[ "x${unchangedFiles[$FILE]}" != "x" ]]; then
             STICKER_STATUS="Unchanged"
-            ST_JSON=`jq ".stickers[] | select(.body == \"$STICKER_NAME\")" < "$DEST_INDEX"`
+            ST_JSON="`jq --arg STICKER_NAME "$STICKER_NAME" '.stickers[] | select(.body == $STICKER_NAME)' < "$DEST_INDEX"`"
         else
-            if [[ -v updatedFiles[$FILE] ]]; then
+            if [[ "x${updatedFiles[$FILE]}" != "x" ]]; then
                 STICKER_STATUS="`__yellow Updated`"
                 sed -i "/[0-9a-f]\{32\}  $FILE/d" $SUM_FILE
             else
                 STICKER_STATUS="`__green New`"
-                if [[ "`md5sum $FILE |  sed -e 's/\([0-9a-f]\{32\}\)  .*$/\1/'`" = "$(cat $SUM_FILE | grep "$FILE" | sed -e 's/\([0-9a-f]\{32\}\)  .*$/\1/')" ]]; then
+                if [[ `md5sum "$FILE" |  sed -e 's/\([0-9a-f]\{32\}\)  .*$/\1/'` = `cat $SUM_FILE | grep "$FILE" | sed -e 's/\([0-9a-f]\{32\}\)  .*$/\1/'` ]]; then
                     die "New file is replacing existing file, that should not be happening"
                 fi
             fi
 
-            process_sticker_file $FILE ST_JSON
-            md5sum $FILE >> $SUM_FILE
+            process_sticker_file "$FILE" ST_JSON
+            md5sum "$FILE" >> $SUM_FILE
         fi
 
         echo "$ST_JSON" >> "$TEMP_JSON"
 
         # For the report
-        [ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName MXC Type Width Height Status"
+        [ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName${COLUMN_SEP}MXC${COLUMN_SEP}Type${COLUMN_SEP}Width${COLUMN_SEP}Height${COLUMN_SEP}Status${COLUMN_SEP}"
         FIRST_IN_ARRAY=","
-        local report="`echo -ne "$ST_JSON" | jq -r '[.body, .url, .info.mimetype, .info.w, .info.h]|join(" ")'` $STICKER_STATUS"
+        local report="`echo -ne "$ST_JSON" | jq -r --arg COLUMN_SEP "$COLUMN_SEP" '[.body, .url, .info.mimetype, .info.w, .info.h]|join($COLUMN_SEP)'`${COLUMN_SEP}${STICKER_STATUS}"
         echo "$report"
     done
 
     for i in "${!deletedFiles[@]}"; do
-        local STICKER_NAME=$(file_get_name "$i")
-        ST_JSON=`jq ".stickers[] | select(.body == \"$STICKER_NAME\")" < "$DEST_INDEX"`
+        local STICKER_NAME="$(file_get_name "$i")"
+        ST_JSON="`jq --arg STICKER_NAME "$STICKER_NAME" '.stickers[] | select(.body == $STICKER_NAME)' < "$DEST_INDEX"`"
         sed -i "/[0-9a-f]\{32\}  $i/d" $SUM_FILE
-        local report="`echo -ne "$ST_JSON" | jq -r '[.body, .url, .info.mimetype, .info.w, .info.h]|join(" ")'` `__red Deleted`"
-        echo "$report"
+        local report="`echo -ne "$ST_JSON" | jq -r --arg COLUMN_SEP "$COLUMN_SEP" '[.body, .url, .info.mimetype, .info.w, .info.h]|join($COLUMN_SEP)'`$COLUMN_SEP`__red Deleted`"
+        echo -n "$report"
     done
-}   | column -t
+}   | column -t -s "$COLUMN_SEP"
     echo "----------------"
 
     echo -ne "# $PACK_NAME\n(no preview)" > "$DEST_FOLDER/README.md"
-- 
GitLab