#!/bin/sh

SRC='P1010380 P1010381 P1010382 P1010383 P1010384'
#CONF='P1010382'
DST='home-window-19'

UFRAW='ufraw-batch --interpolation=ahd --out-type=tiff16 --overwrite --zip --wavelet-denoising-threshold=200'
NONA='nona -t 2 -o tmp-'
FULLA='fulla -t 2'
CCM='color_correct -E -c -p.03 -D64 -a-4096:4096+1024 -k24:40/32 -g28:36/32 -otmp.tiff'
ENBL='enblend -w -z -m 768 --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 75 -bits 16 -m mask.png -inpaint'

LANG=C; export LANG

N=0
for n in ${SRC}
do
    o=$(printf '%02d.tiff' ${N})
    N=$((${N}+1))
    [ -r ${o} ] && continue
    [ -r ${n}.RAW.bz2 ] && bzcat ${n}.RAW.bz2 >${n}.RAW
    [ -r ${n}.RAW.gz ] && zcat ${n}.RAW.gz >${n}.RAW
    [ -r ${n}.RAW ] || exit 126
    fl=$(dcraw -i -v ${n}.RAW|awk '/^Focal length: /{print $3}')
    conf=${n}.ufraw
    [ -z "${CONF}" ] || conf=${CONF}.ufraw
    [ -r ${conf} ] || exit 125
    ${UFRAW} --conf=${conf} --output=${n}.tiff ${n}.RAW
    [ -r ${n}.RAW.bz2 -o -r ${n}.RAW.gz ] && rm -f ${n}.RAW
    tca="$( ( cd ../../tools/tca && echo "disp(tca('tca.db','fz50',${fl},1,0))"|octave -q|cat|head -1 ) )"
    ${FULLA} ${tca} -o ${n}.tiff ${n}.tiff
    ${CONV} ${n}.tiff \
        -background transparent \
        -rotate 2.85 \
        -compress LZW ${o}
    rm -f ${n}.tiff
done
#exit

${NONA} ${DST}.pto \
&& rm [0-9][0-9].tiff \
&& ${ENBL} -f 10260x3693 -o tmp.tiff tmp-0*.tif \
&& rm tmp-0*.tif \
&& ${CONV} tmp.tiff -crop 7360x2944+1696+80 +repage -enhance -unsharp 0x1 -compress LZW ${DST}.tiff \
&& rm tmp.tiff \
&& ${CONV} ${DST}.tiff -resize x1536 -unsharp 0x0.75+0.5+0.03 -compress LZW ${DST}.tiff \
&& ${CONV} ${DST}.tiff -quality 80 ${DST}-big.jpeg \
&& ${CONV} ${DST}.tiff -resize x768 -unsharp 0x0.75+0.5+0.03 -compress LZW ${DST}.tiff \
&& ${CONV} ${DST}.tiff -quality 80 ${DST}.jpeg \
&& ${CONV} ${DST}.tiff -resize x256 -unsharp 0x0.75+0.5+0.03 -compress LZW ${DST}.tiff \
&& ${CONV} ${DST}.tiff -gamma 1.1 -quality 90 ${DST}-small.jpeg \
&& rm ${DST}.tiff

#&& ${CCM} tmp-0006.tif tmp-0007.tif && mv tmp.tiff tmp-0007.tif \
#&& ${CCM} tmp-0007.tif tmp-0008.tif && mv tmp.tiff tmp-0008.tif \
#&& ${CCM} tmp-0008.tif tmp-0009.tif && mv tmp.tiff tmp-0009.tif \
#&& ${CCM} tmp-0009.tif tmp-0010.tif && mv tmp.tiff tmp-0010.tif \
#&& ${CCM} tmp-0010.tif tmp-0011.tif && mv tmp.tiff tmp-0011.tif \
#&& ${CCM} tmp-0006.tif tmp-0005.tif && mv tmp.tiff tmp-0005.tif \
#&& ${CCM} tmp-0005.tif tmp-0004.tif && mv tmp.tiff tmp-0004.tif \
#&& ${CCM} tmp-0004.tif tmp-0003.tif && mv tmp.tiff tmp-0003.tif \
#&& ${CCM} tmp-0003.tif tmp-0002.tif && mv tmp.tiff tmp-0002.tif \
#&& ${CCM} tmp-0002.tif tmp-0001.tif && mv tmp.tiff tmp-0001.tif \
#&& ${CCM} tmp-0001.tif tmp-0000.tif && mv tmp.tiff tmp-0000.tif \
