#!/bin/bash

# *************************************************************************** #
# tgall-get_images - copy images in a tgall managed photo gallery             #
# Copyright (C) 2004,2012 Elena Grandi                                        #
#                                                                             #
# This program is free software; you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation; either version 2 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program; if not, write to the Free Software                 #
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                   #
# *************************************************************************** #
# Usage:                                                                      #
#                                                                             #
# tgall-get_images SOURCEDIR                                                  #
# *************************************************************************** #

# ****** Dependencies ******************************************************** #
# jhead: http://www.sentex.net/~mwandel/jhead/
JHEAD=/usr/bin/jhead
# jpegtran: http://jpegclub.org/jpegtran/
JPEGTRAN=/usr/bin/jpegtran
# git-annex: http://git-annex.branchable.com/
GIT_ANNEX=/usr/bin/git-annex

# Check that the dependencies are available

for DEP in $JHEAD $JPEGTRAN $GIT_ANNEX 
do
    if [ ! -x $DEP ]
    then
        echo "This script requires " $DEP
        exit 1
    fi
done

# ****** Error codes ********************************************************* #

E_OPTERROR=64 # command line usage error
E_CANTCREAT=73 # can't create some file

if [ $# -lt 1 ]
then
    echo "usage: `basename $0` SOURCEDIR"
    exit $E_OPTERROR
fi

for DIR in "$@"
do
    if [ -d "$DIR" ]
    then
        for IMG in $(find "$DIR" -iname *.jpg)
        do
            if ( jhead $IMG | grep "^Date" > /dev/null )
            then
                IDATE=$($JHEAD $IMG | awk '/^Date/ { print $3 }' | tr -d ':')
                ITIME=$($JHEAD $IMG | awk '/^Date/ { print $4 }' | tr -d ':')
                if mkdir -p $IDATE
                then
                    FNAME=$ITIME-$(basename ${IMG,,})
                    cp "$IMG" "$IDATE"/"$FNAME"
                    chmod 644 "$IDATE"/"$FNAME"
                    jhead -autorot "$IDATE"/"$FNAME"
                    git-annex add "$IDATE"/"$FNAME"

                else
                    echo "Could not copy files to $IDATE: not a directory"
                    exit $E_CANTCREAT
                fi
            else
                echo "Ignoring $IMG: no exif data"
            fi
        done
    fi
done
