summaryrefslogtreecommitdiff
path: root/localwebsearch
blob: affb195a161f504305e10b62fc9a8318f0069391 (plain)
  1. #!/bin/bash
  2. #
  3. # /usr/local/sbin/localwebsearch
  4. # Copyright 2001-2002 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localwebsearch,v 1.5 2004-01-20 22:39:19 jonas Exp $
  7. #
  8. # Web search engine maintenance script
  9. #
  10. # halt on errors (NB! this is a bashism...)
  11. set -e
  12. function usage() {
  13. echo "Usage: $(basename $0) initprep|init|update|ignore <website> [<website>...]"
  14. echo " If no website is given, all are attempted"
  15. echo " Tip: Automagically runs when symlinked to /etc/cron.{daily,weekly,monthly}/"
  16. exit 1
  17. }
  18. function exit1() {
  19. echo "Error: $1"
  20. echo "Exiting..."
  21. exit 1
  22. }
  23. # automagically configure when run from cron dirs
  24. case $(dirname $0) in
  25. /etc/cron.daily)
  26. stamp=update
  27. ;;
  28. /etc/cron.weekly)
  29. stamp=initprep
  30. ;;
  31. /etc/cron.monthly)
  32. stamp=ignore
  33. ;;
  34. *)
  35. stamp=$1
  36. shift || usage
  37. ;;
  38. esac
  39. case "$stamp" in
  40. initprep|init|update|ignore)
  41. ;;
  42. *)
  43. usage
  44. ;;
  45. esac
  46. if [ "$stamp" = "ignore" ]; then
  47. [ $DEBUG ] && echo "Asked to ignore - exiting silently..."
  48. exit 0
  49. fi
  50. WEBROOT=/var/www
  51. LOGROOT=/var/log/apache
  52. # Options: $1=LOGROOT, $2=WEBROOT, $3=WEBSITE
  53. function searchdir() { echo $2/VIRTUAL/search.$(dnsdomainname)/www/$3; }
  54. function webdirs() { find /etc/htdig -type f -name '*.conf' ! -name 'htdig.conf' -exec basename '{}' .conf \; ; }
  55. #function host() { cat $2/VIRTUAL/$3/hostname || exit1 "Unable to get hostname for virtual host."; }
  56. #function domain() { cat $2/VIRTUAL/$3/domainname || exit1 "Unable to get domainname for virtual host."; }
  57. function htdig_cfg() { echo /etc/htdig/$3.conf; }
  58. function pre_init() { true; }
  59. function post_init() { true; }
  60. function pre_update() { true; }
  61. function post_update() { true; }
  62. # The above can be overridden
  63. LOCALCONFIG=/etc/local/websearch.conf
  64. . $LOCALCONFIG || exit1 "Unable to read local config file $LOCALCONFIG"
  65. # variables and functions too boring to be configurable
  66. HTDIG_BIN="/usr/bin/rundig"
  67. HTDIG_REAL_BIN="/usr/local/sbin/localrundig" #TODO: Convince Debian maintainer to change official rundig
  68. #function awstats_setlog() { sed -e "s!^\(LogFile=\).*\$!\\1$2!" $1 > $1.tmp; mv $1.tmp $1; }
  69. # Webiste/independent checks
  70. test -d $WEBROOT || exit1 "Webroot \"$WEBROOT\" doesn't exist"
  71. test -d $LOGROOT || exit1 "Logroot \"$LOGROOT\" doesn't exist"
  72. # Index searches for websites from stdin or all default sites
  73. WEBSITES=$@
  74. if [ "$WEBSITES" = "" ]; then
  75. WEBSITES=$(webdirs $LOGROOT $WEBROOT $WEBSITE)
  76. fi
  77. for WEBSITE in $WEBSITES; do
  78. SEARCHDIR=$(searchdir $LOGROOT $WEBROOT $WEBSITE) || exit1 "Unable to resolve SEARCHDIR."
  79. # FIXME test -d $SEARCHDIR/.. || exit1 "Directory above SEARCHDIR doesn't exist."
  80. HTDIG_CFG=$(htdig_cfg $LOGROOT $WEBROOT $WEBSITE) || exit1 "Unable to resolve HTDIG_CFG."
  81. if [ $stamp = "init" ]; then
  82. [ $DEBUG ] && echo "Execute $WEBSITE PRE_INIT"
  83. pre_init $LOGROOT $WEBROOT $WEBSITE || exit1 "Error executing PRE_INIT."
  84. fi
  85. if [ $stamp = "update" ]; then
  86. [ $DEBUG ] && echo "Execute $WEBSITE PRE_UPDATE"
  87. pre_update $LOGROOT $WEBROOT $WEBSITE || exit1 "Error executing PRE_UPDATE."
  88. fi
  89. # HOST=$(host $LOGROOT $WEBROOT $WEBSITE) || exit1 "Unable to get hostname for virtual host."
  90. # DOMAIN=$(domain $LOGROOT $WEBROOT $WEBSITE) || exit1 "Unable to get domainname for virtual host."
  91. # HTDIG_OPTIONS="-a" #TODO: This seems to always init currently
  92. HTDIG_OPTIONS=""
  93. [ "$WEBSITE" != "htdig" ] && HTDIG_OPTIONS="$HTDIG_OPTIONS -c $HTDIG_CFG"
  94. if [ $DEBUG ]; then
  95. echo "Indexing search for $WEBSITE in $SEARCHDIR:"
  96. HTDIG_OPTIONS="$HTDIG_OPTIONS -v -s"
  97. else
  98. HTDIG_OPTIONS="$HTDIG_OPTIONS"
  99. fi
  100. if [ "$stamp" = "init" ]; then
  101. HTDIG_OPTIONS="$HTDIG_OPTIONS -i"
  102. fi
  103. if [ "$stamp" = "initprep" ]; then
  104. [ $DEBUG ] && echo "Mark next run as a full-scale, and move on to next site..."
  105. touch /etc/htdig/full_refresh_$WEBSITE
  106. continue
  107. fi
  108. # TODO
  109. if [ $stamp = "initXXX" ]; then
  110. [ $DEBUG ] && echo "$WEBSITE: Purge SEARCHDIR"
  111. rm -rf $SEARCHDIR
  112. mkdir $SEARCHDIR
  113. if [ -x $HTDIG_BIN ]; then
  114. [ $DEBUG ] && echo "$WEBSITE: Create/update htdig config"
  115. echo "\
  116. # NB! This file is automatically generated. Do not edit directly!
  117. # Instead, put additions/overrides in $HTDIG_CFG.local
  118. HOSTNAME $HOST.$DOMAIN
  119. HOSTURL http://$HOST.$DOMAIN/
  120. BASEURL http://$HOST.$DOMAIN
  121. LANGUAGE DANISH
  122. "\
  123. > $HTDIG_CFG
  124. [ -s $HTDIG_CFG.local ] && cat $HTDIG_CFG.local >> $HTDIG_CFG
  125. mkdir $SEARCHDIR/htdig
  126. fi
  127. fi
  128. # if ! lockfile-create /var/run/localwebsearch_$WEBSITE; then
  129. # # Another htdig indexing cronjob is already running
  130. # [ $DEBUG ] && echo "Another $WEBSITE indexing is already running. Ignoring silently..."
  131. # continue
  132. # fi
  133. # lockfile-touch /var/run/localwebsearch_$WEBSITE &
  134. # # Save the PID of the lockfile-touch process
  135. # BADGER="$!"
  136. if [ $stamp = "init" -o $stamp = "update" ]; then
  137. if [ -x $HTDIG_BIN -a -x $HTDIG_REAL_BIN ]; then
  138. export TMPDIR=/tmp
  139. [ $DEBUG ] && echo "$WEBSITE: Update htdig search"
  140. if [ $stamp = "init" -o -f /etc/htdig/full_refresh_$WEBSITE ]; then
  141. $HTDIG_REAL_BIN $HTDIG_OPTIONS
  142. rm -f /etc/htdig/full_refresh_$WEBSITE
  143. else
  144. $HTDIG_REAL_BIN $HTDIG_OPTIONS
  145. fi
  146. fi
  147. fi
  148. # kill "${BADGER}"
  149. # lockfile-remove /var/run/localwebsearch_$WEBSITE
  150. if [ $stamp = "init" ]; then
  151. [ $DEBUG ] && echo "Execute $WEBSITE POST_INIT"
  152. post_init $LOGROOT $WEBROOT $WEBSITE || exit1 "Error executing POST_INIT."
  153. fi
  154. if [ $stamp = "update" ]; then
  155. [ $DEBUG ] && echo "Execute $WEBSITE POST_UPDATE"
  156. post_update $LOGROOT $WEBROOT $WEBSITE || exit1 "Error executing POST_UPDATE."
  157. fi
  158. done