#!/bin/sh

SRC='P1010101 P1010102 P1010103 P1010104 P1010105 P1010106 P1010107 P1010108 P1010109 P1010110 P1010111 P1010112'
DST='spring-1'

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'
PFSHDR='eval pfsinhdrgen ${DST}.hdrgen | pfshdrcalibrate -v -r linear -g 8.00 -b 16'
PFSTMO='eval pfstmo_reinhard02 -v -k 0.00625 -p 8 | pfsgamma -m 1.075 -g 2.2 | pfsoutimgmagick --quality 100'

LANG=C; export LANG

if [ "${#}" -eq 0 ]
then

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 \
        -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
#exit

#${NONA} ${DST}.pto \
#&& echo rm [0-9][0-9].tiff \
#&& ${CONV} tmp-0000.tif -crop 390x390+2540+5744 -compress zip tmp-0000a.tif \
#&& ${CONV} tmp-0001.tif -crop 390x390+2540+5744 -compress zip tmp-0001a.tif \
#&& ${CONV} tmp-0001.tif -crop 3346x1048+1414+4760 -compress zip tmp-0001b.tif \
#&& ${CONV} tmp-0001.tif -crop 3346x1235+1414+6070 -compress zip tmp-0001c.tif \
#&& ${CONV} tmp-0001.tif -crop 1190x390+1414+5744 -compress zip tmp-0001d.tif \
#&& ${CONV} tmp-0001.tif -crop 1894x390+2866+5744 -compress zip tmp-0001e.tif \
#&& ${CONV} tmp-0002.tif -crop 390x390+2540+5744 -compress zip tmp-0002a.tif \
#&& ${CONV} tmp-0002.tif -crop 3299x1048+1414+4760 -compress zip tmp-0002b.tif \
#&& ${CONV} tmp-0002.tif -crop 3299x1188+1414+6070 -compress zip tmp-0002c.tif \
#&& ${CONV} tmp-0002.tif -crop 1190x390+1414+5744 -compress zip tmp-0002d.tif \
#&& ${CONV} tmp-0002.tif -crop 1847x390+2866+5744 -compress zip tmp-0002e.tif \
#&& ${CCM} tmp-0001a.tif tmp-0000a.tif && mv tmp.tiff tmp-0001a.tif \
#&& ${CCM} tmp-0002a.tif tmp-0000a.tif && mv tmp.tiff tmp-0002a.tif \
#&& ${ENBL} -f 6020x8483 -o tmp+0.tiff tmp-0000.tif tmp-0003.tif tmp-0006.tif tmp-0009.tif \
#&& ${ENBL} -f 6020x8483 -o tmp-1.tiff tmp-0001[a-e].tif tmp-0004.tif tmp-0007.tif tmp-0010.tif \
#&& ${ENBL} -f 6020x8483 -o tmp+1.tiff tmp-0002[a-e].tif tmp-0005.tif tmp-0008.tif tmp-0011.tif \
#&& echo rm tmp-0*.tif \
true && ${CONV} tmp+0.tiff -crop 3712x7232+1232+120 +repage -compress zip ${DST}+0.tiff \
&& ${CONV} tmp-1.tiff -crop 3712x7232+1232+120 +repage -compress zip ${DST}-1.tiff \
&& ${CONV} tmp+1.tiff -crop 3712x7232+1232+120 +repage -compress zip ${DST}+1.tiff \
&& echo rm tmp*.tiff \
&& ${CONV} ${DST}+0.tiff -channel A -separate -resize 50% -gaussian 0x1.0 -evaluate multiply 2.5 -depth 1 +matte -quality 95 alpha+0.png \
&& ${CONV} ${DST}-1.tiff -channel A -separate -resize 50% -gaussian 0x1.0 -evaluate multiply 2.5 -depth 1 +matte -quality 95 alpha-1.png \
&& ${CONV} ${DST}+1.tiff -channel A -separate -resize 50% -gaussian 0x1.0 -evaluate multiply 2.5 -depth 1 +matte -quality 95 alpha+1.png \
&& ${CONV} alpha+0.png -negate -quality 95 alpha+0.png \
&& ${CONV} alpha-1.png -negate -quality 95 alpha-1.png \
&& ${CONV} alpha+1.png -negate -quality 95 alpha+1.png \
&& ${CONV} alpha+0.png alpha-1.png -compose multiply -composite -quality 95 mask.png \
&& ${CONV} mask.png alpha+1.png -compose multiply -composite -quality 95 mask.png \
&& echo rm -f alpha*.png \
&& ${CONV} ${DST}+0.tiff +matte -resize 50% -gaussian 0x0.5 mask.png -compose multiply -composite -compress LZW ${DST}+0.tiff \
&& ${CONV} ${DST}-1.tiff +matte -resize 50% -gaussian 0x0.5 mask.png -compose multiply -composite -compress LZW ${DST}-1.tiff \
&& ${CONV} ${DST}+1.tiff +matte -resize 50% -gaussian 0x0.5 mask.png -compose multiply -composite -compress LZW ${DST}+1.tiff \
&& echo rm -f mask.png \
&& ${CONV} ${DST}+0.tiff -gamma 0.45455 -compress zip hdr+0.tiff \
&& ${CONV} ${DST}-1.tiff -gamma 0.45455 -compress zip hdr-1.tiff \
&& ${CONV} ${DST}+1.tiff -gamma 0.45455 -compress zip hdr+1.tiff \
&& ${PFSHDR} | ${PFSTMO} ${DST}.tiff \
&& echo rm hdr*.tiff \
&& exit

#&& ${CONV} alpha+1.png alpha+0.png -compose darken -composite -compress LZW mask.png \
#&& ${CONV} alpha-1.png mask.png -compose darken -composite -compress LZW mask.png \

else

${CONV} ${DST}.ppm -depth 16 ${DST}.tiff \
&& ${CONV} ${DST}.tiff -resize 1536 -unsharp 0x0.75+0.6+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.0 -quality 90 ${DST}-big.jpeg \
&& ${CONV} ${DST}.tiff -resize 768 -unsharp 0x0.75+0.6+0.04 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.0 -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 -gamma 1.0 -quality 90 ${DST}-small.jpeg \
&& ${CONV} ${DST}.tiff -resize x256 -unsharp 0x0.65+0.6+0.03 -compress zip ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.0 -quality 90 ${DST}-tiny.jpeg \
&& rm ${DST}.tiff
exit

fi
