#!/bin/bash
# Script to rename camera files with timestamp and copyright information
# (C) 2007.10.18, Uwe Koch, Herscheid
# V 0.3.2
# Change : 2006.02.05: insert help and comments
#          2007.10.18: add RAW support
#          2007.12.27: move RAW to $rawdir
#          2008.05.26: add yyyymmdd(xxx) for Nokia E51
#          2009.07.28: add *.AVI
#          2009.08.16: add *_UK_*
#
echo "uk_camera : The tool to rename digital camera files to unique file names"
echo "            and insert copyright information into the picture"
echo "            (C) Uwe Koch, Herscheid, www.uwe-koch.de"
echo " "
if [ "$1" == "-?" ]
  then
    echo "Usage       : uk_camera <year>"
    echo "Long help   : uk_camera --help"
    echo "Lange Hilfe : uk_camera --hilfe"
    exit
  elif [ "$1" == "--help" ]
    then
      echo "uk_camera renames the picture file names in the actual directory from names like IMG_01234.JPG"
      echo "          to a more usefull name like 2006.02.05_123456_IMG_01234.jpg in the format "
      echo "          yyyy.mm.dd_HHMMSS_original-name.jpg with the time and date info from the exif data."
      echo "          Then all pictures are copied unchanged to a directory for originals."
      echo "          The pictures in the actual working directory are marked with copyright information"
      echo "          in the EXIF data and in the lower right corner of the picture."
      echo " "
      echo "          If a year is entered, it is used for the copyright. If a - is entered, no year "
      echo "          will be inserted. With no parameter the actual year is used for copyright"
      exit
  elif [ "$1" == "--hilfe" ]
    then 
      echo "uk_camera benennt die Bild-Dateien im aktuellen Verzeichnis von z.B. IMG_01234.JPG um in "
      echo "          sinnvollere eindeutige Namen wie z.B. 2006.02.05_123456_IMG_01234.jpg im Format"
      echo "          jjjj.mm.tt_HHMMSS_original-name.jpg mit dem Aufnahmedatum aus den EXIF-Daten"
      echo "          Anschließend werden alle Dateien unverändert in ein anderes Verzeichnis kopiert."
      echo "          Die Bilder im aktuellen Arbeitsverzeichnis werden dann mit einer Copyright-Information"
      echo "          in den EXIF-Daten und in der unteren rechten Ecke des Bildes versehen."
      echo " "
      echo "          Wenn eine Jahreszahl als Parameter angegeben wird, wird sie für das Copyright verwendet."
      echo "          Wird ein - angegeben, so wird kein Jahr angegeben. Ohne Parameter wird das aktuelle Jahr verwendet."
      exit
  elif [ "$1" == "" ]
    then
      jahr=" `date +%G`,"
  elif [ "$1" == "-" ]
    then
      jahr=""
  else
    jahr=" $1,"
fi
#
# here are variables to be preconfigured
# 

kommentar="(C)$jahr Uwe Koch, www.Uwe-Koch.de"
origdir="Originale"
rawdir="/home/uwe/Bilder/_Annahme/_RAW/"
videodir="/home/uwe/Bilder/Video/2009/"
#
mkdir $origdir 2>/dev/null
mkdir $origdir/raw 2>/dev/null
#
# step 1
# renaming all JPG files to match YYYY.MM.DD_HHMMSS_UK_filename_.jpg
#
echo "Renaming : "
for image in `ls ????????.JPG 2>/dev/null` `ls ????????.jpg 2>/dev/null` `ls ????????.jpeg 2>/dev/null` `ls ?????????????.jpg 2>/dev/null`
do
  img=$(echo $image | cut -b -8)
  jhead -nf'%Y.%m.%d_%H%M%S_UK_'$img $image
done
#
echo "RAW-Files : "
for raw in `ls ????????.CR2 2>/dev/null` 
do
  img=$(echo $raw | cut -b -8)
  dat=$(ls ????.??.??_??????_$img.jpg | cut -b -18)
  echo "$img.cr2 --> $dat_UK_$img.cr2"
  mv $img.CR2  $dat$img.cr2
done
#
for raw in `ls ????????.cr2 2>/dev/null`
do
  img=$(echo $raw | cut -b -8)
  dat=$(ls ????.??.??_??????_$img.jpg | cut -b -18)
  echo "$img.cr2 --> $dat_UK_$img.cr2"
  mv $img.cr2  $dat$img.cr2
done
#
for raw in `ls *.cr2 2>/dev/null`
do
  echo "$raw --> $origdir/raw/$raw"
  cp $raw  $origdir/raw/$raw
  mv $raw  $rawdir
done
for video in `ls *.AVI 2>/dev/null`
do
  vid=$(echo $video | cut -b -8)
  echo "$video --> $dat$vid.avi"
  mv $vid.AVI $dat_UK_$vid.avi
done
#
# step 2
# do for all renamed files these tasks
# copy file unchanged to a different directory
# insert the comment text to exif data
# insert the comment as text into the lower right corner of the picture
# 
echo " "
echo "Tagging with copyright info :"
mkdir $origdir 2>/dev/null
# loop for all jpg files          
for img in `ls ????.??.??_*.jpg` 
do
  echo -n $img 
# insert the comment to exif data                              
  convert -comment "$kommentar"  $img $origdir/$img
  echo " ."
# insert the comment into the picture
#  convert -pointsize 12 -draw 'gravity southeast fill grey text 10,10 "(C) Uwe Koch, Herscheid"' $img UK_$img
#  echo -n "."
#  mv UK_$img $img  1>/dev/null 2>/dev/null
#  echo "."
done
echo "Done."
