From 9da6db0131772211f19e54abd50e3de2918035d8 Mon Sep 17 00:00:00 2001
From: Elliu <elliu@hashi.re>
Date: Sat, 8 Oct 2022 02:42:57 +0200
Subject: [PATCH] Properly delete deleted stickers

---
 stpkg | 54 ++++++++++++++++++++++--------------------------------
 1 file changed, 22 insertions(+), 32 deletions(-)

diff --git a/stpkg b/stpkg
index 0a1a554..26a8b69 100755
--- a/stpkg
+++ b/stpkg
@@ -757,7 +757,7 @@ __pack() {
     if [ ! -f "$SUM_FILE" ]; then
         touch $SUM_FILE
     fi
-    local -A hashes unchangedFiles updatedFiles deletedFiles skipFiles
+    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
@@ -766,32 +766,25 @@ __pack() {
 
     for i in `cat $TEMP_CHECK | grep ': OK$' | sed 's/\(.*\): OK/\1/'`; do
         if [ "x$i" = "x" ]; then continue; fi
-        echo "$FILE is OK"
         if [ "x$STPKG_REUPLOAD_SAME_FILE" = "xyes" ]; then
-            echo "$FILE is updated"
             updatedFiles["$i"]="${hashes["$i"]}"
         else
-            echo "$FILE is unchanged"
             unchangedFiles["$i"]="${hashes["$i"]}"
         fi
-        echo "unset $FILE"
-        unset hashes["$i"]
-        skipFiles[$i]=""
     done
     for i in `cat $TEMP_CHECK | grep ': FAILED$' | sed 's/\(.*\): FAILED/\1/'`; do
         if [ "x$i" = "x" ]; then continue; fi
         updatedFiles["$i"]="${hashes["$i"]}"
-        unset hashes["$i"]
-        skipFiles[$i]=""
     done
     for i in `cat $TEMP_CHECK | grep ': FAILED open or read$' | sed 's/\(.*\): FAILED open or read/\1/'`; do
-        if [ "x$i" = "x" ]; then continue; fi
+        if [ "x$i" == "x" ]; then continue; fi
         deletedFiles["$i"]="${hashes["$i"]}"
-        unset hashes["$i"]
-        skipFiles[$i]=""
+        echo "add $i to deleted"
     done
 
     local ST_JSON=""
+    echo "----- Pack -----"
+{
     for FILE in *; do
         # Add a ',' only if it's not the first in the array
         echo -n "$FIRST_IN_ARRAY" >> $TEMP_JSON
@@ -801,18 +794,12 @@ __pack() {
         if [[ -v unchangedFiles[$FILE] ]]; then
             STICKER_STATUS="Unchanged"
             ST_JSON=`jq ".stickers[] | select(.body == \"$STICKER_NAME\")" < "$DEST_INDEX"`
-
-        elif [[ -v deletedFiles[$FILE] ]]; then
-            STICKER_STATUS="Deleted"
-            ST_JSON=`jq ".stickers[] | select(.body == \"$STICKER_NAME\")" < "$DEST_INDEX"`
-            #TODO: remove from checksum?
         else
             if [[ -v updatedFiles[$FILE] ]]; then
-                STICKER_STATUS="Updated"
-                #TODO: deleted old checksum
+                STICKER_STATUS="`__yellow Updated`"
                 sed -i "/[0-9a-f]\{32\}  $FILE/d" $SUM_FILE
             else
-                STICKER_STATUS="New"
+                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
                     die "New file is replacing existing file, that should not be happening"
                 fi
@@ -829,24 +816,27 @@ __pack() {
         FIRST_IN_ARRAY=","
         local report="`echo -ne "$ST_JSON" | jq -r '[.body, .url, .info.mimetype, .info.w, .info.h]|join(" ")'` $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"`
+        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"
+    done
+}   | column -t
+    echo "----------------"
 
-    done | column -t
-    # Update the sum file:
-    #   Keep deleted but kept
-    #   Remove deleted and deleted
-    #   Update updated
-    #   Keep unchanged
-    #   Add new
-
-    # Create the preview
-    #montage "$DEST_FOLDER/*.{png,gif}" -background none "$DEST_FOLDER/preview.png"
-    #echo -ne "# $PACK_NAME\n![Preview of $PACK_NAME](preview.png)" > "$DEST_FOLDER/README.md"
     echo -ne "# $PACK_NAME\n(no preview)" > "$DEST_FOLDER/README.md"
     find $DEST_FOLDER -type f \( ! -name "preview.png" -and ! -name "$PACK_NAME.json" -and ! -name '.*.checksums' \) -exec rm {} \;
 
     echo -n "]}" >> $TEMP_JSON
     cat $TEMP_JSON | jq > $DEST_INDEX
-    echo $TEMP_JSON
+
+    # Sort checksums to get better diffs
+    cat $SUM_FILE | sort | sponge $SUM_FILE
+
     info "Pack created, you can now commit it and create a MR to share it with other users"
 }
 
-- 
GitLab