#!/bin/sh
# getpics.sh by Frank "yogan" Blendinger (fb@intoxicatedmind.net)
#
# Downloads all files from your digicam with gphoto2 and renames them.
#
# Usage: getpics.sh DIRNAME [BASENAME]
#	A directory DIRNAME will be created and all files from the camera are
#	put there.
# 	If the optional parameter BASENAME is given, the pictures will be
# 	renamed to BASENAME_-_<num>.jpg. Videoclips will be renamed to
# 	BASENAME_-_clip_<num>.avi

if [ ! "$1" ] ; then
	echo "No directory name given."
	echo "Usage: getpics.sh DIRNAME [BASENAME]"
	exit 1
fi

if [ -d "$1" ] ; then
	echo "Directory '$1' already exists, aborting."
	exit 2
else
	mkdir "$1" && cd "$1"
fi

if ! gphoto2 --get-all-files ; then
	echo "Could not download files, aborting."
	cd .. && rmdir "$1"
	exit 3
fi
echo "Downloaded `ls *.JPG | wc -l` pictures and `ls *.AVI | wc -l` video clips."

if [ "$2" ] ; then
	echo -n "Renaming files... "
	i=1;
	for file in *.JPG; do
		if [ $i -le 9 ] ; then num=00${i} ;
		else
			if [ $i -le 99 ] ; then num=0${i} ;
			else num=$i; fi ;
		fi
		mv $file "${2}_-_${num}.jpg"
		let "i+=1"
	done
	i=1;
	for file in *.AVI; do
		if [ $i -le 9 ] ; then num=00${i} ;
		else
			if [ $i -le 99 ] ; then num=0${i} ;
			else num=$i; fi ;
		fi
		mv $file "${2}_-_clip_${num}.avi"
		let "i+=1"
	done
	echo "done."
fi

exit 0

