TMPDIR=tmp

PHOTODIR=www/photo
BIGDIR=${PHOTODIR}/big
MIDDIR=${PHOTODIR}
SMLDIR=${PHOTODIR}/small

DSTDIRS=${BIGDIR} ${MIDDIR} ${SMLDIR}
DSTPFX=2008-12-28

SRC!= ls -1 *.CR2 \
#SRC_ARGS_P1070406=-rotate -90

RM?=rm
CONV?=convert
UFRAW?=ufraw-batch \
	--create-id=also \
	--interpolation=ahd \
	--out-type=tiff --out-depth=16 \
	--overwrite --zip 
#UFRAW?=ufraw \
#	--create-id=also \
#	--interpolation=ahd \
#	--out-type=tiff --out-depth=16 \
#	--overwrite --zip --wavelet-denoising-threshold=350

N=0
.for n in ${SRC}
.for d in ${DSTDIRS}
DST!=printf '%s %s/%s-%04d.jpeg' '${DST}' ${d} ${DSTPFX} ${N}
.endfor
SRCN!=printf '%s %s,%04d' '${SRCN}' ${n} ${N}
N!=echo $$((${N}+1))
.endfor
.undef N

all: files

dirs: ${TMPDIR} ${DSTDIRS}
${TMPDIR} ${DSTDIRS}:
	mkdir -p ${@}

files: dirs ${DST}

.for n in ${SRCN}

${BIGDIR}/${DSTPFX}-${n:C/.*,//}.jpeg: ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff
	@echo ">>>> ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff(${n:C/,.*//}) => ${@}"
	${CONV} ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff \
		-resize 1536^ -unsharp 0x0.65+0.5+0.04 \
		-compress zip ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff
	${CONV} ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff \
	    ${DST_ARGS_${n:C/,.*//:R:R}} \
		-quality 90 ${@}
${MIDDIR}/${DSTPFX}-${n:C/.*,//}.jpeg: ${BIGDIR}/${DSTPFX}-${n:C/.*,//}.jpeg
	@echo ">>>> ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff(${n:C/,.*//}) => ${@}"
	${CONV} ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff \
		-resize 768^ -unsharp 0x0.65+0.4+0.04 \
		-compress zip ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff
	${CONV} ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff \
	    ${DST_ARGS_${n:C/,.*//:R:R}} \
		-quality 90 ${@}
${SMLDIR}/${DSTPFX}-${n:C/.*,//}.jpeg: ${MIDDIR}/${DSTPFX}-${n:C/.*,//}.jpeg
	@echo ">>>> ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff(${n:C/,.*//}) => ${@}"
	${CONV} ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff \
		-resize 384^ -unsharp 0x0.55+0.4+0.03 \
		-compress zip ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff
	${CONV} ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff \
		-resize 256^ -unsharp 0x0.55+0.4+0.03 \
		-compress zip ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff
	${CONV} ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff \
	    -gamma 1.10 ${DST_ARGS_${n:C/,.*//:R:R}} \
		-quality 90 ${@}
	${RM} ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff

.if (exists(${n:C/,.*//:M*.JPG}))
${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff:
	@echo ">>>> ${n:C/,.*//} => ${@}"
	${CONV} ${n:C/,.*//} \
		${SRC_ARGS_${n:C/,.*//:R:R}} \
		-resize 2048^ -unsharp 0x0.75 \
		-compress zip ${@}
#.elif (exists(${n:C/,.*//:M*.CR2}) && exists(${n:C/,.*//:.CR2=.ufraw}))
.elif (exists(${n:C/,.*//:M*.CR2}))
${n:C/,.*//:.CR2=.ufraw}:
.if (!exists(${n:C/,.*//:.CR2=.ufraw}))
	cp IMG_0078.ufraw ${n:C/,.*//:.CR2=.ufraw}
.endif
${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff: ${n:C/,.*//:.CR2=.ufraw}
	@echo ">>>> ${n:C/,.*//} => ${@}"
	crop=''; \
	test -z "${NOCROP}" \
	&& eval "$$(awk '/^<Crop>.+<\/Crop>/{print "crop='\''--crop-left="substr($$1,7)" --crop-top="$$2" --crop-right="$$3" --crop-bottom="substr($$4,1,length($$4)-7)"'\''"}' ${n:C/,.*//:.CR2=.ufraw})"; \
	test -z "$${crop}" || echo "**** ${n:C/,.*//} -- $${crop}" >>${TMPDIR}/log; \
	env LANG=C ${UFRAW} \
		$${crop} \
		--conf=${n:C/,.*//:.CR2=.ufraw} --output=${n:C/,.*//:.CR2=.tiff} ${n:C/,.*//}
	mv ${n:C/,.*//:.CR2=.tiff} ${@}
	${CONV} ${@} \
		${SRC_ARGS_${n:C/,.*//:R:R}} \
        -orient top-left \
		-resize 2048^ -unsharp 0x0.75 \
		-compress zip ${@}
.else
${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff:
	@echo "Cannot convert ${n:C/,.*//} into ${@}"
	@exit 1
.endif
	@echo '${n:C/,.*//};${DSTPFX}-${n:C/.*,//};${SRC_ARGS_${n:C/,.*//:R:R}};${DST_ARGS_${n:C/,.*//:R:R}}' >>${TMPDIR}/log

.endfor
