comparison 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
comparison
equal deleted inserted replaced
186:197266fefbb4 187:efd957294c8c
104 *) Fatal_Err "No known md5 tool for this platform. Please specify manually" 104 *) Fatal_Err "No known md5 tool for this platform. Please specify manually"
105 esac 105 esac
106 fi 106 fi
107 MD5_COMMAND=`echo ${MD5} | cut -d' ' -f1` 107 MD5_COMMAND=`echo ${MD5} | cut -d' ' -f1`
108 # basic tools 108 # basic tools
109 COMMANDS="cut sed wc wget xsltproc xargs rm mkdir chown comm grep ${MD5_COMMAND}" 109 COMMANDS="cut sed wc wget xsltproc xargs rm mkdir chown comm grep date ${MD5_COMMAND}"
110 for COMMAND in ${COMMANDS} 110 for COMMAND in ${COMMANDS}
111 do 111 do
112 COMMAND_CHECK=`command -v "${COMMAND}"` 112 COMMAND_CHECK=`command -v "${COMMAND}"`
113 [ "${COMMAND_CHECK}" ] || Err_Fatal "${COMMAND} doesn't exist in ${PATH}" 113 [ "${COMMAND_CHECK}" ] || Err_Fatal "${COMMAND} doesn't exist in ${PATH}"
114 done 114 done
143 touch "${SITE_DIR}-${TARGET_DIR}-${i}" || Fatal_Err "Error creating ${TARGET_DIR}-${i}. This shouldn't happen" 143 touch "${SITE_DIR}-${TARGET_DIR}-${i}" || Fatal_Err "Error creating ${TARGET_DIR}-${i}. This shouldn't happen"
144 done 144 done
145 # 145 #
146 } 146 }
147 147
148 # Do some cleanup
149 Cleanup_Repository() {
150
151 # THE FILES
152
153 # current dir: ${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}
154 TRASH_DIR="${SITE_DIR}-${TARGET_DIR}-`date -u +%Y%m%d-%H.%M`"
155 mkdir -p "${BASE_DIR}/trash/${TRASH_DIR}" || Err_Fatal "Unable to create trash folder"
156 for TRASH in "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}/*"
157 do
158 ISTRASH=
159 if [ -d "${TRASH}" ]; then
160 ISTRASH=1
161 else
162 [ "`echo \"${FILE}\" | sed -e 's/.*\/\([\^\/]*\)/\1/g;s/\([0-9a-f]\{32\}.*\)//g' | grep -v ^$`" ] && ISTRASH=1
163 fi
164 if [ "${ISTRASH}" ]; then
165 mv -f "${TRASH}" "${BASE_DIR}/trash/${TRASH_DIR}" || Err_Fatal "Error deleting files"
166 echo "Moved ${TRASH} to ${BASE_DIR}/trash/${TRASH_DIR}"
167 fi
168 done
169 rmdir "${BASE_DIR}/trash/${TRASH_DIR}" 2>/dev/null
170 }
171
148 # check files correctness 172 # check files correctness
149 Check_Files() { 173 Check_Files() {
150 if [ ! "${ISNEW}" ]; then 174 if [ ! "${ISNEW}" ]; then
175 [ "${NOCLEAN}" ] || Cleanup_Repository
151 echo "Checking for errors..." 176 echo "Checking for errors..."
152 # THE FILES
153
154 # current dir: ${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}
155 cd "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}" 177 cd "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}"
156 TRASH_DIR="${SITE_DIR}-${TARGET_DIR}-`date -u +%Y%m%d-%H.%M`"
157 mkdir -p "${BASE_DIR}/trash/${TRASH_DIR}"
158 # FIXME FIXME FIXME FIXME FIXME FIXME
159 for TRASH in *
160 do
161 if [ -d "${TRASH}" ]; then
162 mv -f "${TRASH}" "${BASE_DIR}/trash/${TRASH_DIR}" || Err_Fatal "Error deleting files"
163 echo "Moved ${TRASH} to ${BASE_DIR}/trash/${TRASH_DIR}"
164 else
165 TRASH="`echo ${TRASH} | sed -e 's/\([0-9a-f]\{32\}.*\)//g' | grep -v ^$`"
166 if [ "${TRASH}" ]; then
167 mv -f "${TRASH}" "${BASE_DIR}/trash/${TRASH_DIR}" || Err_Fatal "Error deleting files"
168 echo "Moved ${TRASH} to ${BASE_DIR}/trash/${TRASH_DIR}"
169 fi
170 fi
171 done
172 [ "`ls "${BASE_DIR}/trash/${TRASH_DIR}"`" ] || rmdir "${BASE_DIR}/trash/${TRASH_DIR}"
173 printf "" > "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error" 178 printf "" > "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error"
174 for FILE in * 179 for FILE in *
175 do 180 do
176 if [ "`${MD5} "${FILE}" | cut -d ' ' -f1 -`" != "`echo "${FILE}" | cut -d '.' -f1`" ] 181 if [ "`echo \"${FILE}\" | sed -e 's/\([0-9a-f]\{32\}.*\)//g' | grep -v ^$`" ]; then
177 then 182 echo "Not a valid danbooru file: ${FILE}"
178 echo 183 else
179 echo "${FILE}" >> "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error" 184 if [ "`${MD5} "${FILE}" | cut -d ' ' -f1 -`" != "`echo "${FILE}" | cut -d '.' -f1`" ]
180 echo "Error: ${FILE}" 185 then
186 echo
187 echo "${FILE}" >> "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error"
188 echo "Error: ${FILE}"
189 fi
190 printf "."
181 fi 191 fi
182 printf "."
183 done 192 done
184 echo 193 echo
185 TOTAL_ERROR=`echo \`wc -l < "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error"\`` 194 TOTAL_ERROR=`echo \`wc -l < "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error"\``
186 echo "${TOTAL_ERROR} file(s) error" 195 echo "${TOTAL_ERROR} file(s) error"
187 echo "Removing error files" 196 echo "Removing error files"
272 [ "${SITE}" ] || SITE="${DEFAULT_SITE}" 281 [ "${SITE}" ] || SITE="${DEFAULT_SITE}"
273 [ "${TAGS}" ] || Err_Fatal "No tag specified" 282 [ "${TAGS}" ] || Err_Fatal "No tag specified"
274 # Get base folder - default, current folder or fallback to ${HOME} 283 # Get base folder - default, current folder or fallback to ${HOME}
275 [ "${BASE_DIR}" ] || BASE_DIR="${PWD}" 284 [ "${BASE_DIR}" ] || BASE_DIR="${PWD}"
276 [ "${BASE_DIR}" ] || BASE_DIR="{$HOME}" 285 [ "${BASE_DIR}" ] || BASE_DIR="{$HOME}"
277 [ "`echo ${BASE_DIR} | cut -c1 | grep \/`" ] || BASE_DIR="/${BASE_DIR}" 286 [ "`echo \"${BASE_DIR}\" | cut -c1 | grep \/`" ] || BASE_DIR="/${BASE_DIR}"
278 287
279 echo "Tags: ${TAGS}" 288 echo "Tags: ${TAGS}"
280 # slash is not wanted for folder name 289 # slash is not wanted for folder name
281 TARGET_DIR="`echo "${TAGS}" | sed -e 's/\//_/g'`" 290 TARGET_DIR="`echo "${TAGS}" | sed -e 's/\//_/g'`"
282 SITE_DIR="`echo "${SITE}" | sed -e 's/\/$//g;s/\//_/g'`" 291 SITE_DIR="`echo "${SITE}" | sed -e 's/\/$//g;s/\//_/g'`"