summaryrefslogtreecommitdiff
path: root/localwebgallerydaemon
blob: b1d1c2848fbfae30a63c09668739294cbf36d5e7 (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/bin/localwebgalleryupdate
  4. # Copyright 2008 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localwebgallerydaemon,v 1.2 2008-03-20 20:35:51 jonas Exp $
  7. #
  8. # (Re)generate web gallery whenever source directory changes
  9. #
  10. # TODO: Implement options:
  11. # --init (create targetdir if missing, fail if exists and non-empty)
  12. #
  13. # TODO: implement ask() and the following options:
  14. # --reset (purge output dir -- use with --init to always build from scratch)
  15. # --force
  16. #
  17. # TODO: Support more gallery engines
  18. #
  19. # TODO: Support system-wide and per-user config overrides
  20. #
  21. set -e
  22. # Defaults options
  23. SOURCEDIR="$HOME/public_media"
  24. TARGETDIR="$HOME/public_html/gallery"
  25. GENERATOR="lazygal"
  26. THEME="image-index"
  27. PRG=$(basename "$0")
  28. showhelp() {
  29. cat <<EOF
  30. Usage: $PRG [ SOURCEDIR [ TARGETDIR ] ]
  31. Options:
  32. -g, --generator Gallery generator to use (default: $GENERATOR)
  33. -w, --watch Watch source, updating each time something changes
  34. -n, --now Update once
  35. -v, --verbose Print activities to stderr while running
  36. -h, --help This text
  37. The following directories are used if not provided on commandline:
  38. SOURCEDIR: $SOURCEDIR
  39. TARGETDIR: $TARGETDIR
  40. If --watch is not supplied, --now is implied.
  41. Use --generator=list to get a list of locally supported generators.
  42. Examples:
  43. $PRG ~/my/images /var/www/gallery
  44. daemon -u me -n gallery-me -- $PRG --watch
  45. EOF
  46. }
  47. exit1() {
  48. echo >&2 "Error: $1"
  49. echo >&2 "Exiting..."
  50. exit 1
  51. }
  52. TEMP="`getopt -s sh -o g:wnvh -l generator:,watch,now,verbose,help -n "$PRG" -- "$@"`"
  53. if [ $? != 0 ] ; then exit1 " Internal getopt error."; fi
  54. eval set -- "$TEMP"
  55. watch=
  56. now=
  57. verbose=
  58. while true ; do
  59. case "$1" in
  60. -g|--generator) generator="$2"; shift 2;;
  61. -w|--watch) watch="yes"; shift;;
  62. -n|--now) now="yes"; shift;;
  63. -v|--verbose) verbose="yes"; shift;;
  64. -h|--help) showhelp; exit 0;;
  65. --) shift; break;;
  66. *) exit1 "Internal error resolving options.";;
  67. esac
  68. done
  69. # Use defaults if not overridden
  70. generator="${generator:-$GENERATOR}"
  71. sourcedir="${1:-$SOURCEDIR}"
  72. targetdir="${2:-$TARGETDIR}"
  73. generators="lazygal"
  74. if [ "list" = "$generator" ]; then
  75. echo "The following gallery generators are supported on this system:"
  76. echo
  77. for generator in $generators; do
  78. if which $generator >/dev/null; then
  79. echo " $generator"
  80. fi
  81. done
  82. echo
  83. exit 0
  84. fi
  85. # Check if generator is supported and declare needed external programs
  86. case $generator in
  87. lazygal)
  88. which "$generator" >/dev/null \
  89. || exit1 "Unsupported generator \"$generator\" (try --generator=list)."
  90. which "mispipe" >/dev/null \
  91. || exit1 "Helper tool \"mispipe\" (part of \"moreutils\") is unavailable."
  92. ;;
  93. *) exit1 "Unsupported generator \"$generator\".";;
  94. esac
  95. # Static defaults
  96. lazygal_opts="--quiet --check-all-dirs --clean-destination"
  97. lazygal_theme_opts="${THEME:+--theme $THEME}"
  98. # Sanity checks
  99. [ -d "$sourcedir" ] || exit1 "Source directory \"$sourcedir\" is not a directory"
  100. [ -d "$targetdir" ] || exit1 "Target directory \"$targetdir\" is not a directory"
  101. [ -z "$watch" ] || which fileschanged >/dev/null \
  102. || exit1 "Helper tool \"fileschanged\" needed for --watch mode is unavailable."
  103. # TODO: Make it work to signal status to parent proces
  104. #trap 'echo >&2 "Starting gallery update as requested."' USR1
  105. updategallery() {
  106. onemoretime=yes
  107. trap 'onemoretime=yes' USR1
  108. while [ -n "$onemoretime" ]; do
  109. # [ -z "$verbose" ] || kill -USR1 $PPID
  110. onemoretime=
  111. case "$generator" in
  112. lazygal)
  113. mispipe \
  114. "lazygal $lazygal_opts $lazygal_theme_opts -o \"$targetdir\" \"$sourcedir\" 2>&1" \
  115. "egrep -v '^(Error: Directory Makernote with 5376 entries considered invalid; not read.|Warning: Failed to read Makernote, rc = 6)$'" \
  116. || onemoretime=yes
  117. ;;
  118. esac
  119. [ "--once" = "$1" ] || sleep 10
  120. done
  121. }
  122. started=
  123. # Update immediately if requested or if watch mode isn't requested
  124. if [ -n "$now" ] || [ -z "$watch" ]; then
  125. if [ -z "$watch" ]; then
  126. [ -z "$verbose" ] || echo >&2 "Starting gallery update once."
  127. updategallery --once
  128. else
  129. started=yes
  130. [ -z "$verbose" ] || echo >&2 "Starting gallery update before invoking watch mode."
  131. updategallery &
  132. fi
  133. fi
  134. [ -z "$watch" ] || [ -z "$verbose" ] || echo >&2 "Invoking watch mode."
  135. [ -z "$watch" ] || fileschanged -r -s created,changed,deleted "$sourcedir" | while read file; do
  136. # Ignore hidden files (used for temporary files by rsync)
  137. case "$file" in
  138. .*)
  139. continue
  140. ;;
  141. esac
  142. # Invoke update if not active or telling to take another spin fails
  143. [ -z "$verbose" ] || echo >&2 "Update request triggered by \"$file\""
  144. if [ -z "$started" ] || ! kill -USR1 $(jobs -p) 2>/dev/null; then
  145. started=yes
  146. [ -z "$verbose" ] || echo >&2 "Request gallery update."
  147. updategallery &
  148. fi
  149. done
  150. exit 0