#!/bin/sh

SRC='P1000177 P1000178 P1000179 P1000180 P1000181 P1000182 P1000183 P1000184
     P1000185 P1000187 P1000188 P1000189 P1000190 P1000191 P1000192 P1000193
     P1000194 P1000195 P1000196 P1000197 P1000198 P1000199 P1000200 P1000201
     P1000202 P1000203 P1000204 P1000205 P1000206 P1000207 P1000208 P1000209
     P1000210 P1000211 P1000213 P1000214 P1000215 P1000216 P1000217 P1000218
     P1000219 P1000220 P1000221 P1000222 P1000223 P1000224 P1000225 P1000226
     P1000227 P1000228 P1000229 P1000230 P1000231 P1000232 P1000233 P1000234
     P1000235'
DST='kazan-12'

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'
FGLB='fulla -t 2 -s -g 0.09:0.02:-0.9:1.75'
#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

DST="${DST}=244=-60"

[ -n "${DEPTH}" ] || DEPTH="${2}"
[ -n "${DEPTH}" ] || DEPTH=16

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 undefined \
        -enhance -unsharp 0x0.8 -resize 50% -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 \
&& echo rm [0-9][0-9].tiff \
&& ${ENBL} -f 7168x8192 -o tmp.tiff tmp-0*.tif \
&& rm tmp-0*.tif \
&& mv tmp.tiff ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize 5120 -unsharp 0x0.75+0.8+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize 3072 -unsharp 0x0.75+0.8+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -negate -gamma 1.2 -negate -gamma 1.1 -quality 90 ${DST}-huge.jpeg \
&& ${CONV} ${DST}.tiff -resize 1536 -unsharp 0x0.75+0.6+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -negate -gamma 1.2 -negate -gamma 1.1 -quality 90 ${DST}-big.jpeg \
&& ${CONV} ${DST}.tiff -resize 768 -unsharp 0x0.75+0.6+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -negate -gamma 1.2 -negate -gamma 1.1 -quality 90 ${DST}.jpeg \
&& ${CONV} ${DST}.tiff -resize 512 -unsharp 0x0.65+0.6+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize 256 -unsharp 0x0.65+0.6+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -negate -gamma 1.2 -negate -gamma 1.15 -quality 90 ${DST}-small.jpeg \
&& ${CONV} ${DST}.tiff -resize x256 -unsharp 0x0.65+0.6+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -negate -gamma 1.2 -negate -gamma 1.15 -quality 90 ${DST}-tiny.jpeg \
&& rm ${DST}.tiff
exit
