#!/bin/bash
#microdown *.jpg ||exit 1
time (

# CNC always outputs good images now
#microdown *

if [ -f out.pto ] ; then
   mv out.pto out_old.pto
fi

echo
echo
echo '**********************************'
time pr0nstitch out.pto $( (shopt -s nullglob; echo *.jpg *.png) ) "$@" ||exit 1

#if [ $(md5sum stitch_failures.json |cut -c1-32) != 63e4be57043380c84996254fe8120e04 ]
if cat stitch_failures.json |grep '"critical_images": 0'
then
    echo 'No stitch failures'
else
	echo 'WARNING: failure'
	# used to exit when missing images was a bigger deal
	# now that using (quick) pre-optimizer might as well
	# make a best effort
	#exit 1
fi

echo
echo
echo '**********************************'
echo 'No failures, optimizing'
time pr0npto --pre-opt out.pto

# fit to screen
time pano_modify --fov=AUTO --canvas=AUTO -o out.pto out.pto

echo
echo
echo 'Done!  Open with Hugin to adjust size and rotate'
)

pr0nhugin out.pto

