From 53149a63440a60e798095044a205c62f4c997722 Mon Sep 17 00:00:00 2001
From: Elliu <elliu@hashi.re>
Date: Wed, 17 Aug 2022 16:18:28 +0200
Subject: [PATCH] WIP: WIP

---
 stpkg | 59 ++++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 34 insertions(+), 25 deletions(-)

diff --git a/stpkg b/stpkg
index 9296b5d..287c650 100755
--- a/stpkg
+++ b/stpkg
@@ -775,10 +775,14 @@ __pack() {
     # Check packs
     local DEST_FOLDER="$STPKG_BASE/packs/$PACK_NAME"
     [ -d "$DEST_FOLDER" ] \
-        && { { warn "Delete old folder content for pack $PACK_NAME" ; rm "$DEST_FOLDER"/*; } \
-           || die "Failed to remove old folder pack content"; } \
+        && { warn "Old folder for pack $PACK_NAME exists, using checksums to reupload necessary stickers"; } \
         || { { info "Create folder for pack $PACK_NAME" ; mkdir "$DEST_FOLDER"; } \
            || die "Failed to create destination folder for pack '$PACK_NAME'"; }
+    #[ -d "$DEST_FOLDER" ] \
+    #    && { { warn "Delete old folder content for pack $PACK_NAME" ; rm "$DEST_FOLDER"/*; } \
+    #       || die "Failed to remove old folder pack content"; } \
+    #    || { { info "Create folder for pack $PACK_NAME" ; mkdir "$DEST_FOLDER"; } \
+    #       || die "Failed to create destination folder for pack '$PACK_NAME'"; }
     local DEST_INDEX="$DEST_FOLDER/$PACK_NAME.json"
     touch $DEST_INDEX
     [ ! -f $DEST_INDEX ] && die "Failed to create the index file '$DEST_INDEX'"
@@ -792,42 +796,46 @@ __pack() {
     local FIRST_IN_ARRAY=""
 
     # Check checksums
-    local SUM_FILE=".${PACK_NAME}.checksums"
+    local SUM_FILE="$DEST_FOLDER/.$PACK_NAME.checksums"
     if [ ! -f "$SUM_FILE" ]; then
         touch $SUM_FILE
     fi
-    local -a newFiles
-    local -A hashes unchangedFiles updatedFiles deletedFiles
+    local -A hashes unchangedFiles updatedFiles deletedFiles skipFiles
     local TEMP_CHECK=`mktemp --suffix=.stpkg`
     md5sum -c $SUM_FILE &>/dev/null >$TEMP_CHECK
     while read -r _hash _file; do
         hashes["$_file"]="$_hash"
     done < $SUM_FILE
-    newFiles=(*)
 
-    for i in "`cat $TEMP_CHECK | grep ": OK$" | sed "s/: OK$//g"`"; do
-        unchangedFiles["$i"]=("${hashes["$i"]}")
+    for i in `cat $TEMP_CHECK | grep ': OK$' | sed 's/\(.*\): OK/\1/'`; do
+        if [ "x$i" = "x" ]; then continue; fi
+        unchangedFiles["$i"]="${hashes["$i"]}"
         unset hashes["$i"]
-        newFiles="${newFiles[@]/$i}"
+        skipFiles[$i]=""
     done
-    for i in "`cat $TEMP_CHECK | grep ": FAILED$" | sed "s/: FAILED$//g"`"; do
-        updatedFiles["$i"]=("${hashes["$i"]}")
+    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"]
-        newFiles="${newFiles[@]/$i}"
+        skipFiles[$i]=""
     done
-    for i in "`cat $TEMP_CHECK | grep ": FAILED open or read$" | sed "s/: FAILED open or read$//g"`"; do
-        deletedFiles["$i"]=("${hashes["$i"]}")
+    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
+        deletedFiles["$i"]="${hashes["$i"]}"
         unset hashes["$i"]
-        newFiles="${newFiles[@]/$i}"
+        skipFiles[$i]=""
     done
 
-    for FILE in "${!newFiles[@]}"; do
+    for FILE in *; do
         # Add a ',' only if it's not the first in the array
         echo -n "$FIRST_IN_ARRAY" >> $DEST_INDEX
 
-        if [[ $(md5sum $FILE) = $(cat $SUM_FILE | grep ) ]]; then
-        else
-            md5sum $FILE >> $SUM_FILE
+        if [[ -v skipFiles[$FILE] ]]; then continue; fi
+
+        echo "Processing new file $FILE"
+
+        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
 
         local ST_JSON=""
@@ -836,10 +844,10 @@ __pack() {
 
         # For the report
         [ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName MXC Type Width Height"
-        local FIRST_IN_ARRAY=","
+        FIRST_IN_ARRAY=","
         echo -e "$ST_JSON" | jq -r '[.body, .url, .info.mimetype, .info.w, .info.h]|join(" ")'
 
-        # TODO: add hashes
+        md5sum $FILE >> $SUM_FILE
 
         progress_reset
     done | column -t
@@ -847,7 +855,7 @@ __pack() {
     for FILE in "${!unchangedFiles[@]}"; do
         echo -n "$FIRST_IN_ARRAY" >> $DEST_INDEX
         [ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName MXC Type Width Height"
-        local FIRST_IN_ARRAY=","
+        FIRST_IN_ARRAY=","
         #echo -e "$ST_JSON" | jq -r '[.body, .url, .info.mimetype, .info.w, .info.h]|join(" ")'
         echo "$FILE unchanged"
     done | column -t
@@ -855,14 +863,14 @@ __pack() {
     for FILE in "${!updatedFiles[@]}"; do
         echo -n "$FIRST_IN_ARRAY" >> $DEST_INDEX
         [ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName MXC Type Width Height"
-        local FIRST_IN_ARRAY=","
+        FIRST_IN_ARRAY=","
         echo "$FILE updated"
     done | column -t
 
     for FILE in "${!deletedFiles[@]}"; do
         echo -n "$FIRST_IN_ARRAY" >> $DEST_INDEX
         [ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName MXC Type Width Height"
-        local FIRST_IN_ARRAY=","
+        FIRST_IN_ARRAY=","
         #echo -e "$ST_JSON" | jq -r '[.body, .url, .info.mimetype, .info.w, .info.h]|join(" ")'
         echo "$FILE deleted, but kept in the pack"
     done | column -t
@@ -878,9 +886,10 @@ __pack() {
     #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" \) -exec rm {} \;
+    find $DEST_FOLDER -type f \( ! -name "preview.png" -and ! -name "$PACK_NAME.json" -and ! -name '.*.checksums' \) -exec rm {} \;
 
     echo -n "]}" >> $DEST_INDEX
+    cat $DEST_INDEX
     cat $DEST_INDEX | jq | sponge $DEST_INDEX
     info "Pack created, you can now commit it and create a MR to share it with other users"
 }
-- 
GitLab