#!/bin/sh ################################################################################ # Search Pictures with $keyword in $directory ################################################################################ searchpics(){ echo "Searching for pictures marked with \"$keyword\" in:" echo "$directory" echo "This might take a while depending on the amount of files in directory..." # TODO: Would be nice to put a progress meter or visual of some sort if [ $change_dir -eq 1 ]; then grep --include=*.{JPG,jpg,JPEG,jpeg} -ril "$keyword" "$directory" |\ sed "s,$upload_from_dir,,g" > ~/.picup_load cat ~/.picup_load # Change to the directory specified by the '-c' option cd $upload_from_dir else cat ~/.picup_load grep --include=*.{JPG,jpg,JPEG,jpeg} -ril "$keyword" "$directory" > ~/.picup_load fi } ################################################################################ # Prompt the user before continuing with settings unless $silent=1 ################################################################################ prompt(){ if [ $sync -eq 1 ]; then method="synchronized upload..." else method="direct upload..." fi echo echo "Do you wish to continue? \c" if [ $silent -eq 0 ]; then read answer if [ $answer == "no" ]; then echo "Aborted" exit 2 else echo "Continuing with $method" fi else echo "yes [silent=$silent]" fi } ################################################################################ # Create a default configuration file. ################################################################################ create_conf(){ cat > ~/.picup_config << EOF #!/bin/sh ################################################################################ # INTRODUCTION ################################################################################ # This file contains some EXAMPLE variable settings for using the picup script. # You WILL NEED to modify some of them to fit your unique needs. # # There are two main methods for uploading photos to a server: # 1.) (\$sync=0) You can choose to directly upload the photos to a particular # location on a server. # 2.) (\$sync=1) You can choose to synchronize all the photos (even the # untagged photos) to a general location on the server and then have the # cream of the crop (photos marked with \$keyword) displayed in another # location on the server such as a gallery. # Note: These photos will not take up any additional space on the # server since they will be linked to the general location like so: # ln -s /public_html/general/image.jpg /public_html/gallery/image.jpg # # The pipup script works quite well with photo gallery websites like ZenPhoto # where a database is not necessary and photos can be directly uploaded to an # album folder such as /public_html/zenphoto/albums/. # # The idea behind this configuration file is that you shouldn't need to change # your variables very often and remembering which command line arguments should # be included can be intimitating. Instead, we want to 'SET it and FORGET it'. # # Don't be afraid to tweak the settings around. You will be prompted and # presented with a verification step which summarizes the steps it will take. # If you don't like what you see, you can simply answer "no" to proceeding. # Likewise, if you like what you see, you can anser "yes" to proceeding. If you # don't want to ever be prompted, simply set the varilble \$silent=1. # # One last note is that this script will use SSH to upload pictures to a remote # server. You may want to setup keyless SSH access so you don't need to enter # your password everytime ssh, scp, and/or rsync is called. # http://linuxproblem.org/art_9.html # # Improvements/Questions: # * When a directory is removed or renamed on the host, the server is not # updated to reflect it and the old folder name remains. # * Should I be able to search for more than one matching keyword? ################################################################################ # BASIC VARIABLES ################################################################################ # This is where the keyword or key tag to look for in a photo is specified. # These are the photos that will be uploaded (\$sync=0) or linked to (\$sync=1). # The keyword here is case insensitive. keyword=favorites # This is the ABSOLUTE PATH of the directory that will be searched # for photos marked with \$keyword. As an example, the \`date +"%Y/%m # - %B/"\` portion of the directory line will select the current # year and month of photos to search in so time is saved when # grepping photos. If the year is 2010, it will look in # /home/user/pictures/2010/. This is useful if you have a chronological # directory structure, ie. # + 2009 # \\ # | 01 - January # \\ # | Playing in the Snow # \\ # | IMG_0001.JPG # | IMG_0002.JPG # | 02 - Feburary # | 03 - March # + 2010 # \\ # | 01 - January # | 02 - Feburary # | 03 - March # directory="/Volumes/MyFiles/Pictures/`date +"%Y/%m - %B/"`" ################################################################################ # MAIN OPTIONS ################################################################################ # This is where you set whether you want a direct upload or a synchronized one # as explained above. sync=1 # This is where you set whether or not you want to be prompted to continue. # Until you've ran the script at least once to verify correct settings, the # recommended setting is silent=0. silent=0 # You can choose to change to a particular directory before uploading to the # server so that you can have more control over how the directory structure # appears on your server. If you are going to change directories, you must # specify \$change_dir=1 and change \$upload_from_dir accordingly, otherwise, # leave it as change_dir=0. A more concrete example is this (assuming # \$server_dir=/home/user/public_html/gallery/albums and # \$directory=/home/user/pictures/2010 and the photo # of /01-January/Snow_Pics/0001.jpg has been marked with \$keyword): # 1.) (change_dir=0) If directory is not changed, the uploaded files would # appear as: # /home/user/public_html/gallery/albums/home/user/pictures/2010/01-January/Snow_Pics/0001.jpg # 2.) (change_dir=1) If directory is changed to /home/user/pictures/, the # uploaded files would appear as: # /home/user/public_html/gallery/albums/2010/01-January/Snow_Pics/0001.jpg change_dir=0 upload_from_dir="/home/`whoami`/pictures/" ################################################################################ # SERVER VARIABLES ################################################################################ # This is where you setup your server information such as your ssh login, ssh # options, and destination address of your photos on the server. There are two # destination address options which are taken care of automatically depending # on the \$sync option used. If \$sync=1, then ALL THREE variables \$server_dir, # \$server_dir_sync, and \$rsync_opts will be used. If \$sync=0, then ONLY # \$server_dir will be used. # # Keep in mind the subtleties of rsync and how a directory with a trailing slash # is different than one without. See \`man rsync\` for more info. server_login="user@domainname.com" server_dir="/home/user/public_html/gallery/albums" server_dir_sync="/home/user/public_html/pictures" # This is the options for rsync. The values chosen below are pretty typical, # but you may still want to refer to \`rsync man\` to make sure you understand # what is happening (ie. --delete-after option will remove files on the server # that have been removed on the host). rsync_opts="-avzh -e ssh --numeric-ids -i --delete-after --exclude-from=${upload_from_dir}excludes" EOF } ################################################################################ # Main Script ################################################################################ # INITIALIZE VARIABLES if [ -e ~/.picup_config ]; then # source .picup_config variables . ~/.picup_config else echo "It looks like this is the first time you're running $0." echo "An example default .picup_config file will be created at:" echo ~/.picup_config create_conf echo "Please edit the config file to meet your needs and run $0 again." exit 2 fi # FIXME: Check to make sure the directory specified actually exists if [ ! -e "$directory" ]; then echo "Error: The directory \"$directory\" does not exist!" exit 1 fi # Check to see if rsync is installed if sync enabled if [ $sync -eq 1 ]; then if [ ! `which rsync` ] then echo "Error: Missing dependancy rsync" exit 1 fi fi # Start searching for pictures, store results to ~/.picup_load searchpics if [ -s ~/.picup_load ]; then sample_line=`head -1 ~/.picup_load` echo echo "Please VERIFY the steps that are ABOUT TO BE EXECUTED!" if [ $sync -eq 1 ]; then echo "This script is about to synchronize the directory" echo " $upload_from_dir" echo "with the remote site" echo " ${server_login}:$server_dir_sync" echo "with the options of" echo " $rsync_opts" echo "which will appear on the server as" echo " $server_dir_sync/$sample_line" echo "When finished, the following command will ran on the server" echo " ln -s \"$server_dir_sync/$sample_line\" \"$server_dir/$sample_line\"" prompt rsync $rsync_opts $upload_from_dir $server_login:$server_dir_sync if [ $? -eq 255 ] then echo "Error: rsync exited with exit status 255. Check your internet connection!" exit 1 fi else echo "This script is about to directly upload photos from the directory" echo " $directory" echo "to the remote site" echo " $server_login:$server_dir_sync" echo "which will appear on the server as" echo " $server_dir_sync/$sample_line" prompt fi while read line do # TODO: Optimization would be to only create and link files that are new # TODO: Figure out a way to remove directories that may have been removed on local machine # which one method would be to just erase all directories and symlinks in $server_dir if [ $sync -eq 1 ]; then echo "mkdir -p \"\`dirname \"$server_dir/$line\"\`\"" >> server_exec echo "ln -s \"$server_dir_sync/$line\" \"$server_dir/$line\"" >> server_exec else scp -B "$line" "$server_login:$server_dir" fi done < ~/.picup_load if [ $sync -eq 1 ]; then ( chmod +x server_exec echo "Uploading and executing script on server..." scp server_exec ${server_login}:$server_dir ssh $server_login exec $server_dir/server_exec ssh $server_login exec rm -f $server_dir/server_exec rm server_exec ) 2> /dev/null echo "Finished uploading your pictures. Enjoy!" fi rm ~/.picup_load else echo "Error: No suitable JPG,jpg,JPEG,jpeg files found with $keyword. Refine your search and try again." exit 1 fi