#!/bin/sh

SRC='P1000088 P1000089 P1000090 P1000091 P1000092 P1000093 P1000094 P1000095'
DST='kazan-8-m'

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

depth=16; [ -z "${2}" ] || depth="${2}"

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 ${depth} -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 \
&& ${ENBL} -f 6972x12239 -o tmp.tiff tmp-0*.tif \
&& rm tmp-0*.tif \
&& ${CONV} tmp.tiff -crop 4512x7424+768+96 +repage -compress zip ${DST}.tiff \
&& rm tmp.tiff \
&& ${CONV} ${DST}.tiff -resize 4096 -unsharp 0x0.5+0.6+0.04 -density 300 -depth 8 -compress none ${DST}-prn.tiff \
&& ${CONV} ${DST}.tiff -resize 3072 -unsharp 0x0.75+0.9+0.06 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize 1536 -unsharp 0x0.75+0.7+0.06 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.0 -quality 90 ${DST}-big.jpeg \
&& ${CONV} ${DST}.tiff -resize 768 -unsharp 0x0.75+0.5+0.05 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.0 -quality 90 ${DST}.jpeg \
&& ${CONV} ${DST}.tiff -resize 512 -unsharp 0x0.6+0.4+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize 256 -unsharp 0x0.6+0.4+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.0 -quality 90 ${DST}-small.jpeg \
&& rm ${DST}.tiff
exit
