#!/bin/bash
## Prof_Frink's photo tool
## This is set up for my directory structure, naming convention and ftp server.
## Use the settings at the top to change these.
## This is by far the most complex script I've written so far, so it probably
## does things in [strange|inefficient] ways.
## Uses ImageMagick's `mogrify` to resize images for upload
########################################################################
##
## Copyright (C) 2005 Alan Blanchflower (alanATalanblanchflowerDOTcoDOTuk)
##
##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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##
########################################################################
## Thoughts (Bugs, plans and the like)
## 
## [DONE] System-specific stuff assigned at start for portability
## [DONE] Use pmount instead of mount to avoid sudoing
## -NOTE> Mountpoint must be in /media for pumount to work
## [DONE] Relative dates with $datestr == [+-]*
## [DONE] Cleaned up the Ugly Hack (a bit)
## 
## [0FIX] Clean up indenting and if..fi statements
## [PLAN] Sanity-checking on eg. date entry - use while..do..done
## -NOTE> Need to understand regexes. Urgh.
## [PLAN] [Started] Assign more stuff at the start, then use something like $input to adjust them
## -NOTE> read with no options sends output to $REPLY
## -PLAN> Fully automatic option, with command-line params

mode=1					## Default mode 1 - Get photos
datefmt=%y-%m-%d			## See date(1) manpage for format syntax
device=/dev/sda1			## Default device block
mountpoint=/media/usbdisk		## Default mountpoint (in /media for pmount)
defsource=$mountpoint/DCIM/100_FUJI	## Source directory, varies with camera model
photoroot=/media/win-data/Pictures	## Root photo directory
viewer=gthumb				## Photo viewer app
webdir=web				## (sub)directory containing resized photos
upsize=800x800				## Size to resize photos to before upload
ftpserver=ftp.alanblanchflower.co.uk	## FTP server for upload
ftpuser=alanblan			## Username to log in to FTP server
ftpupdir=upload				## Remote directory to upload to

