Handle some extra cases (dirs being empty or not existing)
authorCameron Ball <cameron@moodle.com>
Thu, 11 Apr 2019 02:45:15 +0000 (10:45 +0800)
committerCameron Ball <cameron@moodle.com>
Thu, 11 Apr 2019 02:45:15 +0000 (10:45 +0800)
SongSync.sh

index 30304ef..c8f96be 100755 (executable)
@@ -91,6 +91,16 @@ if [ "$1" = "apply" ]; then
 
         echo "Moving ${removal_path} to /tmp/${removes[$removal_path]}"
         mv "${path_to_songs}/${removal_path}" "/tmp/${removes[$removal_path]}"
+        crc32="$(echo -n "/Songs/${removal_path}/" | gzip -c | tail -c8 | hexdump -n4 -e '"%u"')"
+        rm "${path_to_song_cache}/${crc32}"
+
+        pack_dir=$(echo "$removal_path" | cut -sd / -f1)
+        count="$( find "${path_to_songs}/${pack_dir}" -mindepth 1 -maxdepth 1 | wc -l )"
+        if [ $count -eq 0 ] ; then
+            echo "${pack_dir} is now empty, deleting"
+            rmdir "${path_to_songs}/${pack_dir}"
+        fi
+
     done
 
     for add_path in "${!adds[@]}"; do
@@ -101,6 +111,11 @@ if [ "$1" = "apply" ]; then
             rm "${path_to_song_cache}/${crc32}"
         fi
 
+        if ! [ -d "${path_to_songs}/${add_path}" ]; then
+            echo "Creating dir for: ${add_path}"
+            mkdir -p "${path_to_songs}/${pack_dir}"
+        fi
+
         # The file existed previously, just move that over.
         if [ -d "/tmp/${adds[$add_path]}" ]; then
             echo "Moving /tmp/${adds[$add_path]} to ${path_to_songs}/${add_path}"
@@ -108,8 +123,6 @@ if [ "$1" = "apply" ]; then
             continue
         fi
 
-        echo "Creating dir for: ${add_path}"
-        mkdir -p "${path_to_songs}/${pack_dir}"
         echo "Downloading files"
         escaped_add_path="/mnt/media/Simfiles/"$(echo "$add_path" | sed 's/ /\\\\\\ /g' | sed "s/'/\\\\\\\\\\\'/g" | sed "s/(/\\\\\\\\\\\\(/g" | sed "s/)/\\\\\\\\\\\\\\)/g" | sed "s/&/\\\\\\\\\\\&/g" | sed "s/\`/\\\\\\\\\\\\\`/g")
         scp_command="scp -r groovenet@cameron1729.xyz:$escaped_add_path \"${path_to_songs}/${pack_dir}\""