summaryrefslogtreecommitdiff
path: root/localwebgallerydaemon
blob: f0bc0d1f803c3ee67187a7c6a4126e68f37dc16c (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/bin/localwebgalleryupdate
  4. # Copyright 2008 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localwebgallerydaemon,v 1.1 2008-03-18 23:22:03 jonas Exp $
  7. #
  8. # (Re)generate web gallery whenever source directory changes
  9. #
  10. # TODO: Implement options:
  11. # --help
  12. # --sourcedir
  13. # --targetdir
  14. # --run-once
  15. # --now
  16. # --verbose
  17. # --debug
  18. # --init (create targetdir if missing, fail if exists and non-empty)
  19. #
  20. # TODO: implement ask() and the following options:
  21. # --reset (purge output dir -- use with --init to always build from scratch)
  22. # --force
  23. #
  24. # TODO: Support more gallery engines
  25. #
  26. # TODO: Support overriding defaults in rc-file
  27. #
  28. set -e
  29. #set -bm
  30. # Defaults options
  31. SOURCEDIR="$HOME/public_media"
  32. TARGETDIR="$HOME/public_html/gallery"
  33. GENERATOR="lazygal"
  34. THEME="image-index"
  35. # Use defaults if not overridden
  36. sourcedir="${1:-$SOURCEDIR}"
  37. targetdir="${2:-$TARGETDIR}"
  38. PRG=$(basename "$0")
  39. # Required external programs
  40. EXTPRG="/usr/bin/fileschanged"
  41. case $GENERATOR in
  42. lazygal)
  43. EXTPRG="$EXTPRG /usr/bin/$GENERATOR"
  44. ;;
  45. esac
  46. showhelp() {
  47. cat <<EOF
  48. Usage: $PRG [ SOURCEDIR ] [ TARGETDIR ]
  49. Defaults:
  50. SOURCEDIR: $SOURCEDIR
  51. TARGETDIR: $TARGETDIR
  52. Examples:
  53. $PRG ~/my/images /var/www/gallery &
  54. daemon -u me -n gallery-me -- $PRG ~/my/images /var/www/gallery
  55. EOF
  56. }
  57. exit1() {
  58. echo >&2 "Error: $1"
  59. echo >&2 "Exiting..."
  60. exit 1
  61. }
  62. lazygal_opts="--quiet --check-all-dirs --clean-destination ${THEME:+--theme $THEME}"
  63. # Sanity checks
  64. [ -d "$sourcedir" ] || exit1 "Source directory \"$sourcedir\" is not a directory"
  65. [ -d "$targetdir" ] || exit1 "Target directory \"$targetdir\" is not a directory"
  66. for prg in $EXTPRG; do
  67. [ -x "$prg" ] || exit1 "Required program \"$prg\" not installed"
  68. done
  69. updategallery() {
  70. onemoretime=yes
  71. trap 'onemoretime=yes' USR1
  72. while [ -n "$onemoretime" ]; do
  73. onemoretime=
  74. case "$GENERATOR" in
  75. lazygal)
  76. lazygal $lazygal_opts -o "$targetdir" "$sourcedir"
  77. ;;
  78. esac
  79. sleep 10
  80. done
  81. }
  82. pid=
  83. fileschanged -r -s created,changed,deleted "$sourcedir" | while read file; do
  84. # Ignore hidden files (used for temporary files by rsync)
  85. case "$file" in
  86. .*)
  87. continue
  88. ;;
  89. esac
  90. # Invoke update if not active or telling to take another spin fails
  91. if [ -z "$progress" ] || ! kill -USR1 $(jobs -p) 2>/dev/null; then
  92. progress=yes
  93. updategallery &
  94. fi
  95. done
  96. exit 0