while [ 1 == 1 ] ## Return to main menu after completing each task
do
	#clear ## Uncomment to clear terminal before (re)starting
	echo "---------------------------------"
	echo "Prof_Frink's photo tool main menu"
	echo "---------------------------------"
	echo "What would you like to do?" 
	echo "1: Move photos to hard disk"
	echo "2: Copy and resize photos for web upload"
	echo "3: Upload files to $ftpserver"
	echo "Enter your choice or Q to quit [$mode]"
		read selmode
		if [ "$selmode" != "" ] ; then mode=$selmode ; else echo "" ; fi;
	case "$mode" in
	### Option 1 ### 8 inputs
		"1" )
		echo "1: Move photos to hard disk"
		echo "---------------------------"
		#echo "Enter date to use: [`date +$datefmt`]"
		#	read datestr
		#if [ "$datestr" == "" ] ; then datestr=`date +$datefmt` ; else echo "" ; fi;
		echo "Enter date to use, use [+-] for relative: [`date +$datefmt`]"
			read datestr
				if ["$datestr" == "" ] ;
					then datestr=`date +$datefmt` ;
				elif [[ "datestr" == [+-]* ]] ;
					then datestr=`date -d "$REPLY day" +$datefmt` ;
				else datestr=$datestr ;
				fi ;
		echo "Name for this photo collection:"
			read entname
		namestr=_${entname//\ /-}
		destdir=$photoroot/$datestr$namestr
		echo "Creating target directory $destdir"
		mkdir $destdir
		echo "Enter the path to the source directory: [$defsource]"
			read sourcedir
		if [ "$sourcedir" == "" ] ; then sourcedir=$defsource ; else echo "" ; fi ;
		if [ -e $sourcedir ] ; 
			then echo "" ; 
			else	## Check existance of source dir, offer to mount
				echo "Source directory does not exist, mount $device on $mountpoint? [yNo]"
				#echo "(requires sudo rights)"
				read mount
				if [ "$mount" == "y" ] ;
					#then sudo mount -t vfat -o rw,umask=0000 $device $mountpoint ;
					then pmount -t vfat -w -u 000 $device $mountpoint ;
				elif [ "$mount" == "o" ] ;	## 'o' for options, choose device and mountpoint manually
					then echo "Enter device name [$device]"
					read device2
					echo "Enter mount point [$mountpoint]"
					read mountpoint2
					if [ "$device2" != "" ] ; then device=$device2 ; else echo "" ; fi;
					if [ "$mountpoint2" != "" ] ; then mountpoint=$mountpoint2 ; else echo "" ; fi;
					echo "Mounting $device on $mountpoint"
					#sudo mount -t vfat -o rw,umask=0000 $device $mountpoint ;
					pmount -t vfat -w -u 000 $device $mountpoint ;
					else echo "Not mounting device" ;
				fi
			fi
		fi	## End mount section
		echo "Moving files from $sourcedir to $destdir"
			mv $sourcedir/* $destdir 2>/dev/null
		cd $destdir
		echo "Unmount the camera ($mountpoint) now? [yN]"	## Let me turn it off and charge the battery
		#echo "(requires sudo rights)"
		read unmount
			if [ "$unmount" == "y" ] ;
				#then sudo umount $mountpoint ; 
				then pumount $mountpoint
				else echo "$mountpoint still mounted" ;
			fi
		echo "View photos with $viewer now? [yN]"
		read view
			if [ "$view" == "y" ] ;
				then $viewer . 2>/dev/null ; 
			else echo "Not viewing now" ;
			fi
		echo "Photos are in $destdir"
		mode=2	## Change default mode to 2 - resize
		;;
	### Option 2 ### One input
		"2" )
		echo "2: Copy and resize photos for web upload"
		echo "----------------------------------------"
		echo "Enter the path to the photo directory: [`pwd`]"
		read photodir ## Default is current working dir - follows on from '1'
			if [ "$photodir" == "" ] ; then photodir=`pwd` ; else echo "" ; fi;
		cd $photodir
		echo "Creating subdirectory $webdir"
			mkdir ./$webdir
		echo "Copying all .jpg files to subdirectory $webdir"
			cp *.jpg ./$webdir
		cd $webdir
		echo "Resizing copied files to $upsize maximum"
			mogrify -resize "$upsize" *.jpg
		echo "Resized files are in $photodir/$webdir"
		mode=3	## Change default mode to 3 - FTP upload
		;;
	### Option 3 ### 2 inputs
	## Read the echoed WARNING before messing with this one
		"3" )
		echo "3: Automatically upload from the command line"
		echo "---------------------------------------------"
		echo "WARNING: This will delete the contents of /$ftpupdir on the remote server"
		echo "         and replace it with the contents of the specified directory"
		echo "Enter the path to the photo directory: [`pwd`]"
			read uploaddir	## Default is current working dir - follows on from '2'
				if [ "$uploaddir" == "" ] ; then uploaddir=`pwd` ; else echo "" ; fi;
		echo "Changing local working directory to $uploaddir"
			cd $uploaddir
		echo "The following files will be uploaded:"
			ls
		echo "Last chance to abort - just enter the wrong password"
		echo "Enter FTP password:"
			read -s password
		echo "Connecting to $ftpserver"
		ftp -in $ftpserver <<ftpcommands	## Start commands to send to `ftp`
			user $ftpuser $password 	## Log in
			cd /$ftpupdir	## Change to /$ftpupdir on remote
			mdelete *	## Delete everything from /$ftpupdir
			mput *		## Upload everything from the current directory to /$ftpupdir
			bye		## Log out 
## Next line MUST be 'ftpcommands' ONLY - No indentation, no comments.
ftpcommands
		echo "Done."
		mode=Q	## Default to Quit
		;;
	### Option Q(uit) ###
		"q" | "Q" )
		exit 0
		;;
	### Any other option ###
		* )
		echo "Invalid option."
		;;
	esac
done
exit 42	## If you get this, 1 != 1, and the outcome of the script is the least of your worries.
