#!/bin/bash
#
# First preliminary step in NEF-workflow. Makes jpg-images quick
# for selection process.
# 
# Håvard Berland <berland@pvv.ntu.no>  
# http://www.pvv.ntnu.no/~berland
#
# Details of this script:
#
# 1. Makes all filenames lowercase.
# 2. Changes the _ in the D70s-filenames to a chosen number between 0 and 9
# 3. Runs dcraw in cheapest mode for preview-jpg-pictures
# 4. Extracts exif-tags from nef-files and write them to preview-jpg-pictures.
# 5. Remove Orientation-exif-tag as dcraw has already rotated the picture.
#
# Next step of workflow is to run qiv, where the delete-command also
# removes the nef-file.
#
# This script performs its operations on all nef-files present
# in current directory.
#

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

digit5=0 # The fifth digit in filenames.

##############################################

# 1: Make all filenames lowercase
for file in `ls *`
	do
	newname=`echo $file | tr [A-Z] [a-z]`
	mv -u -f $file $newname
done	

# 2: Add a fifth significant digit to filenames
for file in `ls *_*.jpg *_*.nef *.bib 2>/dev/null`
         do
         newname=`echo $file | tr [_] [${digit5}]`
	 mv $file $newname
done



# 3: Dcraw in fastest mode
# 4: Exif tags
# 5: Clear exif rotation-tag.
for neffile in `ls *.nef`
	do
	echo "Processing $neffile"
	prejpgfile=${neffile%.nef}pre.jpg
	jpgfile=${neffile%.nef}.jpg
	if [ ! -f "$jpgfile" ] ; then
	    nice dcraw -h -q 0 -w -c $neffile | convert - $prejpgfile && \
		neftags2jpg $neffile $prejpgfile  && \
		jhead -norot $prejpgfile 
	else
	    echo "Skipped, $jpgfile exists"
	fi
done

