summaryrefslogtreecommitdiff
path: root/localwebgallerydaemon
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2008-03-20 20:35:51 +0000
committerJonas Smedegaard <dr@jones.dk>2008-03-20 20:35:51 +0000
commit0d5de751e67f400dd1415992d08c84c08e477efa (patch)
tree8c3077ac5e3d136793e90d84be3b9ed87f70ff86 /localwebgallerydaemon
parent8afd9d4d9b76328fe60ca999987f4b87343a1da4 (diff)
Implement a bunch of options, and only stay awake with --watch enabled.
Diffstat (limited to 'localwebgallerydaemon')
-rwxr-xr-xlocalwebgallerydaemon132
1 files changed, 93 insertions, 39 deletions
diff --git a/localwebgallerydaemon b/localwebgallerydaemon
index f0bc0d1..a43da53 100755
--- a/localwebgallerydaemon
+++ b/localwebgallerydaemon
@@ -3,18 +3,11 @@
# /usr/local/bin/localwebgalleryupdate
# Copyright 2008 Jonas Smedegaard <dr@jones.dk>
#
-# $Id: localwebgallerydaemon,v 1.1 2008-03-18 23:22:03 jonas Exp $
+# $Id: localwebgallerydaemon,v 1.2 2008-03-20 20:35:51 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:
@@ -23,11 +16,10 @@
#
# TODO: Support more gallery engines
#
-# TODO: Support overriding defaults in rc-file
+# TODO: Support system-wide and per-user config overrides
#
set -e
-#set -bm
# Defaults options
SOURCEDIR="$HOME/public_media"
@@ -35,31 +27,31 @@ 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 <<EOF
-Usage: $PRG [ SOURCEDIR ] [ TARGETDIR ]
+Usage: $PRG [ SOURCEDIR [ TARGETDIR ] ]
+
+Options:
+ -g, --generator Gallery generator to use (default: $GENERATOR)
+ -w, --watch Watch source, updating each time something changes
+ -n, --now Update once
+ -v, --verbose Print activities to stderr while running
+ -h, --help This text
-Defaults:
- SOURCEDIR: $SOURCEDIR
- TARGETDIR: $TARGETDIR
+The following directories are used if not provided on commandline:
+ SOURCEDIR: $SOURCEDIR
+ TARGETDIR: $TARGETDIR
+
+If --watch is not supplied, --now is implied.
+
+Use --generator=list to get a list of locally supported generators.
Examples:
- $PRG ~/my/images /var/www/gallery &
- daemon -u me -n gallery-me -- $PRG ~/my/images /var/www/gallery
+ $PRG ~/my/images /var/www/gallery
+ daemon -u me -n gallery-me -- $PRG --watch
+
EOF
}
@@ -69,31 +61,91 @@ exit1() {
exit 1
}
-lazygal_opts="--quiet --check-all-dirs --clean-destination ${THEME:+--theme $THEME}"
+TEMP="`getopt -s sh -o g:wnvh -l generator:,watch,now,verbose,help -n "$PRG" -- "$@"`"
+if [ $? != 0 ] ; then exit1 " Internal getopt error."; fi
+eval set -- "$TEMP"
+
+watch=
+now=
+verbose=
+while true ; do
+ case "$1" in
+ -g|--generator) generator="$2"; shift 2;;
+ -w|--watch) watch="yes"; shift;;
+ -n|--now) now="yes"; shift;;
+ -v|--verbose) verbose="yes"; shift;;
+ -h|--help) showhelp; exit 0;;
+ --) shift; break;;
+ *) exit1 "Internal error resolving options.";;
+ esac
+done
+
+# Use defaults if not overridden
+generator="${generator:-$GENERATOR}"
+sourcedir="${1:-$SOURCEDIR}"
+targetdir="${2:-$TARGETDIR}"
+
+generators="lazygal"
+if [ "list" = "$generator" ]; then
+ echo "The following gallery generators are supported on this system:"
+ echo
+ for generator in $generators; do
+ if which $generator >/dev/null; then
+ echo " $generator"
+ fi
+ done
+ echo
+exit 0
+fi
+
+# Check if generator is supported and declare needed external programs
+case $generator in
+ lazygal) which "$generator" >/dev/null \
+ || exit1 "Unsupported generator \"$generator\" (try --generator=list).";;
+ *) exit1 "Unsupported generator \"$generator\".";;
+esac
+
+# Static defaults
+lazygal_opts="--quiet --check-all-dirs --clean-destination"
+lazygal_theme_opts="${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
+[ -z "$watch" ] || which fileschanged >/dev/null \
+ || exit1 "Helper tool \"fileschanged\" needed for --watch mode is unavailable."
+# TODO: Make it work to signal status to parent proces
+#trap 'echo >&2 "Starting gallery update as requested."' USR1
updategallery() {
onemoretime=yes
trap 'onemoretime=yes' USR1
while [ -n "$onemoretime" ]; do
+# [ -z "$verbose" ] || kill -USR1 $PPID
onemoretime=
- case "$GENERATOR" in
+ case "$generator" in
lazygal)
- lazygal $lazygal_opts -o "$targetdir" "$sourcedir"
+ lazygal $lazygal_opts $lazygal_theme_opts -o "$targetdir" "$sourcedir"
;;
esac
- sleep 10
+ [ "--once" = "$1" ] || sleep 10
done
}
-pid=
-fileschanged -r -s created,changed,deleted "$sourcedir" | while read file; do
+started=
+# Update immediately if requested or if watch mode isn't requested
+if [ -n "$now" ] || [ -z "$watch" ]; then
+ if [ -z "$watch" ]; then
+ [ -z "$verbose" ] || echo >&2 "Starting gallery update once."
+ updategallery --once
+ else
+ started=yes
+ [ -z "$verbose" ] || echo >&2 "Starting gallery update before invoking watch mode."
+ updategallery &
+ fi
+fi
+[ -z "$watch" ] || [ -z "$verbose" ] || echo >&2 "Invoking watch mode."
+[ -z "$watch" ] || fileschanged -r -s created,changed,deleted "$sourcedir" | while read file; do
# Ignore hidden files (used for temporary files by rsync)
case "$file" in
@@ -103,8 +155,10 @@ fileschanged -r -s created,changed,deleted "$sourcedir" | while read file; do
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
+ [ -z "$verbose" ] || echo >&2 "Update request triggered by \"$file\""
+ if [ -z "$started" ] || ! kill -USR1 $(jobs -p) 2>/dev/null; then
+ started=yes
+ [ -z "$verbose" ] || echo >&2 "Request gallery update."
updategallery &
fi
done