#!/bin/sh
#
# rights:
#    Copyright 1999 Jean Pierre LeJacq <jplejacq@quoininc.com>.
#
#    Distributed under the GNU GENERAL PUBLIC LICENSE.
#
# source:
#    /var/cvs/projects/org/wnserver/wn/debian/src/wn/DM_FS_PKG_ETC_CRONDAILY.in,v
#
# revision:
#    1.1
#
# state:
#    Exp
#
# author:
#    jplejacq@quoininc.com
#
# last-modified:
#    2000/01/29 04:17:53
#
# last-editor:
#    jplejacq@quoininc.com
#
# description:
#    Host configuration of daily cron program for wn.
#
#    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.
#
#    Logfile rotation is done by '/etc/logrotate.d/wn' and not here.



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/wn_access.log'
    fi

    if ! is_defined wn_log_file_error
    then
      wn_log_file_error='/var/log/wn/wn_error.log'
    fi

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



update_www_std()
# synopsis:
#    update_www_std
#
# description:
#    Updates WN access to the Debian standard WWW directories.
#
#    This supports both the FHS and legacy location of Debian package
#    documentation.  The legacy location is handled first to make sure
#    that the if a directory exists in both places, the FHS directory
#    takes precedence.
#
{
  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
        /usr/sbin/wnupdatewww -o ${wn_www_root} -m /usr/doc
      fi
    fi

    if [ -d '/usr/share/doc' ]
    then
      if [ -L "${wn_www_root}/doc" ]
      then
        /usr/sbin/wnupdatewww -d -i
      elif [ -d "${wn_www_root}/doc" ]
      then
        /usr/sbin/wnupdatewww\
          -o ${wn_www_root}\
          -m '/usr/share/doc'\
          -r
      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
        /usr/sbin/wnupdatewww\
          -o ${wn_www_root}\
          -m '/usr/lib/cgi-bin'\
          -r
      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
  update_www_std
  update_man2html


  exit 0
