#!/bin/sh

[ -r .conf-top ] || exit 125

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'
#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

. .conf-top

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

N=0
for n in ${SRC}
do
    o=$(printf '%02d' ${N})
    N=$((${N}+1))
    [ -r ${o}.tiff ] && continue
    [ -r ${n}.JPG ] || exit 126
    ${CONV} ${n}.JPG \
        -depth ${DEPTH} -orient undefined \
        -unsharp 0x0.3 \
        -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
[ "${1}" = prep ] && exit

${NONA} ${DST}.pto \
&& rm [0-9][0-9].tiff \
&& ${ENBL} -f 12383x10382 -o tmp.tiff tmp-0*.tif \
&& rm tmp-0*.tif \
&& ${CONV} tmp.tiff -crop 12096x4224+90+870 +repage +matte -compress zip ${DST}.tiff \
&& rm tmp.tiff \
&& ${CONV} ${DST}.tiff -resize x3072 -unsharp 0x0.85+0.8+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.15,1.3,1.25 -modulate 100,85 -quality 90 ${DST}-huge.jpeg \
&& ${CONV} ${DST}.tiff -resize x1536 -unsharp 0x0.85+0.6+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.15,1.3,1.25 -modulate 100,85 -quality 90 ${DST}-big.jpeg \
&& ${CONV} ${DST}.tiff -resize x768 -unsharp 0x0.85+0.6+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.15,1.35,1.25 -modulate 100,85 -quality 90 ${DST}.jpeg \
&& ${CONV} ${DST}.tiff -resize x384 -unsharp 0x0.75+0.6+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize x256 -unsharp 0x0.75+0.6+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.19,1.35,1.3 -modulate 100,85 -quality 90 ${DST}-small.jpeg \
&& rm ${DST}.tiff
exit
