#!/bin/sh
# Create an HDR photo
# $Id: HDR,v 1.22 2024/09/06 02:04:30 grog Exp $
ECHO=
echo WARNING: This script is out of date, uses align-image-stack and enfuse.
if [ $# = 0 ]; then
    echo Usage: $0 [-g count] result component-images
    echo "   -g count: merge in groups of count images, requires 'by' program"
  exit 1
fi
if [ "$1" = "-g" ]; then      # group by...
  GROUPBY=$2
  shift
  shift
  RESULT=$1
  shift
  while [ "$1" != "" ]; do
    THISGROUP=$GROUPBY
    INPUTS=
    OUTPUT=
    while [ $THISGROUP -gt 0 ]; do
      INPUTS="$INPUTS $1"
      OUTPUT="$OUTPUT$1"
      shift
      THISGROUP=`expr $THISGROUP - 1`
    done
  echo HDR $OUTPUT $INPUTS
  SRC="$SRC $OUTPUT"
  done | by 3
  exit
else
  RESULT=$1
fi
if [ -e $RESULT ]; then
  echo $RESULT already exists
  exit 1
fi
if [ -d /photowork ]; then	# we have a dedicated work directory,
  TMP=/photowork/tmp/$RESULT.tmp # use it
else				# use this directory
  TMP=$RESULT.tmp
fi
shift
# Choose middle of exposure range for EXIF
MID=`expr \( $# + 1 \) / 2`
ANCHOR=$1  			# at least for modern Olys, first image is middle of range
while [ $# -gt 0 ]; do
  if [ -e $1 ]; then
    FILES="$FILES $1"
  else
    echo $1 not found
  fi
  shift
done
echo Creating $RESULT from $FILES
xtset Creating $RESULT
$ECHO nice align_image_stack -a $TMP $FILES
$ECHO nice enfuse -o $RESULT $TMP*
$ECHO exifcopy $ANCHOR $RESULT
$ECHO rm $TMP*
