From f62db1b46f655652cfde990c5b03862618ff7932 Mon Sep 17 00:00:00 2001
From: "W. Trevor King" <wking@drexel.edu>
Date: Thu, 29 Mar 2012 22:24:44 -0400
Subject: [PATCH] Restructure cache-file.sh, adding the -r option for restoring
 files.

Because sometimes you press the wrong button ;).
---
 posts/cache-file.mdwn          |  9 ++--
 posts/cache-file/cache-file.sh | 94 +++++++++++++++++++++++-----------
 2 files changed, 69 insertions(+), 34 deletions(-)

diff --git a/posts/cache-file.mdwn b/posts/cache-file.mdwn
index eae2d5e..72533e6 100644
--- a/posts/cache-file.mdwn
+++ b/posts/cache-file.mdwn
@@ -4,12 +4,13 @@ and add
 
     -1 "cache-file.sh -l saves"
     -2 "cache-file.sh trash"
+    -2 "cache-file.sh -r"
 
 to `~/.pqivrc`, you can hard link the current picture to
-`.cache-saves/` or by pressing 1, or move the current picture to
-`.cache-trash/` by pressing 2.  The `.cache-*/` directory is in the
-same directory as the image file, and will be created if it doesn't
-exist.
+`.cache-saves/` or by pressing 1, move the current picture to
+`.cache-trash/` by pressing 2, or restore the current picture from the
+caches by pressing 3.  The `.cache-*/` directory is in the same
+directory as the image file, and will be created if it doesn't exist.
 
 Not very complicated, but useful for quickly removing
 almost-duplicates, blurry pictures, etc.
diff --git a/posts/cache-file/cache-file.sh b/posts/cache-file/cache-file.sh
index b412d72..7c1ab96 100755
--- a/posts/cache-file/cache-file.sh
+++ b/posts/cache-file/cache-file.sh
@@ -20,41 +20,75 @@
 #
 #
 # usage: cache-file.sh [-l] TAG FILE
+#        cache-file.sh [-r] FILE
 
-PREFIX=".cache-"
+PREFIX='.cache-'
+FILE_DIR=''
+CACHE_DIR=''
 
-if [ "$1" == '-l' ]; then
-	CMD='cp -l'
-	VERB='hard-linked'
-	shift
-else
-	CMD='mv'
-	VERB='moved'
-fi
+function check_file()
+{
+	local FILE="$1"
+	if [ ! -e "${FILE}" ]; then
+		echo "'${FILE}' doesn't exist" >&2
+		exit 1
+	fi
+	FILE_DIR=$(dirname "${FILE}")
+	if [ ! -d "${FILE_DIR}" ]; then
+		echo "'${FILE}' not in a directory" >&2
+		exit 1
+	fi
+}
 
-TAG="$1"
-FILE="$2"
+function check_cache()
+{
+	local TAG="$1"
+	local FILE="$2"
+	CACHE_DIR="${FILE_DIR}/${PREFIX}${TAG}"
 
-if [ ! -e "$FILE" ]; then
-	echo "'$FILE' doesn't exist" >&2
-	exit 1
-fi
-FILE_DIR=$(dirname $FILE)
-if [ ! -d "$FILE_DIR" ]; then
-	echo "'$FILE' not in a directory" >&2
-	exit 1
-fi
+	if [ ! -d "${CACHE_DIR}" ]; then
+		if [ -e "${CACHE_DIR}" ]; then
+			echo "'${CACHE_DIR}' exists, but is not a directory"
+			exit 1
+		fi
+		mkdir "${CACHE_DIR}"
+	fi
+}
 
-CACHE_DIR="$FILE_DIR/$PREFIX$TAG"
+function move()
+{
+	local TAG="$1"
+	local FILE="$2"
+	check_file "${FILE}"
+	check_cache "${TAG}" "${FILE}"
+	echo "move '${FILE}' to '${CACHE_DIR}'"
+	mv "${FILE}" "${CACHE_DIR}"
+}
 
-if [ ! -d "$CACHE_DIR" ]; then
-	if [ -e "$CACHE_DIR" ]; then
-		echo "'$CACHE_DIR' exists, but is not a directory"
-		exit 1
-	fi
-	mkdir "$CACHE_DIR"
-fi
+function link()
+{
+	local TAG="$1"
+	local FILE="$2"
+	check_file "${FILE}"
+	check_cache "${TAG}" "${FILE}"
+	echo "hardlink '${FILE}' from '${CACHE_DIR}'"
+	cp -l "${FILE}" "${CACHE_DIR}"
+}
 
-$CMD "$FILE" "$CACHE_DIR"
+function restore()
+{
+	local FILE="$1"
+	for CACHED_PATH in $(ls "${PREFIX}"*"/${FILE}"); do
+		echo "restore '${FILE}' from '${CACHED_PATH}'"
+		mv "${CACHED_PATH}" "${FILE}"
+	done
+}
 
-echo "$VERB '$FILE' to '$CACHE_DIR'"
+
+if [ "$1" == '-l' ]; then
+	link "$2" "$3"
+elif [ "$1" == '-r' ]; then
+	restore "$2"
+else
+	move "$1" "$2"
+fi
-- 
2.26.2