#!/bin/sh
#
# source:
#   /var/cvs/projects/debian/wn/debian/dpkg.src/wn.cron.daily.in,v
#
# revision:
#   @(#) wn.cron.daily.in,v 1.6 1999/02/02 03:32:24 jplejacq Exp
#
# copyright:
#   Copyright (C) 1998 Jean Pierre LeJacq <jplejacq@quoininc.com>
#
#   Distributed under the GNU GENERAL PUBLIC LICENSE.
#
# description:
#   Host configuration of daily cron program for wn.
#
#   wn support logging either through its own log files or through
#   syslogd.  This scripts assumes that if no log file exists, the
#   syslogd approach is used and no rotation is done.
#
#   The /var/www/doc /var/www/cgi-bin hierarchies are
#   replicated as well.  The approach used here will mirror all new
#   entries and update existing ones.  It will not remove entries for
#   packages that have been removed.
#
#   run-parts gurantees that this program will be run after
#   /etc/cron.daily/dwww.  This insures that access will be available
#   to dwww through wn.


is_defined()
# synopsis:
#   is_defined variable
#
# description:
#   Returns true if variable is defined in the current shell, false otherwise.
{
  if $(set | grep -q "${1}")
  then
    return 0
  else
    return 1
  fi
}


defaults_set()
# synopsis:
#   defaults_set
#
# description:
#   Sets defaults for configuration variables if they aren't defined.
{
  if ! is_defined wn_log_syslog
  then
    wn_log_syslog="y"
  fi

  if ! is_defined wn_log_file_access
  then
    wn_log_file_access="/var/log/wn/access"
  fi

  if ! is_defined wn_log_file_error
  then
    wn_log_file_error="/var/log/wn/error"
  fi

  if ! is_defined wn_www_root
  then
    wn_www_root="/var/www"
  fi
}


savelog_and_restart()
# synopsis:
#   savelog_and_restart
#
# description:
#   Rotate log files and signals daemon to reload file.
{
  # rotate files:
    if [ "n" = "${wn_log_syslog}" ]
    then
      if [ -n "${wn_log_file_access}" ]
      then
        savelog -u nobody -g adm -m 0600 -t ${wn_log_file_access} > /dev/null
      fi

      if [ -n "${wn_log_file_error}" ]
      then
        savelog -u nobody -g adm -m 0600 -t ${wn_log_file_error} > /dev/null
      fi
    elif [ "y" = "${wn_log_syslog}"  -a  -n "${wn_log_file_error}" ]
    then
      if [ -n "${wn_log_file_error}" ]
      then
        savelog -u nobody -g adm -m 0600 -t ${wn_log_file_error} > /dev/null
      fi
    fi

  # restart daemon:
    if [ -x /etc/init.d/wn  -a  "/usr/sbin/wnsd" = "${wn_daemon}" ]
    then
      /etc/init.d/wn reload > /dev/null
    fi
}


update_www_std()
# synopsis:
#   update_www_std
#
# description:
#   Updates WN access to the Debian standard WWW directories.
{
  local file

  if [ -x /usr/sbin/wnupdatewww ]
  then
    if [ -d /usr/doc ]
    then
      if [ -L "${wn_www_root}/doc" ]
      then
        /usr/sbin/wnupdatewww -d -i
      elif [ -d "${wn_www_root}/doc" ]
      then
        rm -f -r ${wn_www_root}/doc
        /usr/sbin/wnupdatewww -o ${wn_www_root} -m /usr/doc
      fi
    fi

    if [ -d /usr/lib/cgi-bin ]
    then
      if [ -L "${wn_www_root}/cgi-bin" ]
      then
        /usr/sbin/wnupdatewww -c -i
      elif [ -d "${wn_www_root}/cgi-bin" ]
      then
        rm -f -r ${wn_www_root}/cgi-bin
        /usr/sbin/wnupdatewww -o ${wn_www_root} -m /usr/lib/cgi-bin
      fi
    fi

    if [ -d "${wn_www_root}/dwww" ]
    then
      /usr/sbin/wnupdatewww -o ${wn_www_root}/dwww -i
    fi
  fi
}


update_man2html()
# synopsis:
#   update_man2html()
#
# description:
#   Updates man2html cache.
#
#   This must be done by this script since the man2html cgi programs
#   are run as user "nobody" who doesn't have write permission in
#   /var/cache/man2html.  We get around this problem by
#   recalculating the cache once a day.  This should minimize the
#   number of write permission denied problems since nothing will need
#   to be written into the directory as long as no man pages are added
#   after this program is run.
#
# bugs:
#   This doesn't solve the problem of having added a man page and this
#   program not having been run.  You will get a permission denied
#   from the server in that case.
#
#   The program "mansec" will segfault if there is a dangling link in
#   the man directories.  Maybe I should check for that to provide a
#   better diagnostic message.
#
{
  local section

  if [ -x "/usr/lib/cgi-bin/manwhatis"  -a  -x "/usr/lib/cgi-bin/manwhatis" ]
  then
    if [ -x "/usr/bin/mandb" ]
    then
      #
      # We first need to update man-db's cache.
      #

      set +e
      /usr/bin/mandb --quiet > /dev/null
      set -e
    fi

    if [ ! -d "/var/cache/man2html" ]
    then
      install -o root -g www-data -m 2775 /var/cache/man2html
    fi

    rm -f /var/cache/man2html/whatis-*
    rm -f /var/cache/man2html/manindex-*

    for section in "1" "2" "3" "4" "5" "6" "7" "8"
    do
      (
        umask 022
        cd /
        /usr/lib/cgi-bin/manwhatis ${section} > /dev/null
      )

      (
        umask 022
        cd /
        /usr/lib/cgi-bin/mansec ${section} > /dev/null
      )
    done

    (
      # "mansec all" produces a file manindex-10.html which is the
      # required name.

      umask 022
      cd /var/cache/man2html
      /usr/lib/cgi-bin/mansec all > /dev/null
    )
  fi
}


# main:
  set -e


  if [ -f "/etc/wn/wn.conf" ]
  then
    . /etc/wn/wn.conf
  fi

  defaults_set
  savelog_and_restart
  update_www_std
  update_man2html


  exit 0
