#!/bin/sh

SRC='P1000267 P1000268 P1000269 P1000270 P1000271 P1000272 P1000273 P1000274'
DST='kazan-17'

NONA='nona -t 2 -o tmp-'
CCM='color_correct -E -l -p.03 -D64 -a-4096:4096+1024 -k24:40/32 -g28:36/32 -otmp.tiff'
#ENBL='enblend -w -z -m 896 --fine-mask'
ENBL='enblend -w -z -m 896'
#CONV='convert -limit memory 512 -limit map 256'
CONV='convert'
INPNT='greycstoration -alpha 0.9 -sigma 0.2 -p 5 -a 1 -dt 20 -iter 5 -bits 16 -m mask.png -inpaint'

LANG=C; export LANG

rm -rf tmp
mkdir -p tmp
N=0
for n in ${SRC}
do
    o=$(printf '%02d' ${N})
    N=$((${N}+1))
    [ -r ${o}.tiff ] && continue
    [ -r ${n}.JPG ] || exit 126
    cp ${n}.JPG tmp/${o}.jpeg
done
sh ../../tools/orient tmp
for n in $(find tmp -type f)
do
    o=$(basename -s .jpeg ${n})
    ${CONV} ${n} \
        -depth 16 -orient top-left \
        -enhance -unsharp 0x0.5 \
        -compress zip ${o}.tiff
    [ -r mask-${o}.png ] \
    && ${CONV} ${o}.tiff -matte \
        mask-${o}.png -compose multiply -composite \
        mask-${o}.png -channel A -fx 'v.r' \
        -compress zip ${o}.tiff
done
rm -rf tmp
[ "${1}" = prep ] && exit

${NONA} ${DST}.pto \
&& rm [0-9][0-9].tiff \
&& ${CCM} tmp-0007.tif tmp-0005.tif && mv tmp.tiff tmp-0005.tif \
&& ${CCM} tmp-0006.tif tmp-0004.tif && mv tmp.tiff tmp-0004.tif \
&& ${CCM} tmp-0005.tif tmp-0003.tif && mv tmp.tiff tmp-0003.tif \
&& ${CCM} tmp-0004.tif tmp-0002.tif && mv tmp.tiff tmp-0002.tif \
&& ${CCM} tmp-0003.tif tmp-0001.tif && mv tmp.tiff tmp-0001.tif \
&& ${CCM} tmp-0002.tif tmp-0000.tif && mv tmp.tiff tmp-0000.tif \
&& ${ENBL} -f 8011x13998 -o tmp.tiff tmp-0*.tif \
&& rm tmp-0*.tif \
&& ${CONV} tmp.tiff -crop 5696x7360+1632+576 +repage -compress zip ${DST}.tiff \
&& rm tmp.tiff \
&& echo ${CONV} ${DST}.tiff -resize 3072 -unsharp 0x0.5+0.6+0.04 -density 300 -depth 8 -compress none ${DST}-prn.tiff \
&& ${CONV} ${DST}.tiff -resize 3072 -unsharp 0x1.35 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.25,1.25,1.375 -quality 90 ${DST}-huge.jpeg \
&& ${CONV} ${DST}.tiff -resize 1536 -unsharp 0x0.9+0.7+0.06 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.25,1.25,1.375 -quality 90 ${DST}-big.jpeg \
&& ${CONV} ${DST}.tiff -resize 768 -unsharp 0x0.95+0.5+0.05 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.25,1.25,1.375 -quality 90 ${DST}.jpeg \
&& ${CONV} ${DST}.tiff -resize 512 -unsharp 0x0.8+0.4+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize 256 -unsharp 0x0.8+0.4+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.35,1.35,1.485 -quality 90 ${DST}-small.jpeg \
&& ${CONV} ${DST}.tiff -resize x256 -unsharp 0x0.8+0.4+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.35,1.35,1.485 -quality 90 ${DST}-tiny.jpeg \
&& rm ${DST}.tiff
exit
