#!/bin/bash
#
# getpix.sh
# http://clevername.homeip.net/scripts/getpix
# Todd Slater
#
# Get pictures from digital camera and copy them to hard drive.
# And do some other stuff.
#
# Revised 8 January 2003
#
# REQUIREMENTS:
#
# JHEAD in your path (or modify jhead command in script)
# Download JHEAD: http://www.sentex.net/~mwandel/jhead/
# ImageMagick
#
# OPTIONS:
#
# [-c] generate contact sheet, default is 4x4 @ 200x150
# [-n] rename pictures using NAME-nnn, i.e. xmas-001.jpg, xmas-002.jpg
# [-r] resize pictures, default width is 800, can be overriden with -w
# [-t] the name you want to give to the target directory. If you don't 
#		specify this, the default is taken from the date
#		YYYY_MM_DD.
# [-w] width in pixels to resize pictures.
# [-z] zip resized pictures.
#
# Note that if you resize the images, they will be placed
# in a subdirectory; you are not losing the original size.
#
# SET UP SYSTEM VARIABLES

CAM="/mnt/fujifilm"			# Mountpoint for your camera
SRC="/mnt/fujifilm/dcim/100_fuji"	# The absolute path to the pictures on your
				# camera
ROOT="/home/toader9x/Documents/pics/fuji"	# The root/base directory where you store
				# your images (like ~/images)
EXT=".jpg"			# Or .JPG, or .jpe (your camera's convention)

# That's all you *have* to set up

# IMAGEMAGICK VARIABLES

TILEW=4                 # Number of images across
TILEH=4                 # Number of images down
GEOMETRYW=200           # Width of image tiles
PADX=2                  # Padding in the x direction
PADY=5                  # Padding in the y direction
LABEL=`basename "%f"`   # Print filenames under tiles
BGCOLOR=white
RENUMBER=0
FONT="-mdk-helvetica-medium-r-normal--13-130-75-75-p-71-tcvn-5712"
TARGET=`date +%Y_%m_%d`
WIDTH=800
CONTACT=false
RESIZE=false
ZIP=false
INCREMENT=1
USAGE="$0 [-c] [-n name] [-r] [-t target] [-w width] [-z]"

while getopts cn:rt:w:z OPTION ;
do
	case "$OPTION" in
		c) CONTACT="true" ;;
		n) NAME="$OPTARG" ;;
		r) RESIZE="true" ;;
		t) TARGET="$OPTARG" ;;
		w) WIDTH="$OPTARG" ;;
		z) ZIP="true" ;;
		?) echo $USAGE; exit 1 ;;
	esac
done

clear

# Don't zip if pictures are not resized

if [ $ZIP == "true" ] && [ $RESIZE == "false" ] ; then
	echo "Error: Zip is only performed on resized pictures."
	exit 1
fi

mount $CAM

# Check to see if target directory exists. If so, apend -n to name

while [ -d "$ROOT/$TARGET" ] ;
do
	INCREMENT=$(($INCREMENT+1))
	TARGET=`echo $TARGET | awk -F'-' '{print $1}'`-$INCREMENT
	echo "Target exists, using $TARGET"
done

mkdir $ROOT/$TARGET

BIGPATH="$ROOT/$TARGET"

echo "Copying images . . ."
cp $SRC/*$EXT $BIGPATH &&
echo "Finished copying images."

umount $CAM
echo "You can disconnect the camera now."

chmod -x $BIGPATH/*$EXT		# For some reason my pix have the executable
				# bit set

# Use -n NAME to rename pictures, else rename according to date

if [ $NAME ] ; then
	NUMBER=1
	echo "Renaming images using $NAME-###"
	for image in `ls $BIGPATH`
	do
		NUMCOUNT=`printf "%03d" $NUMBER`
		mv $BIGPATH/$image "$BIGPATH/$NAME-$NUMCOUNT$EXT"
		NUMBER=$(($NUMBER+1))
	done
else
	# jhead does other stuff you might want to add here, too
	jhead -n%Y_%m_%d-%H_%M_%S $BIGPATH/*$EXT
	echo "Images have been renamed."
	echo "Images are in $BIGPATH"
fi
shift $((OPTIND - 1))

if [ $RESIZE == true ] ; then
	mkdir $BIGPATH/$WIDTH
	cp $BIGPATH/*$EXT $BIGPATH/$WIDTH
	echo "Resizing . . ."
	mogrify -geometry $WIDTH $BIGPATH/$WIDTH/*$EXT
fi
	
if [ $ZIP == true ] ; then
	echo "Zipping images . . ."
	zip $BIGPATH/$WIDTH/$TARGET-$WIDTH.zip $BIGPATH/$WIDTH/*$EXT
fi

if [ $CONTACT == true ] ; then
	totalimages=`ls -1 $BIGPATH | wc -l`
	maximages=$(($TILEW*$TILEH))
	c=1
	e=$maximages
	count=1
	if [ $totalimages -le $maximages ] ;
	then
		echo "Generating contact sheet . . ."
		montage -background $BGCOLOR -title $BIGPATH -tile $TILEW\x$TILEH -geometry $GEOMETRYW+$PADX+$PADY -label $LABEL $BIGPATH/*$EXT $BIGPATH/index$EXT
	else
		for i in `find $BIGPATH -type f -iname *$EXT`
		do
			echo $i >> $BIGPATH/dirlist.$$
		done
		sort $BIGPATH/dirlist.$$ > $BIGPATH/dirlist2.$$
		mv $BIGPATH/dirlist2.$$ $BIGPATH/dirlist.$$
		while [ $c -le $totalimages ] ; do
			numcount=`printf "%02d" $count`
			sed -n $c,$e\p $BIGPATH/dirlist.$$ > $BIGPATH/index-$numcount
			count=$(($count+1))
			c=$(($e+1))
			e=$(($e+$maximages))
		done
		rm $BIGPATH/dirlist.$$
		of=`ls $BIGPATH/index-* | wc -l`
		for l in $BIGPATH/index-*
		do
			inumber=`basename $l | sed s/index-//`
			TITLE="$BIGPATH page $inumber of $of"
			echo "Creating index for $l . . ."
			montage -background $BGCOLOR -title "$TITLE" -tile $TILEW\x$TILEH -geometry $GEOMETRYW+$PADX+$PADY -label $LABEL -font $FONT `cat $l` $l$EXT
			wait
			rm $l
		done
	fi
fi
echo "Done!"
gqview $BIGPATH &
exit
