#!/usr/local/bin/bash
# $Id: gn,v 1.17 2012/02/13 23:21:19 grog Exp $
#
# Prompt for names for images
#
# TOOLS=/home/grog/Photos/Tools
if [ -f WARNING ]; then
  echo
  echo '*** WARNING ***'
  echo
  cat WARNING
  echo -n 'Do you want to continue? '
  read -e RESPONSE
  if [ "$RESPONSE" != "y" ]; then
    exit 1
  fi
fi
if [ "$AUTHOR" == "" ]; then
  AUTHOR="Greg Lehey"
fi
# Do we need this any more?
# XXX # Check if we need to convert Olympus raw format files
# XXX # We have 4 kinds of JPEG:
# XXX # $i.JPG    JPEG from Olympus
# XXX # $i.JPEG   JPEG from automatic ufraw
# XXX # $i.jpg    JPEG from custom ufraw
# XXX # $i.jpeg   forgotten this one
# XXX FILES=
# XXX if [ "`ls orig/${1}*.ORF`" != "" ]; then
# XXX   cd orig
# XXX   for i in ${1}*.ORF; do
# XXX     b=`basename $i .ORF`
# XXX     if [ ! -f $b.jpg -a ! -f $b.jpeg -a ! -f $b.JPG -a ! -f $b.JPEG -a ! -f $b.embedded.jpg ]; then
# XXX        FILES="$FILES $b"
# XXX     else
# XXX        echo $b already converted.
# XXX     fi
# XXX   done
# XXX   for f in $FILES; do echo $f; done | by 5 mkjpeg2
# XXX   cd ..
# XXX fi
# These shouldn't be in the base directory.  Move them to orig before we get start.
# Don't print an error message if there are none; that's a feature, not a bug.
mv *.jpg *.gif *.JPG *.JPEG *.GIF *.tiff *.TIFF orig 2>/dev/null
# Source files.  We don't include .jpeg here, because that's what we
# call generated JPEGs.  We still have an issue with GIF files, but
# that's in the "too hard" category.
echo args $#
if [ "$1" = "-r" ]; then
  ROTATE=1
  shift
else
  ROTATE=0
fi
if [ $# -gt 0 ]; then
  FILES=$*
else
  FILES=`ls -1rt orig/*.jpg orig/*.JPG orig/*.gif orig/*.JPEG orig/*.GIF orig/*.tiff orig/*.TIFF orig/*,embedded.jpg`
fi
# Display the images on default display
xv $FILES &
# Now go through the files and prompt
SCRIPT=makejpeg
echo Writing script to $SCRIPT
# We're going to grep through the script later, so make sure something's there.
if [ ! -f $SCRIPT ]; then
  touch $SCRIPT
fi
CWD=`pwd`
PARENT=`dirname $CWD`
for SRCJPEG in $FILES; do
  # Remove appendages
  JPG=`basename $SRCJPEG .JPEG`
  JPG=`basename $JPG .jpg`
  JPG=`basename $JPG .JPG`
  ENTRY=`grep $JPG $SCRIPT`	# look for an entry in the script file
  if [ "$ENTRY" != "" ]; then
    echo $JPG already present in $SCRIPT:
    echo $ENTRY
    continue
  fi
  ROTATION=0
  if [ $ROTATE -ne 0 ]; then
    # Check the exif file for hints about rotation, which might or might
    # not be correct.  
    # Earlier I had written:
    #    XXX This is currently complicated by the way we
    #    copy EXIF info from the raw file, so we won't do it if the file
    #    name starts in P.
    # That's a pain, so I've removed it:
    #  if [ ${JPG:0:1} != 'P' ]; then
    IFS="	"
    set -- `exiftool -T -orientation -aperture -shutterspeed -iso -lensid -focallength $SRCJPEG`
    IFS=" 	"
    APERTURE=$2
    SHUTTER=$3
    ISO=$4
    LENS=$5
    FOCAL_LENGTH=$6
    case $1 in
        "Rotate 90 CW")
          ROTATION=90;;
    
        "Rotate 270 CW")
          ROTATION=270;;

        \|bottom)
          ROTATION=180;;
    
        \|left)
          ROTATION=270
      esac
# XXX  echo ROTATION $ROTATION APERTURE $APERTURE LENS $LENS FOCAL_LENGTH $FOCAL_LENGTH 
  fi
  exifx $SRCJPEG
  echo Author $AUTHOR
  if [ $ROTATION -ne 0 ]; then
    echo -n "New name for $SRCJPEG (will rotate $ROTATION°):	"
  else
    echo -n "New name for $SRCJPEG:	"
  fi
  read -e BASE ROT SELECT
  if [ "$BASE" = "-a" ]; then
     AUTHOR="$ROT $SELECT"
     if [ $ROTATION -ne 0 ]; then
	 echo -n "New name for $SRCJPEG (will rotate $ROTATION°):	"
     else
	 echo -n "New name for $SRCJPEG:	"
     fi
     read -e BASE ROT SELECT
  fi
#  echo  "BASE " $BASE "ROT" $ROT "SCORE" $SCORE
  if [ "$BASE" != "" ]; then
    TYPE=`identify $SRCJPEG | awk '{print $2}' | tr "[:upper:]" "[:lower:]"`
    EXIF=$BASE.exif
    NAME=$BASE.$TYPE          # create an extension
    # 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.
    if [ "$ROT" != "-" -a "$ROT" != "" ]; then
        ROTATION=$ROT
    fi
    if [ "$SCORE" = "" ]; then
      SCORE=3
    fi
# The entries here are:
# Base name of the source file (we'll try a number of JPEG-like extensions in sequence)
# New name
# Rotation
# Gamma
      cat <<EOF >> $SCRIPT
$JPG $NAME $ROTATION $SCORE
EOF
# Now check for missing data
# XXX call setexif.  Omit for now.
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 -e 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 [yd]? "
  read RESPONSE
  if [ "$RESPONSE" = "y" ]; then
    by 5 ${PHOTOTOOLS}/imgconvert < $SCRIPT
  elif [ "$RESPONSE" = "d" ]; then
    rm $SCRIPT
    echo $SCRIPT removed    
  fi
else
  echo No script collected 
fi
