#/bin/bash
####################################################################### 
# Name: overlay.sh
#
# Requirements: Imageshack
#
# Description: Given a directory of images, overlay.sh will create two
# subdirectories, "overlayed" and "thumbnails" and will resize photos
# to 120x... for the thumbnails, and will resize to 800x... and
# overlay a template for the "overlayed" subdirectory.
# The script will not duplicate work. e.g. if the image has already
# been thumbnailed and overlayed, it will be skipped. If either the
# thumbnail or overlay does not exist, both will be redone. A logfile
# will be output of the file processed and the time taken.
# 
# Usage: overlay.sh overlay.png /path/to/images/
#
# Notes: 
# IMPORTANT: paths must have final '/'.
# For example: /path/to/files/ 
# where the path ends in '/' NOT /path/to/files
#
# Warning: This script can be very cpu intensive. On slow computers,
# it may take a very long time to run on 100+ images.
#
# Author: Andrew Grieser agrieser(at)gmail(dot)com
# Copyright 2007
#
# License: GPL
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
####################################################################### 

# start by defining a function called 'usage' which will be displayed
# if the inputs are incorrect or nonexistant

function usage() {
echo
echo
echo "overlay.sh"
echo "usage: overlay.sh /path/to/template /path/to/photos/"
echo
echo "This script allow you to overlay templates onto images"
echo "in the command line"
echo
echo "example: overlay.sh ~/pictures/templates/001.png ~/pictures/weekend/"
echo
}

# Check to see if inputs are correct
# If inputs are nonexistant, or noncompatable, run the function usage
# and exit the script

if [ $# -lt 2 ]; then
        usage; exit
elif ! [ -a "$1" ] || [ -d "$1" ]; then
	usage; echo "$1 is bad"; exit
elif ! [ -d "$2" ]; then
	usage; echo "$2 is bad"; exit
fi



# Store the second string passed as $path (the path to the files)
path=$2

# Store the first string passed as $overlay (the overlay to be used)
overlay=$1

# cd into that path
cd $path


# Check to see if a subdirectory has already been made. If it hasn't, make one
if ! [ -d "${path}overlayed" ]; then

mkdir ${path}overlayed

fi

if ! [ -d "${path}thumbnails" ]; then

mkdir ${path}thumbnails

fi


# Create (or append if already exists) the current date to a logfile.
# This will allow the user to review what was done at a later date. It also
# provides a way to see how long the script took to run

echo "##########`date +'%m-%d-%Y__%T'`##########" >> ${path}logfile.txt

# Begin a for loop that resizes the images and saves them to the subdirector
# 'overlayed'. This leaves the origional images untouched

for img in $(ls -I "overlayed" -I "thumbnails" -I "*.txt" $path); do

# seperate the file name from the extension and store it as $name
name=${img%.*}

# seperate the extension from the file name and store it as $extension
extension=${img##*.}

# create an outfile name which preserves the origional name, and just adds 'small' to it
outfile1=${path}overlayed/${name}_small.${extension}
outfile2=${path}thumbnails/${name}_thumb.${extension}


# Begin an 'if' statement to check if this image has already been resized
# If it has, skip it, if not then resize it
# This is important, as this script is quite cpu intensive. Don't duplicate
# work that has already been done.

if ! [ -a "$outfile1" ] || ! [ -a "$outfile2" ]; then

# echo to screen the file being processed
echo "$name"

# echo to logfile the file being processed
echo "$outfile1" >> ${path}logfile.txt


# resize the image, and save it as $outfile, which is in a subdirectory
# then preform the overlay

convert -resize 800x800 $img $outfile1

convert -resize 120x120 $img $outfile2

convert $outfile1 $overlay -gravity center -composite -format $extension $outfile1

fi

done

#add a line to show when the script finished. This allows a user to see how
# long the script took to run

echo "##########`date +'%m-%d-%Y__%T'`##########" >> ${path}logfile.txt

