#!/bin/sh # # /usr/local/bin/localwebgalleryupdate # Copyright 2008 Jonas Smedegaard # # $Id: localwebgallerydaemon,v 1.1 2008-03-18 23:22:03 jonas Exp $ # # (Re)generate web gallery whenever source directory changes # # TODO: Implement options: # --help # --sourcedir # --targetdir # --run-once # --now # --verbose # --debug # --init (create targetdir if missing, fail if exists and non-empty) # # TODO: implement ask() and the following options: # --reset (purge output dir -- use with --init to always build from scratch) # --force # # TODO: Support more gallery engines # # TODO: Support overriding defaults in rc-file # set -e #set -bm # Defaults options SOURCEDIR="$HOME/public_media" TARGETDIR="$HOME/public_html/gallery" GENERATOR="lazygal" THEME="image-index" # Use defaults if not overridden sourcedir="${1:-$SOURCEDIR}" targetdir="${2:-$TARGETDIR}" PRG=$(basename "$0") # Required external programs EXTPRG="/usr/bin/fileschanged" case $GENERATOR in lazygal) EXTPRG="$EXTPRG /usr/bin/$GENERATOR" ;; esac showhelp() { cat <&2 "Error: $1" echo >&2 "Exiting..." exit 1 } lazygal_opts="--quiet --check-all-dirs --clean-destination ${THEME:+--theme $THEME}" # Sanity checks [ -d "$sourcedir" ] || exit1 "Source directory \"$sourcedir\" is not a directory" [ -d "$targetdir" ] || exit1 "Target directory \"$targetdir\" is not a directory" for prg in $EXTPRG; do [ -x "$prg" ] || exit1 "Required program \"$prg\" not installed" done updategallery() { onemoretime=yes trap 'onemoretime=yes' USR1 while [ -n "$onemoretime" ]; do onemoretime= case "$GENERATOR" in lazygal) lazygal $lazygal_opts -o "$targetdir" "$sourcedir" ;; esac sleep 10 done } pid= fileschanged -r -s created,changed,deleted "$sourcedir" | while read file; do # Ignore hidden files (used for temporary files by rsync) case "$file" in .*) continue ;; esac # Invoke update if not active or telling to take another spin fails if [ -z "$progress" ] || ! kill -USR1 $(jobs -p) 2>/dev/null; then progress=yes updategallery & fi done exit 0