diff moefetch.sh @ 187:efd957294c8c

refactoring. cleanup. etc. also initial support for --noclean
author edhoprima@gmail.com <edhoprima@gmail.com>
date Sun, 28 Jun 2009 13:57:18 +0000
parents 197266fefbb4
children 9bf4eabbf0a9
line wrap: on
line diff
--- a/moefetch.sh	Sun Jun 28 13:13:48 2009 +0000
+++ b/moefetch.sh	Sun Jun 28 13:57:18 2009 +0000
@@ -106,7 +106,7 @@
 	fi
 	MD5_COMMAND=`echo ${MD5} | cut -d' ' -f1`
 	# basic tools
-	COMMANDS="cut sed wc wget xsltproc xargs rm mkdir chown comm grep ${MD5_COMMAND}"
+	COMMANDS="cut sed wc wget xsltproc xargs rm mkdir chown comm grep date ${MD5_COMMAND}"
 	for COMMAND in ${COMMANDS}
 	do
 		COMMAND_CHECK=`command -v "${COMMAND}"`
@@ -145,41 +145,50 @@
 	#
 }
 
+# Do some cleanup
+Cleanup_Repository() {
+
+	# THE FILES
+	
+	# current dir: ${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}
+	TRASH_DIR="${SITE_DIR}-${TARGET_DIR}-`date -u +%Y%m%d-%H.%M`"
+	mkdir -p "${BASE_DIR}/trash/${TRASH_DIR}" || Err_Fatal "Unable to create trash folder"
+	for TRASH in "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}/*"
+	do
+		ISTRASH=
+		if [ -d "${TRASH}" ]; then
+			ISTRASH=1
+		else
+			[ "`echo \"${FILE}\" | sed -e 's/.*\/\([\^\/]*\)/\1/g;s/\([0-9a-f]\{32\}.*\)//g' | grep -v ^$`" ] && ISTRASH=1
+		fi
+		if [ "${ISTRASH}" ]; then
+			mv -f "${TRASH}" "${BASE_DIR}/trash/${TRASH_DIR}" || Err_Fatal "Error deleting files"
+			echo "Moved ${TRASH} to ${BASE_DIR}/trash/${TRASH_DIR}"
+		fi
+	done
+	rmdir "${BASE_DIR}/trash/${TRASH_DIR}" 2>/dev/null
+}
+
 # check files correctness
 Check_Files() {
 	if [ ! "${ISNEW}" ]; then
+		[ "${NOCLEAN}" ] || Cleanup_Repository
 		echo "Checking for errors..."
-		# THE FILES
-		
-		# current dir: ${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}
 		cd "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}"
-		TRASH_DIR="${SITE_DIR}-${TARGET_DIR}-`date -u +%Y%m%d-%H.%M`"
-		mkdir -p "${BASE_DIR}/trash/${TRASH_DIR}"
-		# FIXME FIXME FIXME FIXME FIXME FIXME 
-		for TRASH in *
-		do
-			if [ -d "${TRASH}" ]; then
-				mv -f "${TRASH}" "${BASE_DIR}/trash/${TRASH_DIR}" || Err_Fatal "Error deleting files"
-				echo "Moved ${TRASH} to ${BASE_DIR}/trash/${TRASH_DIR}"
-			else
-				TRASH="`echo ${TRASH} | sed -e 's/\([0-9a-f]\{32\}.*\)//g' | grep -v ^$`"
-				if [ "${TRASH}" ]; then
-					mv -f "${TRASH}" "${BASE_DIR}/trash/${TRASH_DIR}" || Err_Fatal "Error deleting files"
-					echo "Moved ${TRASH} to ${BASE_DIR}/trash/${TRASH_DIR}"
-				fi
-			fi
-		done
-		[ "`ls "${BASE_DIR}/trash/${TRASH_DIR}"`" ] || rmdir "${BASE_DIR}/trash/${TRASH_DIR}"
 		printf "" > "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error"
 		for FILE in *
 		do
-			if [ "`${MD5} "${FILE}" | cut -d ' ' -f1 -`" != "`echo "${FILE}" | cut -d '.' -f1`" ]
-			then
-				echo
-				echo "${FILE}" >> "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error"
-				echo "Error: ${FILE}"
+			if [ "`echo \"${FILE}\" | sed -e 's/\([0-9a-f]\{32\}.*\)//g' | grep -v ^$`" ]; then
+				echo "Not a valid danbooru file: ${FILE}"
+			else
+				if [ "`${MD5} "${FILE}" | cut -d ' ' -f1 -`" != "`echo "${FILE}" | cut -d '.' -f1`" ]
+				then
+					echo
+					echo "${FILE}" >> "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error"
+					echo "Error: ${FILE}"
+				fi
+				printf "."
 			fi
-			printf "."
 		done
 		echo
 		TOTAL_ERROR=`echo \`wc -l < "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error"\``
@@ -274,7 +283,7 @@
 	# Get base folder - default, current folder or fallback to ${HOME}
 	[ "${BASE_DIR}"	] || BASE_DIR="${PWD}"
 	[ "${BASE_DIR}" ] || BASE_DIR="{$HOME}"
-	[ "`echo ${BASE_DIR} | cut -c1 | grep \/`" ] || BASE_DIR="/${BASE_DIR}"	
+	[ "`echo \"${BASE_DIR}\" | cut -c1 | grep \/`" ] || BASE_DIR="/${BASE_DIR}"	
 
 	echo "Tags: ${TAGS}"
 	# slash is not wanted for folder name