#!/bin/bash # TODO make this runnable from anywhere... # - prepend paths with './' only if local/relative # HACK: this is here to avoid using windows find... PATH=/bin:$PATH printhelp(){ echo "Usage: `basename $0` [ARGUMENTS] [ARCHIVE_ROOT]" echo echo "Arguments:" echo " -h --help - print this help and exit." echo " --common-previews PATH" echo " - build a single preview set at PATH." echo " -c - build a single common path at ARCHIVE_ROOT;" echo " this is a shorthand for: --common-path '.'." echo echo " --skip-archive - skip creating archive structure (use: exiftool)." echo " --skip-previews - skip creating previews (use: vips)." echo " --skip-cache - skip creating cache (use: buildcache)." echo " --skip-all - same as setting all of the above." echo echo "NOTE: common preview path is relative to ARCHIVE_ROOT." echo "NOTE: if no arguments are passed then this will process all directories" echo " in current location." echo } # process args... while true ; do case $1 in -h|--help) printhelp exit ;; -c) COMMON_PREVIEWS="." shift ;; --common-previews) COMMON_PREVIEWS="${2}" shift shift ;; --skip-archive) SKIP_ARCHIVE=yes echo skipping making archive... shift ;; --skip-previews) SKIP_PREVIEWS=yes echo skipping making previews... shift ;; --skip-cache) SKIP_CACHE=yes echo skipping making cache... shift ;; --skip-all) SKIP_ARCHIVE=yes echo skipping making archive... SKIP_PREVIEWS=yes echo skipping making previews... SKIP_CACHE=yes echo skipping making cache... shift ;; *) break ;; esac done if [ -z "$1" ] ; then ARCHIVE_ROOT="." else ARCHIVE_ROOT="$1" fi echo "Doing: \"$ARCHIVE_ROOT\"" METADATA_DIR="metadata" RAW_PREVIEW_DIR="hi-res (RAW)" PROCESSED_PREVIEW_DIR="preview" PROCESSED_PREVIEW_NAME="%-:1d/${PROCESSED_PREVIEW_DIR}/%f.jpg" PREVIEW_NAME="%-:1d/${RAW_PREVIEW_DIR}/%f.jpg" JSON_NAME="%-:1d/${METADATA_DIR}/%f.json" # TODO do a version of this using exiv2... # - to be more flexible... # - check speed... # - give the user more options... # TODO use dcraw to extract/generate previews if we could not get any # via exiftool # dcraw -e $RAW # - try and extract a preview # - creates a file: $RAW-thumb.jpg # dcraw -c $RAW | pnmtojpeg -quality=90 > $JPG # - process raw and convert to jpeg (slow) # XXX need to also copy jpg originals to the preview dir (things that # were shot in jpeg in-camera)... # XXX need to prevent overwriting of unchanged exif data... # when file exists?? # XXX add PSD metadata extraction... # -execute '-FileModifyDate $W) s = $H else s = $W ; s / $SIZE" | bc -l) # NOTE: bash does not do float comparisons so we cheat again ;) TOO_SMALL=$(echo "if($FACTOR <= 1) s = 1 else s = 0 ; s" | bc -l) # the input is smaller than target size, copy as-is... if [[ $TOO_SMALL == 1 ]] ; then echo "$IN: Too small, copying as-is..." cp "./$IN" "./$OUT" # shrink... else echo "($FACTOR): ${OUT}:${COMPRESSION}" vips im_shrink "./$IN" "./${OUT}:${COMPRESSION}" $FACTOR $FACTOR 2> /dev/null fi touch -c -r "./$IN" "./${OUT}" else echo "File already exists: ${OUT}" fi } export SIZE COMPRESSION export -f makepreview cd "./${ARCHIVE_ROOT}" # make previews... if [ -z $SKIP_PREVIEWS ] ; then #export TOTAL=$(find . -type d -name 'hi-res (RAW)' -exec ls "{}" \; | wc -l) # XXX do not know how to pass and modify a var... #export CUR=1 #export TOTAL=`find . -path '*hi-res (RAW)/*.jpg' | wc -l` find . -path '*hi-res (RAW)/*.jpg' -exec bash -c 'makepreview "$SIZE" "{}"' \; fi # collect previews to one location... # XXX test!!! if ! [ -z "$COMMON_PREVIEWS" ] ; then if ! [ -e "./$COMMON_PREVIEWS" ] ; then mkdir -p "./$COMMON_PREVIEWS" fi #if [ -z $TOTAL ] ; then # export TOTAL=`find . -path '*hi-res (RAW)/*.jpg' | wc -l` #fi # XXX BUG: this does not rename if target exists... find . -type d \ -name 'preview (RAW)' \ -print \ -exec cp --backup=t -rl "{}" "./$COMMON_PREVIEWS" \; #-exec rm -rf "./$d" # cleanup filenames... (HACK) # image.jpg -> image_3.jpg # image.jpg.~3~ -> image_2.jpg # image.jpg.~2~ -> image_1.jpg # image.jpg.~1~ -> image.jpg # i=0 while true ; do i=$((i + 1)) images=("$COMMON_PREVIEWS/preview (RAW)/"*.~$i~) # break if no matches... if ! [ -e "${images[0]}" ] ; then break fi for img in "${images[@]}" ; do # decrement... # image.jpg.~(N)~ -> image_(N-1).jpg mv "$img" "${img/.jpg.~${i}~/_$((i-1)).jpg}" # next image does not exist... # image.jpg -> image_(N).jpg # image_0.jpg -> image.jpg if ! [ -e "${img/.jpg.~${i}~/.jpg.~$((i+1))~}" ] ; then mv "${img/.jpg.~${i}~/.jpg}" "${img/.jpg.~${i}~/_$((i)).jpg}" mv "${img/.jpg.~${i}~/_0.jpg}" "${img/.jpg.~${i}~/.jpg}" fi done done fi # build cache... if [ -z $SKIP_CACHE ] ; then # a little tweak to make build cache work... export PYTHONIOENCODING=UTF-8 #if [ -z $TOTAL ] ; then # export TOTAL=`find . -path '*hi-res (RAW)/*.jpg' | wc -l` #fi if ! [ -z "$COMMON_PREVIEWS" ] && [ -e "./$COMMON_PREVIEWS/preview (RAW)" ] ; then buildcache "./$COMMON_PREVIEWS/preview (RAW)" else find . -type d -name 'preview (RAW)' -exec buildcache "{}" \; fi fi # vim:set nowrap nospell :