#!/bin/csh -f # makethumbs_remote # ------------------------------------------------------------------------- # C shell script to create a "thumbs" folder of thumbnails of all images # (JPG, GIF, PNG, etc.) in the current directory, but create the # thumbnails using the CPU of a remote server, and then copy the files # back here. Saves CPU time locally, and avoids the annoying popup of # the Java runtime as each image is scaled, which steals the keyboard # focus each time. # ------------------------------------------------------------------------- # Revision History: # $Log$ # ------------------------------------------------------------------------- # Collect command line options set option_quick_string = "" set option_interactive_string = "-i" while ($#argv > 0) if ("$1" == "-h" || "$1" == "--help") then echo "Usage: $0:t [options]" echo "Options:" echo " -h = Show this help text" echo " --help = Show this help text" echo " -q = Quick. Skip prompt when deleting obsolete files." exit 1 else if ("$1" == "-q") then shift set option_quick_string = "-q" set option_interactive_string = "" else if ("-" == "`echo $1:q | cut -c 1`") then echo "Error: Invalid option: $1:q" $0 --help exit 1 else # Not a recognized option. Assume it's the first argument break endif end # No arguments defined, so allow none if ($#argv > 0) then $0 --help exit 1 endif set server = "mbp3" # Check for running in the wrong folder ls *.[jJ][pP][gG] *.[gG][iI][fF] *.[pP][nN][gG] >& /dev/null if ($status) then echo "No JPG, GIF or PNG files found. Nothing to do. Aborting..." exit 1 endif ls index.htm >& /dev/null if ($status) then echo "No index.htm found. Not a pictures folder? Aborting..." exit 1 endif # Don't bother unless at least one thumbnail is needed set thumb_needed = "false" foreach image_file (*.[jJ][pP][gG] *.[gG][iI][fF] *.[pP][nN][gG]) set file_without_extension = $image_file:r:q set thumb_file = "thumbs/$file_without_extension.jpg" if (! -e "$thumb_file") then echo "Found at least one new image: $image_file" set thumb_needed = "true" break else if ($image_file:q == "`find '$image_file:q' -newer '$thumb_file:q'`") then echo "Found at least one updated image: $image_file" set thumb_needed = "true" break endif end # Check also for thumbnails that are obsolete because the main image has # been deleted. The loop above would miss those. Without this extra # loop, we could leave some obsolete images and thumbnails on the remote # server. If we add, change or rename at least one image, it's detected # by the above loop and everything gets synced, but if we only delete an # image, the old image and its old thumbnail would be left on the server. # Would be deleted at next backup to that server by the quick script, but # it's better to do it immediately in this script. # Also, the old thumbnail would not be deleted from the local thumbs # folder until later when a new image was added, changed, or renamed. if ("$thumb_needed" == "false" && -d thumbs) then cd thumbs foreach thumb_file (*.[jJ][pP][gG] *.[gG][iI][fF] *.[pP][nN][gG]) set thumb_file_without_extension = $thumb_file:r:q # Note: Couldn't get this to work via a simple -e test as in the # previous loop. In that loop, I knew I was looking for a # thumbnail ending in .jpg. Here, I don't know what type # of image the thumbnail was created from. Have to use a # wildcard or pattern, and I can't find a way to get that # expanded properly in a quoted filename that may contain # spaces. Tried things like: # set image_file = "../$thumb_file_without_extension.*" # set image_file = "../$thumb_file_without_extension".* # and: # if (! -e "$image_file") then # if (! -e $image_file:q) then # if (! -e $image_file) then # Had problems with all combinations I tried, especially when # the filename had spaces or the wildcard matched more than # one file in the parent folder. Finally decided to just use # a nested loop. set image_found = "false" foreach image_file (../*.[jJ][pP][gG] ../*.[gG][iI][fF] ../*.[pP][nN][gG]) set image_file_without_extension = $image_file:r:t:q if ("$image_file_without_extension" == \ "$thumb_file_without_extension") then set image_found = "true" break endif end if ("$image_found" == "false") then echo "Found a deleted image for at least one thumbnail: $thumb_file" set thumb_needed = "true" break endif end cd .. endif if ("$thumb_needed" == "false") exit # Modeled after the remotepub script. See notes there for why pwd -P is used. set local_dir = "`pwd -P | normalize_filename`" printheader "Syncing local directory tree $local_dir/... with $server..." # Note: Create remote copy of local directory, including all ancestors, in case # the parent doesn't yet exist. Otherwise, rsync aborts when trying to # create the directory and finding no such parent: # rsync: mkdir ".../parent/directory" failed: # No such file or directory (2) # Note: Use -P to force remote cd to go to current working directory, not # to the directory that's not yet created. (set echo; $server -P . "mkdir -v -p $local_dir") (set echo; rsyncupdate --del $local_dir/ ${server}:$local_dir) printheader "Running makethumbs $option_quick_string on $server for $local_dir..." # Note: Use &&, not ;, to skip makethumbs if the cd fails (set echo; $server "cd $local_dir && makethumbs $option_quick_string") printheader "Copying generated thumbs from $server for $local_dir..." # Note: Do NOT add --del here. See below for why not. (set echo; rsyncupdate ${server}:$local_dir/thumbs/ thumbs) # Check for obsolete thumbs files that have no full-sized counterpart. # Seems safer than using --del on the rsyncupdate command above. In case # of typo or bug, that could delete lots of stuff here w/o prompting. cd thumbs foreach thumb_file (*.[jJ][pP][gG] *.[gG][iI][fF] *.[pP][nN][gG]) # Note: Put all the -o clauses at the end. Can't end with -print. # That causes it to find nothing. Why? # Also, can't put -print earlier. That causes it to find all. # Just leave -print out. It's implied anyhow. set image_file = `find .. -maxdepth 1 \ -name "${thumb_file:r}.[jJ][pP][gG]" \ -o -name "${thumb_file:r}.[gG][iI][fF]" \ -o -name "${thumb_file:r}.[pP][nN][gG]" \ ` if ("$image_file" == "") then echo "Deleting obsolete thumbnail $thumb_file..." rm -v $option_interactive_string "$thumb_file" endif end