#!/bin/bash
# Script to prepare camera files while downloading from camera with digiKam
# (C) 2016.08.28, Uwe Koch, Herscheid
# V 0.0.2
# Change : 2016.08.29: insert help and comments
#
echo "uk_digikam : The tool to prepare digital camera files "
echo "             (C) Uwe Koch, Herscheid, www.uwe-koch.de"
echo " "

# Get parameters PATH and NAME
pfad=$1
name=$2

# Prepare internal parameters
type=$(echo $name | cut -d . -f 4)
bild=$(echo $name | cut -d . -f 1-3)
img=$(echo $name | cut -b 22-29)
pref=$(echo $name | cut -b 1-21)

# Get date
datum=`date +'%Y.%m.%d_%H%M'`
jahr=`date +'%Y'`

# Define destination folder for original copy
zielorig='/srv/Originale/'

# Change to current download folder
cd $pfad

# Get digikam generated time stamp of current file
# and set timestamp to file date
stamp=$(echo $name | cut -b 1-4,6-7,9-10,12-15)".00"
touch -t $stamp $name

# If RAW files exist, update filename and timestamp to current timestamp
# of JPG file
if [ -f '1970.01.01_010000_UK_'$img'.cr2' ]
  then
    mv '1970.01.01_010000_UK_'$img'.cr2' $pref$img'.cr2'
    stamp=$(echo $pref$img'.cr2' | cut -b 1-4,6-7,9-10,12-15)".00"
    touch -t $stamp $pref$img'.cr2'
fi


# Copy to original folder for backup of unchanged files
if [ "$type" == "jpg" ]
  then               
	cp $name $zielorig'jpg/'
  elif [ "$type" == "cr2" ]
    then
      cp $name $zielorig'raw/'
  elif [ "$type" == "mp4" ]
    then
      cp $name $zielorig'mov/'
  else 
    cp $name $zielorig 
fi
    
# End
