#!/bin/bash

###
### QUICK IMAGE VIEWER "qiv-command" file
### http://qiv.spiegl.de/
###
### This file will be launched by QIV if you push 0-9 (or any other free key)
### NEW:  Now you can use all free keys for qiv-command.
### NEW:  If you press "X" followed by any string,
###       qiv-command is called with X and this string
###
### Just put it in a directory that is in your path
###
### Syntax: qiv-command [pushed_number] [image_filename_with_full_path]
###
### Note:
###  If the first line of the output is like "NEWNAME=xxxxxxx" then qiv
###  thinks that the filename of the currently displayed image has
###  changed to this new name and updates its internal filelist.
###  This is very useful when using qiv-command to rename files.

#
# some examples by Andy Spiegl <qiv.andy@spiegl.de>
# Created:     12. Mai 2002
# Last change: 23. April 2007
#
# needs jhead to rotate JPEG without loss of Exif Jpeg headers
# jhead is:
# Program for extracting Digicam setting information from Exif Jpeg headers
# used by most Digital Cameras.  v2.6 by Matthias Wandel, Dec 26 2006
# http://www.sentex.net/~mwandel/jhead  mwandel@sentex.net

pressed_key=$1
filename=$2

echo "You pressed the $pressed_key key while watching $filename"

case "$pressed_key" in

  0)
    # do something with the image ($2) ->
    #
    # example: copy
    # cp "$2" "/home/myfiles/download/";
    #
    # example: move
    # mv "$2" "/home/myfiles/download/";
    # echo "NEWNAME=/home/myfiles/download/$2";
    #
    # example: show EXIF Header using "exiftool" or "jhead"
    # exiftool "$filename"
    # jhead "$filename"

    echo -ne "0 was pushed.\nStart your own programs by editing the\n\"$0\" file!";
    ;;


  1|2|3|4|5|6|7)
    echo -ne "$pressed_key was pushed.\nStart your own programs by editing the\n\"$0\" file!";
    ;;

  8) # lossless rotation of JPG, without losing EXIF tags
    echo 2>&1 "Rotating to the left."
    jhead -cmd "jpegtran -perfect -rotate 270 -progressive -outfile &o &i" $filename >/dev/null
    # set timestamp of file to Date/Time when the photo was taken
    jhead -ft $filename >/dev/null
    ;;

  9) # lossless rotation of JPG, without losing EXIF tags
    echo 2>&1 "Rotating to the right."
    jhead -cmd "jpegtran -perfect -rotate 90 -progressive -outfile &o &i" $filename >/dev/null
    # set timestamp of file to Date/Time when the photo was taken
    jhead -ft $filename >/dev/null
    ;;

#  # with "^"-prefix: forced (i.e. possibly NOT lossless) rotation
#  ^8) # rotate JPG, but possibly losing quality
#    echo 2>&1 "Forcing rotation to the left."
#    jhead -cmd "jpegtran -rotate 270 -progressive -outfile &o &i" $filename >/dev/null
#    # set timestamp of file to Date/Time when the photo was taken
#    jhead -ft $filename >/dev/null
#    ;;

#  ^9) # rotate JPG, but possibly losing quality
#    echo 2>&1 "Forcing rotation to the right."
#    jhead -cmd "jpegtran -rotate 90 -progressive -outfile &o &i" $filename >/dev/null
#    # set timestamp of file to Date/Time when the photo was taken
#    jhead -ft $filename >/dev/null
#    ;;

  V)
    qiv -h
    ;;

  *)
    echo "Usage: $0 [0-9] or any other free key..."
    echo "Within qiv you simply press a key from 0-9 or any other free key!"
    echo "$pressed_key not defined yet.  Quitting."
    exit 1
esac


exit 0
