#!/bin/sh

SRC='P1000124 P1000125 P1000126 P1000127 P1000128 P1000129 P1000130 P1000131 P1000132 P1000133 P1000134 P1000135 P1000136 P1000138'
DST='kazan-11'

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 \
        -level 0.05%,75%,1.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
convert 08.tiff -crop 288x1344+3308+174 +repage -compress zip 08-a.tiff
convert 08.tiff -crop 192x1344+3234+174 +repage -compress zip 08-b.tiff
rm -rf tmp
[ "${1}" = prep ] && exit

${NONA} ${DST}.pto \
&& rm [0-9][0-9].tiff \
&& ${ENBL} -f 15220x5451 -o tmp.tiff tmp-0*.tif \
&& rm tmp-0*.tif \
&& ${CONV} tmp.tiff -crop 15200x4672+0+0 +repage -compress zip ${DST}.tiff \
&& exit
&& rm tmp.tiff \
&& echo ${CONV} ${DST}.tiff -resize x2048 -unsharp 0x0.5+0.6+0.04 -density 300 -compress zip ${DST}-prn.tiff \
&& ${CONV} ${DST}.tiff -resize x1536 -unsharp 0x0.85+0.7+0.06 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.0 -quality 90 ${DST}-big.jpeg \
&& ${CONV} ${DST}.tiff -resize x768 -unsharp 0x0.85+0.6+0.05 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.0 -quality 90 ${DST}.jpeg \
&& ${CONV} ${DST}.tiff -resize x512 -unsharp 0x0.7+0.5+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x256 -unsharp 0x0.7+0.5+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.0 -quality 90 ${DST}-small.jpeg \
&& rm ${DST}.tiff
exit

#        -level 0.05%,75%,1.5 \
