#!/bin/bash
#
# Third step in NEF-workflow. 
# 
# Håvard Berland <berland@pvv.ntu.no>
# http://www.pvv.ntnu.no/~berland
#
# Details of this script:
# 
# 1. Runs dcraw in chosen default mode for finished jpg-pictures.
# 2. Extracts exif-tags from nef-files and write them to preview-jpg-pictures.
# 3. Deletes *pre.jpg files from step 1 of workflow.
# 4. Remove Orientation-exif-tag as dcraw has already rotated the picture.
#
# Usage:
# $ runnef      # will process all nef-files in current directory
# $ runnef neffile1.nef neffile2.nef # processes chosen files.

###############################################
# User servicable parts:

#dcrawoptions="-h -q 3 -w -B 2 4" 
dcrawoptions="-h -q 3 -w"
convertoptions="-quality 95 -sharpen 5"


################################################
# Function used in this script, takes one 
# nef-file as the argument.
runneffile () {
    neffile=$1
    echo -n "Processing $neffile... "
    prejpgfile=${neffile%.nef}pre.jpg
    jpgfile=${neffile%.nef}.jpg
    rm -f $prejpgfile # This file should go away now.
    if [ ! -f "$jpgfile" ] ; then
	dcraw $dcrawoptions -c $neffile \
	    | convert $convertoptions - $jpgfile && \
	    neftags2jpg $neffile $jpgfile  && \
	    jhead -norot $jpgfile && \
	    echo "done"	
    else
	echo "SKIPPED, $jpgfile exists."
    fi
}


###################################################
# Entry point of this script.

# If no arguments, process all nef-files in current dir.
if [ $# == 0 ]; then
    for neffile in `ls *.nef`
      do
      runneffile $neffile
    done
else
# If shell arguments are supplied, assume it is filenames.
    for neffile in $@
      do
      if [ !  ${neffile%.nef} == $neffile ] ; then
	  runneffile $neffile
      else
	  echo "$neffile does not have correct extension"
      fi
    done
fi


