comparison moefetch.sh @ 223:04ad0b0a3c63

revert back to [
author edhoprima@gmail.com <edhoprima@gmail.com>
date Thu, 02 Jul 2009 21:32:45 +0000
parents e3fb9507cf7f
children 0ac1805621d4
comparison
equal deleted inserted replaced
222:e3fb9507cf7f 223:04ad0b0a3c63
133 133
134 # getting rid of ls (as per suggestion) 134 # getting rid of ls (as per suggestion)
135 Count_Files() { 135 Count_Files() {
136 _i=0 136 _i=0
137 for _f in "${*}/"* "${*}/".*; do 137 for _f in "${*}/"* "${*}/".*; do
138 if test "${_f}" != "${*}/"'*' || test -e "${_f}"; then 138 if [ "${_f}" != "${*}/*" ] || [ -e "${_f}" ]; then
139 _i=$((_i + 1)) 139 _i=$((_i + 1))
140 fi 140 fi
141 done 141 done
142 echo $((_i - 2)) 142 echo $((_i - 2))
143 } 143 }
144 144
145 # check tools availability 145 # check tools availability
146 Check_Tools() { 146 Check_Tools() {
147 # verify all programs required do indeed exist 147 # verify all programs required do indeed exist
148 #MD5 148 #MD5
149 if test -z "${MD5}"; then 149 if [ -z "${MD5}" ]; then
150 case "$(uname)" in 150 case "$(uname)" in
151 *BSD) MD5="md5 -r";; 151 *BSD) MD5="md5 -r";;
152 Linux|SunOS) MD5="md5sum";; 152 Linux|SunOS) MD5="md5sum";;
153 *) Fatal_Err "No known md5 tool for this platform. Please specify manually";; 153 *) Fatal_Err "No known md5 tool for this platform. Please specify manually";;
154 esac 154 esac
162 done 162 done
163 } 163 }
164 164
165 # verify required folders exist and writeable 165 # verify required folders exist and writeable
166 Check_Folders(){ 166 Check_Folders(){
167 test -O "${BASE_DIR}" || Err_Fatal "You don't own ${BASE_DIR}. Please fix ${BASE_DIR} or run this script in your own directory." 167 [ -O "${BASE_DIR}" ] || Err_Fatal "You don't own ${BASE_DIR}. Please fix ${BASE_DIR} or run this script in your own directory."
168 for directory in temp trash deleted "${SITE_DIR}/${TARGET_DIR}"; do 168 for directory in temp trash deleted "${SITE_DIR}/${TARGET_DIR}"; do
169 if [ ! -d "${BASE_DIR}/${directory}" ]; then 169 if [ ! -d "${BASE_DIR}/${directory}" ]; then
170 mkdir -p "${BASE_DIR}/${directory}" || Err_Impossible 170 mkdir -p "${BASE_DIR}/${directory}" || Err_Impossible
171 fi 171 fi
172 if [ ! -O "${BASE_DIR}/${directory}" ]; then 172 if [ ! -O "${BASE_DIR}/${directory}" ]; then
173 echo "You don't own the ${BASE_DIR}/${directory}, applying globally writeable permission on it" 173 echo "You don't own the ${BASE_DIR}/${directory}, applying globally writeable permission on it"
174 chmod -R u=rwX,g=rwX,o=rwX "${BASE_DIR}/${directory}" || Err_Impossible 174 chmod -R u=rwX,g=rwX,o=rwX "${BASE_DIR}/${directory}" || Err_Impossible
175 fi 175 fi
176 done 176 done
177 test "$(Count_Files "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}")" -eq 0 && ISNEW=1 177 [ "$(Count_Files "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}")" -eq 0 ] && ISNEW=1
178 for i in error ok list newlist templist; do 178 for i in error ok list newlist templist; do
179 touch "${TEMP_PREFIX}-${i}" || Fatal_Err "Error creating ${TEMP_PREFIX}-${i}. This shouldn't happen" 179 touch "${TEMP_PREFIX}-${i}" || Fatal_Err "Error creating ${TEMP_PREFIX}-${i}. This shouldn't happen"
180 done 180 done
181 # 181 #
182 } 182 }
187 printf "Cleaning up repository folder... " 187 printf "Cleaning up repository folder... "
188 Progress_Init 188 Progress_Init
189 trash_dir="${BASE_DIR}/trash/${trash_dir}/$(date -u "+${SITE_DIR}-${TARGET_DIR}-%Y%m%d-%H.%M")" 189 trash_dir="${BASE_DIR}/trash/${trash_dir}/$(date -u "+${SITE_DIR}-${TARGET_DIR}-%Y%m%d-%H.%M")"
190 trashes="These files have been moved to ${trash_dir}:" 190 trashes="These files have been moved to ${trash_dir}:"
191 has_trash= 191 has_trash=
192 if test ! -d "${trash_dir}"; then 192 if [ ! -d "${trash_dir}" ]; then
193 mkdir -p "${trash_dir}" || Err_Impossible 193 mkdir -p "${trash_dir}" || Err_Impossible
194 else 194 else
195 if test ! -o "${trash_dir}"; then 195 if [ ! -O "${trash_dir}" ]; then
196 chmod -R u=rwX,g=rwX,o=rwX "${BASE_DIR}/${directory}" || Err_Impossible 196 chmod -R u=rwX,g=rwX,o=rwX "${BASE_DIR}/${directory}" || Err_Impossible
197 fi 197 fi
198 fi 198 fi
199 for trash in "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}/"* 199 for trash in "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}/"*
200 do 200 do
201 is_trash= 201 is_trash=
202 if test -d "${trash}" || test -n "$(Is_NotMD5 "$(basename "${trash}")")" || test -z "$(grep "$(basename "${trash}")" "${TEMP_PREFIX}-list")"; then 202 if [ -d "${trash}" ] || [ -n "$(Is_NotMD5 "$(basename "${trash}")")" ] || [ -z "$(grep "$(basename "${trash}")" "${TEMP_PREFIX}-list")" ]; then
203 is_trash=1 203 is_trash=1
204 has_trash=1 204 has_trash=1
205 mv -f "${trash}" "${trash_dir}" || Err_Impossible 205 mv -f "${trash}" "${trash_dir}" || Err_Impossible
206 trashes="${trashes} 206 trashes="${trashes}
207 $(basename "${trash}")" 207 $(basename "${trash}")"
208 fi 208 fi
209 Progress_Anim 209 Progress_Anim
210 done 210 done
211 rmdir "${trash_dir}" 2>/dev/null 211 rmdir "${trash_dir}" 2>/dev/null
212 Progress_Done 212 Progress_Done
213 test -n "${has_trash}" && echo "${trashes}" 213 [ -n "${has_trash}" ] && echo "${trashes}"
214 } 214 }
215 215
216 # check files correctness 216 # check files correctness
217 Check_Files() { 217 Check_Files() {
218 if test ! -n "${ISNEW}"; then 218 if [ ! -n "${ISNEW}" ]; then
219 test -z "${NOCLEAN}" && Cleanup_Repository 219 [ -z "${NOCLEAN}" ] && Cleanup_Repository
220 printf "Checking for errors... " 220 printf "Checking for errors... "
221 Progress_Init 221 Progress_Init
222 files_error="These files do not match its md5:" 222 files_error="These files do not match its md5:"
223 files_notdanbooru="These files are not checked:" 223 files_notdanbooru="These files are not checked:"
224 has_err_filename= 224 has_err_filename=
225 has_err_md5= 225 has_err_md5=
226 > "${TEMP_PREFIX}-error" 226 > "${TEMP_PREFIX}-error"
227 > "${TEMP_PREFIX}-ok" 227 > "${TEMP_PREFIX}-ok"
228 for file in "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}/"* 228 for file in "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}/"*
229 do 229 do
230 if test "${file}" != "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}/*"; then 230 if [ "${file}" != "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}/*" ]; then
231 if test -n "$(Is_NotMD5 "$(basename "${file}")")" || test -d "${file}"; then 231 if [ -n "$(Is_NotMD5 "$(basename "${file}")")" ] || [ -d "${file}" ]; then
232 files_notdanbooru="${files_notdanbooru} 232 files_notdanbooru="${files_notdanbooru}
233 $(basename "${file}")" 233 $(basename "${file}")"
234 has_err_filename=1 234 has_err_filename=1
235 else 235 else
236 if test "$(${MD5} "${file}" | cut -d ' ' -f 1)" = "$(basename "${file}" | cut -d '.' -f 1)"; then 236 if [ "$(${MD5} "${file}" | cut -d ' ' -f 1)" = "$(basename "${file}" | cut -d '.' -f 1)" ]; then
237 echo "$(basename "${file}")" >> "${TEMP_PREFIX}-ok" 237 echo "$(basename "${file}")" >> "${TEMP_PREFIX}-ok"
238 else 238 else
239 rm "${file}" || Err_Fatal "Error removing ${file}" 239 rm "${file}" || Err_Fatal "Error removing ${file}"
240 echo "$(basename "${file}")" >> "${TEMP_PREFIX}-error" 240 echo "$(basename "${file}")" >> "${TEMP_PREFIX}-error"
241 files_error="${files_error} 241 files_error="${files_error}
245 fi 245 fi
246 fi 246 fi
247 Progress_Anim 247 Progress_Anim
248 done 248 done
249 Progress_Done 249 Progress_Done
250 if test ! -n "${has_err_md5}" && test ! -n "${has_err_filename}"; then 250 if [ ! -n "${has_err_md5}" ] && [ ! -n "${has_err_filename}" ]; then
251 echo "All files OK" 251 echo "All files OK"
252 else 252 else
253 if test -n "${has_err_md5}"; then 253 if [ -n "${has_err_md5}" ]; then
254 echo "${files_error}" 254 echo "${files_error}"
255 echo "$(echo $(wc -l < "${TEMP_PREFIX}-error")) file(s) removed" 255 echo "$(echo $(wc -l < "${TEMP_PREFIX}-error")) file(s) removed"
256 fi 256 fi
257 test -n "${has_err_filename}" && echo "${files_notdanbooru}" 257 [ -n "${has_err_filename}" ] && echo "${files_notdanbooru}"
258 fi 258 fi
259 echo "$(echo $(wc -l < "${TEMP_PREFIX}-ok")) file(s) available locally" 259 echo "$(echo $(wc -l < "${TEMP_PREFIX}-ok")) file(s) available locally"
260 260
261 printf "Generating list of new files... " 261 printf "Generating list of new files... "
262 Progress_Init 262 Progress_Init
267 Progress_Anim 267 Progress_Anim
268 done < "${TEMP_PREFIX}-ok" 268 done < "${TEMP_PREFIX}-ok"
269 Progress_Done 269 Progress_Done
270 echo "$(echo $(wc -l < "${TEMP_PREFIX}-newlist")) file(s) to be downloaded" 270 echo "$(echo $(wc -l < "${TEMP_PREFIX}-newlist")) file(s) to be downloaded"
271 else 271 else
272 if test -n "${ISQUICK}"; then 272 if [ -n "${ISQUICK}" ]; then
273 echo "Quick mode selected. Skipping check" 273 echo "Quick mode selected. Skipping check"
274 else 274 else
275 echo "Empty local repository" 275 echo "Empty local repository"
276 fi 276 fi
277 cat "${TEMP_PREFIX}-list" > "${TEMP_PREFIX}-newlist" 277 cat "${TEMP_PREFIX}-list" > "${TEMP_PREFIX}-newlist"
278 fi 278 fi
279 } 279 }
280 280
281 # start downloading the images 281 # start downloading the images
282 Fetch_Images() { 282 Fetch_Images() {
283 if test "$(echo $(wc -l < "${TEMP_PREFIX}-newlist"))" -eq 0; then 283 if [ "$(echo $(wc -l < "${TEMP_PREFIX}-newlist"))" -eq 0 ]; then
284 echo "No new file" 284 echo "No new file"
285 else 285 else
286 printf "Starting wget... " 286 printf "Starting wget... "
287 cd "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}" 287 cd "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}"
288 wget -e continue=on -bi "${TEMP_PREFIX}-newlist" -o "${TEMP_PREFIX}.log" 288 wget -e continue=on -bi "${TEMP_PREFIX}-newlist" -o "${TEMP_PREFIX}.log"
290 } 290 }
291 291
292 # initialize base variables and initial command check 292 # initialize base variables and initial command check
293 Init(){ 293 Init(){
294 # path initialization 294 # path initialization
295 test -n "${ADDITIONAL_PATH}" && PATH="${ADDITIONAL_PATH}:${PATH}" 295 [ -n "${ADDITIONAL_PATH}" ] && PATH="${ADDITIONAL_PATH}:${PATH}"
296 export PATH 296 export PATH
297 297
298 # misc variables 298 # misc variables
299 ISQUICK= 299 ISQUICK=
300 ISNEW= 300 ISNEW=
329 fi 329 fi
330 ;; 330 ;;
331 esac 331 esac
332 shift 332 shift
333 done 333 done
334 test -n "${SITE}" || SITE=${DEFAULT_SITE} 334 [ -n "${SITE}" ] || SITE=${DEFAULT_SITE}
335 test -n "${TAGS}" || Err_Fatal "No tag specified" 335 [ -n "${TAGS}" ] || Err_Fatal "No tag specified"
336 # Get base folder - default, current folder or fallback to ${HOME} 336 # Get base folder - default, current folder or fallback to ${HOME}
337 test -n "${BASE_DIR}" || BASE_DIR=${PWD} 337 [ -n "${BASE_DIR}" ] || BASE_DIR=${PWD}
338 test -n "${BASE_DIR}" || BASE_DIR=${HOME} 338 [ -n "${BASE_DIR}" ] || BASE_DIR=${HOME}
339 test -n "$(echo "${BASE_DIR}" | cut -c1 | grep \/)" || BASE_DIR="/${BASE_DIR}" 339 [ -n "$(echo "${BASE_DIR}" | cut -c1 | grep \/)" ] || BASE_DIR="/${BASE_DIR}"
340 340
341 echo "Tags: ${TAGS}" 341 echo "Tags: ${TAGS}"
342 # slash is not wanted for folder name 342 # slash is not wanted for folder name
343 TARGET_DIR=$(echo "${TAGS}" | sed -e 's/\//_/g') 343 TARGET_DIR=$(echo "${TAGS}" | sed -e 's/\//_/g')
344 SITE_DIR=$(echo "${SITE}" | sed -e 's/\/$//g;s/\//_/g') 344 SITE_DIR=$(echo "${SITE}" | sed -e 's/\/$//g;s/\//_/g')