#!/bin/csh -f # make_and_pub_index_htm_from_jpgs # ------------------------------------------------------------------------- # Shell script to create and publish a Web page that shows thumbnails of # all images (JPG, GIF, PNG, etc.) in the current directory. It creates # a thumbs directory, populates it with thumbnails, creates an index.htm # file in the current directory that shows all thumbnails with each as a # link to the full-sized image in this directory, and publishes the files # to my Web server. # ??Also, creates a slideshow subdirectory with links to all of the image # ?and *.txt files for use by the Bristle Software Java slideshow software. # Optionally also creates a slideshow subdirectory with an HTML file for # each image file and an index.htm that uses the Bristle Software JS # slideshow software, and publishes them as well. # ------------------------------------------------------------------------- # Revision History: # $Log$ # ------------------------------------------------------------------------- # Collect command line options set option_slideshow = "false" set option_force = "false" set option_quick_string = "" set option_local = "false" set option_local_string = "" set option_makethumbs_remote = "false" 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 " -s = Make slideshow folder also" echo " -f = Force -- run even if subdirs exist" echo " -q = Quick -- Skip interactive review of index.htm and" echo " slideshow/index.htm. Just show and accept changes." echo " Also, skip prompts when deleting obsolete files." echo " -l = Local -- Don't publish updated files" echo " -r = Make the thumbnails remotely on server mbp4" echo " --remote = Make the thumbnails remotely on server mbp4" exit 1 else if ("$1" == "-s") then shift set option_slideshow = "true" else if ("$1" == "-f") then shift set option_force = "true" else if ("$1" == "-q") then set option_quick_string = "-q" setenv PUB_NO_CONFIRM_REMOVE true shift else if ("$1" == "-l") then shift set option_local = "true" set option_local_string = "-l" else if ("$1" == "-r" || "$1" == "--remote") then shift set option_makethumbs_remote = "true" 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 -h exit 1 endif # Collect options from files set option_reverse_string = "" if (-e .reverse_image_order_in_album_and_slideshow) then set option_reverse_string = "--reverse" endif 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 if ("$option_force" != "true") then set subdirs = "`find . -maxdepth 1 \( -type d -o -type l \) -not -name thumbs -not -name slideshow -not -name .`" if ("$subdirs" != "") then beep "Subdirs exist:" ls -1 -d $subdirs echo "Run $0:t in each of them instead of here, to avoid hiding them" echo "via index.htm at the Web site, or use the -f option." $0 -h exit 1 endif endif # Get and increment version_number, which is needed to generate unique # URLs to ensure that all HTML files for loaded slides are not cached # locally by the browser, and updates missed, when I push a new version # of the same slideshow. set minor_version_file = ".bristle_slideshow_version" if ("`printenv BRISTLE_SLIDESHOW_VERSION_FILE`" != "") then set minor_version_file = "$BRISTLE_SLIDESHOW_VERSION_FILE" endif set major_version_number = "0" if (-e $minor_version_file:q) then set minor_version_string = `cat $minor_version_file:q` @ minor_version_number = $minor_version_string + 1 else @ minor_version_number = 1 endif setenv VERSION_NUMBER "${major_version_number}.${minor_version_number}" if ("$option_local" == "false") then echo $minor_version_number >! $minor_version_file:q endif echo "=======================================================================" echo "Rotating image files" echo "=======================================================================" # Note: Detect Dropbox attributes before stripping them off in a later loop. foreach image_file (*.[jJ][pP][gG] *.[gG][iI][fF] *.[pP][nN][gG]) file "$image_file" | grep "orientation=upper-left" > /dev/null if (! $status) then # Image claims to be correctly rotated. Nothing to do. continue else # Image does not claim to be correctly rotated. May claim to be # incorrectly rotated, or may make no claim at all. Rotation info can # be in JPG files, and maybe other types of images (GIF, PNG, etc.), # but may be missing, even in JPG files. # Note: We could check the file for other explicit rotation claims # like upper-right, lower-left, and lower-right, but for now # it seems better to just check Dropbox attributes to decide # whether the user has already been offered a chance to rotate # the image. The above test for upper-left is really just to # avoid offering all images to the user the 1st time, since # most images are not rotated. xattr_detect com.dropbox.attrs "$image_file" if ($status) then # Dropbox attributes have already been stripped off. Nothing to do. # Assume that all files not received via Dropbox (from my camera) # are already properly rotated. That may not be true, but we can # fix them as necessary when we find any. Better to NOT force the # user to look at all such images each time we run, especially # since this script is run over and over, sometimes from an # automated process, as new images are added. It's better to # assume all the old images are OK, and only consider rotating # the new ones. continue else # Dropbox attributes are still present. This file came from Dropbox # and has not yet been considered for rotation. Open the Preview # app, allowing the user to rotate the image. See the notes in # rotateimages for how the user should do this. echo -n "Rotate: " rotateimages "$image_file" endif endif end echo "=======================================================================" echo "Renaming image/video files, removing Quarantine and Dropbox attributes" echo "=======================================================================" foreach file ( *.[jJ][pP][gG] *.[gG][iI][fF] *.[pP][nN][gG] *.[mM][pP]4 ) # Force file extension to lowercase for consistency across all slideshows # Note: Could factor this block of code out into a new script called # lowercase_extension, but we want to not only change the case of # the file extension, but also learn the new lowercased filename # for use in the rest of this script. Maybe later... set file_without_extension = $file:r:q set file_extension = $file:e:q set lowercase_extension = `echo "$file_extension:q" | lowercase` set file_with_lowercase_extension \ = "$file_without_extension.$lowercase_extension" if ("$file" != "$file_with_lowercase_extension") then echo -n "Lowercase: " mv -v "$file" "$file_with_lowercase_extension" set file = "$file_with_lowercase_extension" endif unset file_without_extension unset file_extension unset lowercase_extension unset file_with_lowercase_extension # Strip off undesired attributes that files may have picked up # by being downloaded from the Web or obtained from Dropbox. unquarantine -p "Unquarantine: " "$file" undropbox -p "Un-dropbox: " "$file" # Convert all spaces in the filename to underscore because spaces in # filenames and in URLs cause so many issues in so many places # Note: Do this step last in the loop because we're already looping # over the names with spaces, so other steps inside this loop # won't find the file if we rename it before they're done. rename -q -p "Underscores: " " " "_" "$file" end # Now that we're done using shell wildcards, prevent wildcard and command # line pattern expansion because we're using a regular expression in the # --ignore-matching-lines value. We want to avoid "No match." errors when # the shell tries to expand it into filenames. Also, we really do want the # regular expression to get passed to diff. set noglob # Note: Don't use a space char instead of [[:space:]]. Too hard to quote # things sufficiently to prevent splitting the pattern into 2 command # line args as we pass it through the layers of scripts. # Note: Don't use \s instead of [[:space:]]. \s gets stripped to just s as # the pattern gets passed from diffreview to diff. Also, even when # calling diff directly, I can't get \s or \\s or \\\s to work. Have # to use the long form. # Note: Don't use single quotes around version pattern. 'pattern' fails to # match pattern. set ignore_version_string = "--ignore-matching-lines [Vv]ersion[[:space:]]*=" set diffreviewmv_cmd = "diffreviewmv $option_quick_string $ignore_version_string" set makeslides_cmd = "makeslides $option_quick_string $ignore_version_string" set makepubreview_cmd = "makepubreview $option_quick_string" set makethumbs_cmd = "makethumbs $option_quick_string" set makethumbs_remote_cmd = "makethumbs_remote $option_quick_string" echo "=======================================================================" echo "Creating index.htm" echo "=======================================================================" if (-e index.htm) then # Note: mv, not cp, to avoid updating timestamp. Then if the new # version is identical, we can mv it back via diffreviewmv # and avoid publishing the file to the server needlessly. # Note: Use -i regardless of option_quick_string. Happens rarely. # Only when previous run was aborted with Ctrl-C. Seems to be # worth confirming with the user. mv -i index.htm index.htm.old endif (unset noclobber; make_index_htm_from_jpgs $option_reverse_string > index.htm) if (-e index.htm.old) then # Create empty index.htm if it wasn't created above. Otherwise, # diffreviewmv reports an error, doesn't show diffs, and leaves # index.htm.old in place. if (! -e index.htm) touch index.htm # Give the user a chance to review and reconcile the differences # Note: diffreviewmv, not diffreviewdel, to avoid updating timestamp, # to avoid publishing the file to the server needlessly. $diffreviewmv_cmd index.htm index.htm.old endif echo "=======================================================================" echo "Creating thumbs" echo "=======================================================================" if ("$option_makethumbs_remote" == "true") then $makethumbs_remote_cmd else $makethumbs_cmd endif if ("${option_slideshow}" == "true") then echo "=======================================================================" echo "Creating slides" echo "=======================================================================" #?? Old code. Used to create links to be used by server-side Java #?? slideshow software. Now create slide files to be loaded via JS. #?? make_slideshow_links $makeslides_cmd $option_local_string echo "=======================================================================" echo "Creating slideshow/index.htm" echo "=======================================================================" if (-e slideshow/index.htm) then # Note: mv, not cp, to avoid updating timestamp. Then if the new # version is identical, we can mv it back via diffreviewmv # and avoid publishing the file to the server needlessly. # Note: Use -i regardless of option_quick_string. Happens rarely. # Only when previous run was aborted with Ctrl-C. Seems to be # worth confirming with the user. mv -i slideshow/index.htm slideshow/index.htm.old endif (unset noclobber; make_slideshow_htm_from_slides $option_reverse_string > slideshow/index.htm) if (-e slideshow/index.htm.old) then # Create empty slideshow/index.htm if it wasn't created above. # Otherwise, diffreviewmv reports an error, doesn't show diffs, # and leaves slideshow/index.htm.old in place. if (! -e slideshow/index.htm) touch slideshow/index.htm # Give the user a chance to review and reconcile the differences # Note: diffreviewmv, not diffreviewdel, to avoid updating timestamp, # to avoid publishing the file to the server needlessly. $diffreviewmv_cmd slideshow/index.htm slideshow/index.htm.old endif endif echo "=======================================================================" echo "Creating pub scripts" echo "=======================================================================" (unset noclobber; $makepubreview_cmd) (unset noclobber; cd thumbs; $makepubreview_cmd) if ("${option_slideshow}" == "true") then (unset noclobber; cd slideshow; $makepubreview_cmd) endif if ("$option_local" == "true") then echo "=======================================================================" echo "Local mode. Not running pub scripts" echo "=======================================================================" else echo "=======================================================================" echo "Running pub scripts" echo "=======================================================================" (setenv PUB_EXTRA_FILES "album"; ./pub) (cd thumbs; ./pub) if ("${option_slideshow}" == "true") then #?? How to tolerate spaces in filenames in the value of No. single quotes # Note: Use colon (:), not space, as filename separator, like in PATH, # to be able to tolerate spaces in filenames. Couldn't find a # way to make it work with space as separator, regardless of what # type/combination of quotes, I used. (cd slideshow; setenv PUB_EXTRA_FILES "slideshow:album"; ./pub) endif endif