#!/bin/sh -e pebkac() { [ "${*}" ] && printf "%s\n\n" "${*}" cat < Given an ID number from the site: * downloads and stashes the full-size image in the md5 directory * adds whatever tags are found into the tag area -p Associates the downloaded image with a pool -i Used with -p to indicate the position in the pool -t Refresh the tags, not the picture -f If the image is deleted, provide the local filename EOF kill $$ exit 1 } while getopts p:i:tf: OPT do case "${OPT}" in p) pool="${OPTARG}" ;; i) indx="${OPTARG}" ;; t) tags=TAGS ;; f) hash="${OPTARG}" ;; ?) pebkac ;; esac done shift $(( $OPTIND - 1 )) [ "${*}" ] || pebkac id="${1}" [ -z "$(echo "${id}" | tr -d '[0-9]')" ] || pebkac "ID should be an integer" taggery_name="$(basename "${0}")" taggery_profile="$(dirname "${0}")/lib/profiles/${taggery_name}" [ -f "${taggery_profile}" ] || pebkac "Unsupported profile: ${taggery_name}" . "${taggery_profile}" # Set up temporary area mkdir -p "$(dirname "${0}")/tmp" TMPDIR="$(dirname "${0}")/tmp" # Load KVS kvs="${TMPDIR}/.${taggery_name}.kvdb" . "$(dirname "${0}")/lib/kvs.sh" # Load Taggery . "$(dirname "${0}")/lib/taggery.sh" image_url() { taggery_image_url "${@}"; } image_tags() { taggery_image_tags "${@}"; } cd "$(dirname "${0}")" file="$(image_url "${id}")" tags="$(image_tags "${id}")" [ "${file}" -o "${hash}" ] || { echo "No download URL (deleted?) and -f not provided" >&2; kill -ABRT $$; exit 1; } [ "${tags}" ] || { echo "No tags found" >&2; kill -ABRT $$; exit 1; } if [ "${file}" ] then name="$(basename "${file}")" else name="${hash}" fi extn="${name#*.}" unset nabbed if [ "${file}" ] then if [ -e "md5/${name}" ] then echo "Filename md5/${name} already exists" else referer="$(taggery_image_referer "${id}")" wget ${referer:+--referer="${referer}"} -O "md5/${name}" "${file}" nabbed=NABBED fi fi if [ "${nabbed}" -o "${tags}" ] then for tag in ${tags} do echo ${tag} tag="$(echo "${tag}" | sed -e 's/\//%47/g; s/[\]/\\&/g')" mkdir -p "tag/${tag}" ln -sf "../../md5/${name}" "tag/${tag}/" done fi # Even if we didn't download it, try to link it into the pool [ "${pool}" ] && { plnm="pool/${pool}/${indx}.${extn}" if [ -e "${plnm}" ] then echo "Filename ${plnm} already exists" else mkdir -p "pool/${pool}" ln -svf "../../md5/${name}" "${plnm}" fi }