#!/bin/sh
# $Id: getnames,v 1.12 2008/02/10 00:27:03 grog Exp grog $
#
# Prompt for names for images
#
if [ -f WARNING ]; then
  echo
  echo '*** WARNING ***'
  echo
  cat WARNING
  echo -n 'Do you want to continue? '
  read RESPONSE
  if [ "$RESPONSE" != "y" ]; then
    exit 1
  fi
fi
FILES=`ls -1rt *.jpg *.gif *.JPG *.GIF *.tiff *.TIFF`
# Display the images on default display
xv $FILES &
# JPEG compression quality
if [ "$QUALITY" = "" ]; then
  QUALITY=75
fi
SCRIPT=script.$$.sh
rm -f 
for i in $FILES; do
  ROTATION=0
  # Check the exif file for hints about rotation, which might or might
  # not be correct.
  case `exif $i | grep "Orientation" | awk '{print $2}'` in
    \|right)
      ROTATION=90;;

    \|bottom)
      ROTATION=180;;

    \|left)
      ROTATION=270
  esac
  if [ $ROTATION -ne 0 ]; then
    echo -n "New name for $i (will rotate $ROTATION°):	"
  else
    echo -n "New name for $i:	"
  fi
  read NAME ROT GAMMA
  if [ "$NAME" != "" ]; then
      
    # Handle rotations.  The default is what the exif file tells us.  If
    # we want to override it, specify - for rotation.  Any other
    # rotation value overrides the exif file.  ROTSPEC is the parameter
    # handed to convert.
    ROTSPEC=
    if [ "$ROT" = "" ]; then
      if [ $ROTATION -ne 0 ]; then
        ROTSPEC="-rotate $ROTATION"
      fi
    else if [ "$ROT" != "-" ]; then
        ROTSPEC="-rotate $ROT";
      fi
    fi
    if [ "$GAMMA" != "" ]; then
       GAMMA="-gamma $GAMMA"
    fi
    echo convert -quality $QUALITY $ROTSPEC $GAMMA $i $NAME >> $SCRIPT
# reinstate modification time stamp
    echo touch -r $i $NAME >> $SCRIPT
  fi
done
if [ -f $SCRIPT ]; then
  # Get a dscription for these photos
  if [ -f description ]; then   # already have one,
    echo Current description:
    cat description
  fi
  echo Enter a description for the index:
  read DESCRIPTION
  if [ "$DESCRIPTION" = "" ]; then
    if [ -f description ]; then   # already have one,
      echo Keeping existing description:
      cat description
      echo
    else
      echo No description
    fi
  else
    echo $DESCRIPTION > description
  fi
  echo Script $SCRIPT:
  cat $SCRIPT
  echo -n "Ready to execute this script? "
  read RESPONSE
  if [ "$RESPONSE" = "y" ]; then
    . $SCRIPT
  fi
else
  echo No script collected 
fi
