#!/bin/bash
#
# Script run by qiv when a numeric key is pressed (0-9)
#
# Argument 1 ($1) is the key pressed (0-9)
# 
# Argument 2 is the filename, without full path, active when the numeric
# key was pressed.

case $1 in
# Rotate 90 degrees clockwise.
   1)
	mkdir -p .qiv-trash
	mv $2 .qiv-trash/$2
	jpegtran -copy all -rotate 90 -trim < .qiv-trash/$2  > $2 
	;;
# Rotate 90 degrees counter-clockwise.
   2) 
	mkdir -p .qiv-trash
	mv $2 .qiv-trash/$2
	jpegtran -copy all -rotate 270 -trim < .qiv-trash/$2 > $2 
	;;
# Display EXIF-info
   3)
	jhead $2
	;;
# Open current file in Gimp
   4)   ## Note: 
	## qiv *MUST NOT* be in fullscreen mode when using this, 
	## qiv will hang!
	gimp-remote --no-splash $2 &
	;;
# Open corresponding nef-file in Gimp (plugin for gimp needed)
   5)   ##Note: 
	## qiv *MUST NOT* be in fullscreen mode when using this, 
	## qiv will hang!
	jpgfile=$2
	filebase=${jpgfile%.jpg}
	filebase=${filebase%pre}
	neffile=$filebase.nef
	if [ -f $neffile ]; then
	    gimp-remote --no-splash $neffile &
	fi
	;;
# Move current jpg and nef to .qiv-select
   8)   ##
        mkdir -p .qiv-select
        jpgfile=$2
        filebase=${jpgfile%.jpg}
        filebase=${filebase%pre}
        neffile=$filebase.nef
        mv $jpgfile .qiv-select/
        mv -f $neffile .qiv-select/
	mv -f $neffile.bib .qiv-select/
        ;;
# Delete current jpg file AND corresponding nef-file.
   9) 	## Note:
        ##  Qiv 2.1 and above handles this better.
	mkdir -p .qiv-trash
	jpgfile=$2
	filebase=${jpgfile%.jpg}
	filebase=${filebase%pre}
	neffile=$filebase.nef
	mv -f $jpgfile .qiv-trash/
	mv -f $neffile .qiv-trash/	
	mv -f $neffile.bib .qiv-trash/ 2>/dev/null
	;;
# Unrecognized command, show usage
   *) 
	echo "Usage:"
	echo " 1: Rotate 90 degrees clockwise using jpegtran."
	echo " 2: Rotate 90 degrees counter-clockwise using jpegtran."
	echo " 3: Display EXIF-info using jhead."
	echo " 4: Open current file in Gimp (no fullscreen!)"
	echo " 5: Open corresponding nef-file in Gimp (plugin for gimp needed) (no fullscreen!)"

	echo " 8: Copy current jpg and nef to .qiv-select."
	echo " 9: Delete current jpg-file AND corresponding nef-file."
	exit 
	;;
esac
