TMPDIR=tmp

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

DSTDIRS=${BIGDIR} ${MIDDIR} ${SMLDIR}
DSTPFX=bat-2008

SRC!= ls -1 \
    P1070406.JPG \
    P1070407.JPG \
    P1070410.RAW.bz2 \
    P1070412.RAW.bz2 \
    P1070413.RAW.bz2 \
    P1070415.RAW.bz2 \
    P1070416.RAW.bz2 \
    P1070420.RAW.bz2 \
    P1070421.RAW.bz2 \
    P1070425.RAW.bz2 \
    P1070426.RAW.bz2 \
    P1070428.RAW.bz2 \
    P1070435.RAW.bz2 \
    P1070436.RAW.bz2 \
    P1070437.RAW.bz2 \
    P1070439.RAW.bz2 \
    P1070440.RAW.bz2 \
    P1070442.RAW.bz2 \
    P1070443.RAW.bz2 \
    P1070445.RAW.bz2 \
    P1070451.RAW.bz2 \
    P1070452.RAW.bz2 \
    P1070453.RAW.bz2 \
    P1070457.RAW.bz2 \
    P1070459.RAW.bz2 \
    P1070461.RAW.bz2 \
    P1070463.RAW.bz2 \
    P1070465.RAW.bz2 \
    P1070468.RAW.bz2 \
    P1070469.RAW.bz2 \
    P1070470.RAW.bz2
SRC_ARGS_P1070406=-rotate -90

CONV=convert
UFRAW=ufraw-batch \
	--create-id=no \
	--interpolation=ahd \
	--out-type=tiff --out-depth=16 \
	--overwrite --zip --wavelet-denoising-threshold=350
#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: dirs files

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

files: ${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 x1536 -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 x768 -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 x384 -unsharp 0x0.55+0.4+0.03 \
		-compress zip ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff
	${CONV} ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff \
		-resize x256 -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}} \
		-enhance \
		-resize x2048 -unsharp 0x0.75 \
		-enhance \
		-compress zip ${@}
#.elif (exists(${n:C/,.*//:M*.RAW.bz2}) && exists(${n:C/,.*//:.RAW.bz2=.ufraw}))
.elif (exists(${n:C/,.*//:M*.RAW.bz2}))
${n:C/,.*//:.RAW.bz2=.ufraw}:
.if (!exists(${n:C/,.*//:.RAW.bz2=.ufraw}))
	cp ../../pano/bat-2008-1/P1070431.ufraw ${n:C/,.*//:.RAW.bz2=.ufraw}
.endif
${TMPDIR}/${DSTPFX}-${n:C/.*,//}.tiff: ${n:C/,.*//:.RAW.bz2=.ufraw}
	@echo ">>>> ${n:C/,.*//} => ${@}"
.if (exists(${TMPDIR}/${DSTPFX}-${n:C/.*,//}.ufraw))
	cp ${TMPDIR}/${DSTPFX}-${n:C/.*,//}.ufraw ${n:C/,.*//:.RAW.bz2=.ufraw}
.endif
	crop=''; \
	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/,.*//:.RAW.bz2=.ufraw})"; \
	test -z "$${crop}" || echo "**** ${n:C/,.*//} -- $${crop}" >>${TMPDIR}/log; \
	env LANG=C ${UFRAW} \
		$${crop} \
		--conf=${n:C/,.*//:.RAW.bz2=.ufraw} --output=${@} ${n:C/,.*//}
	${CONV} ${@} \
		${SRC_ARGS_${n:C/,.*//:R:R}} \
		-enhance \
		-resize x2048 -unsharp 0x0.75 \
		-enhance \
		-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
