summaryrefslogtreecommitdiff
path: root/localwebgallerydaemon
blob: 17d61f26785bf89d2bb82c06e7ad3f343f0ec3b4 (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=""
  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. -t, --theme Gallery theme to use (default: ${THEME:-no theme})
  34. -w, --watch Watch source, updating each time something changes
  35. -n, --now Update once
  36. -v, --verbose Print activities to stderr while running
  37. -h, --help This text
  38. The following directories are used if not provided on commandline:
  39. SOURCEDIR: $SOURCEDIR
  40. TARGETDIR: $TARGETDIR
  41. If --watch is not supplied, --now is implied.
  42. Use --generator=list to get a list of locally supported generators.
  43. Examples:
  44. $PRG ~/my/images /var/www/gallery
  45. daemon -u me -n gallery-me -- $PRG --watch
  46. EOF
  47. }
  48. exit1() {
  49. echo >&2 "Error: $1"
  50. echo >&2 "Exiting..."
  51. exit 1
  52. }
  53. TEMP="`getopt -s sh -o g:t:wnvh -l generator:,theme:,,watch,now,verbose,help -n "$PRG" -- "$@"`"
  54. if [ $? != 0 ] ; then exit1 " Internal getopt error."; fi
  55. eval set -- "$TEMP"
  56. watch=
  57. now=
  58. verbose=
  59. while true ; do
  60. case "$1" in
  61. -g|--generator) generator="$2"; shift 2;;
  62. -t|--theme) theme="$2"; shift 2;;
  63. -w|--watch) watch="yes"; shift;;
  64. -n|--now) now="yes"; shift;;
  65. -v|--verbose) verbose="yes"; shift;;
  66. -h|--help) showhelp; exit 0;;
  67. --) shift; break;;
  68. *) exit1 "Internal error resolving options.";;
  69. esac
  70. done
  71. # Use defaults if not overridden
  72. generator="${generator:-$GENERATOR}"
  73. theme="${theme:-$THEME}"
  74. sourcedir="${1:-$SOURCEDIR}"
  75. targetdir="${2:-$TARGETDIR}"
  76. generators="lazygal"
  77. if [ "list" = "$generator" ]; then
  78. echo "The following gallery generators are supported on this system:"
  79. echo
  80. for generator in $generators; do
  81. if which $generator >/dev/null; then
  82. echo " $generator"
  83. fi
  84. done
  85. echo
  86. exit 0
  87. fi
  88. # Check if generator is supported and declare needed external programs
  89. case $generator in
  90. lazygal)
  91. which "$generator" >/dev/null \
  92. || exit1 "Unsupported generator \"$generator\" (try --generator=list)."
  93. which "mispipe" >/dev/null \
  94. || exit1 "Helper tool \"mispipe\" (part of \"moreutils\") is unavailable."
  95. ;;
  96. *) exit1 "Unsupported generator \"$generator\".";;
  97. esac
  98. # Static defaults
  99. lazygal_opts="--quiet --check-all-dirs --clean-destination ${THEME:+--theme $THEME}"
  100. # Sanity checks
  101. [ -d "$sourcedir" ] || exit1 "Source directory \"$sourcedir\" is not a directory"
  102. [ -d "$targetdir" ] || exit1 "Target directory \"$targetdir\" is not a directory"
  103. [ -z "$watch" ] || which fileschanged >/dev/null \
  104. || exit1 "Helper tool \"fileschanged\" needed for --watch mode is unavailable."
  105. # TODO: Make it work to signal status to parent proces
  106. #trap 'echo >&2 "Starting gallery update as requested."' USR1
  107. updategallery() {
  108. onemoretime=yes
  109. trap 'onemoretime=yes' USR1
  110. while [ -n "$onemoretime" ]; do
  111. # [ -z "$verbose" ] || kill -USR1 $PPID
  112. onemoretime=
  113. case "$generator" in
  114. lazygal)
  115. mispipe \
  116. "lazygal $lazygal_opts -o \"$targetdir\" \"$sourcedir\" 2>&1" \
  117. "egrep -v '^(Error: Directory Makernote with 5376 entries considered invalid; not read.|Warning: Failed to read Makernote, rc = 6)$'" \
  118. || onemoretime=yes
  119. ;;
  120. esac
  121. [ "--once" = "$1" ] || sleep 10
  122. done
  123. }
  124. started=
  125. # Update immediately if requested or if watch mode isn't requested
  126. if [ -n "$now" ] || [ -z "$watch" ]; then
  127. if [ -z "$watch" ]; then
  128. [ -z "$verbose" ] || echo >&2 "Starting gallery update once."
  129. updategallery --once
  130. else
  131. started=yes
  132. [ -z "$verbose" ] || echo >&2 "Starting gallery update before invoking watch mode."
  133. updategallery &
  134. fi
  135. fi
  136. [ -z "$watch" ] || [ -z "$verbose" ] || echo >&2 "Invoking watch mode."
  137. [ -z "$watch" ] || fileschanged -r -s created,changed,deleted "$sourcedir" | while read file; do
  138. # Ignore hidden files (used for temporary files by rsync)
  139. case "$file" in
  140. .*)
  141. continue
  142. ;;
  143. esac
  144. # Invoke update if not active or telling to take another spin fails
  145. [ -z "$verbose" ] || echo >&2 "Update request triggered by \"$file\""
  146. if [ -z "$started" ] || ! kill -USR1 $(jobs -p) 2>/dev/null; then
  147. started=yes
  148. [ -z "$verbose" ] || echo >&2 "Request gallery update."
  149. updategallery &
  150. fi
  151. done
  152. exit 0