#!/bin/sh
# $Id: getnames,v 1.18 2008/08/21 03:16:36 grog Exp $
#
# 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
# Check if we need to convert Olympus raw format files
if [ "*.ORF" != "" ]; then
  echo Converting raw images to JPEG
  . /home/grog/Photos/mkjpeg
fi
# 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.
FILES=`ls -1rt *.jpg *.gif *.JPG *.JPEG *.GIF *.tiff *.TIFF`
# Display the images on default display
xv $FILES &
# JPEG compression quality
if [ "$QUALITY" = "" ]; then
  QUALITY=75
fi
SCRIPT=script.$$.sh
echo Writing script to $SCRIPT
for SRCJPEG in $FILES; do
  ROTATION=0
  # Check the exif file for hints about rotation, which might or might
  # not be correct.
  case `exif $SRCJPEG | grep "Orientation" | awk '{print $2}'` in
    \|right)
      ROTATION=90;;

    \|bottom)
      ROTATION=180;;

    \|left)
      ROTATION=270
  esac
  exif $SRCJPEG | grep "Date and Time   "
  if [ $ROTATION -ne 0 ]; then
    echo -n "New name for $SRCJPEG (will rotate $ROTATION°):	"
  else
    echo -n "New name for $SRCJPEG:	"
  fi
  read BASE ROT GAMMA
  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.
    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
    # We want to be able to delete .JPEG files and recreate them at
    # will.  When we run this script, $SRCJPEG has to exist, but not
    # necessarily when we run the script we have created.
    JPG=`basename $SRCJPEG .JPEG`
    if [ $JPG != $SRCJPEG ]; then
      cat <<EOF >> $SCRIPT
# $NAME
if [ ! -s $NAME ]; then
  SRC=$JPG.jpg
  if [ ! -f \$SRC ]; then
    SRC=$SRCJPEG
    make -f ../Makefile \$SRC
  fi
EOF
    else
      cat <<EOF >> $SCRIPT
# $NAME
  if [ ! -s $NAME ]; then
    SRC=$JPG
EOF
    fi
    cat <<EOF >> $SCRIPT
  convert -quality $QUALITY $ROTSPEC $GAMMA \$SRC $NAME
  touch -r \$SRC $NAME  # reinstate modification time stamp
  exif \$SRC > $EXIF
  if [ ! -s $EXIF ]; then   # no exif data
    rm $EXIF
  else                      # give it the same mod time stamp
    touch -r \$SRC $EXIF    # reinstate modification time stamp
  fi
fi
EOF
  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 [yd]? "
  read RESPONSE
  if [ "$RESPONSE" = "y" ]; then
    . $SCRIPT
  elif [ "$RESPONSE" = "d" ]; then
    rm $SCRIPT
    echo $SCRIPT removed    
  fi
else
  echo No script collected 
fi
