#!/bin/sh
#
# source:
#   /var/cvs/projects/debian/wn/debian/dpkg.src/wn.wnconfig.in,v
#
# revision:
#   @(#) wn.wnconfig.in,v 1.5 1998/12/20 17:57:03 jplejacq Exp
#
# copyright:
#   Copyright (C) 1998 Jean Pierre LeJacq <jplejacq@quoininc.com>
#
#   Distributed under the GNU GENERAL PUBLIC LICENSE.
#
# description:
#   Host configuration script for wn package.
#
#   Much of the functions were taken from the Configure program for
#   the Linux kernel by <raymondc@microsoft.com>.
#
#   I had the following goals in mind:
#     * Consistency with a known configuration utitlity.
#     * Represent the configuration as a state.
#     * Be able to continually display and modify the state.
#     * Provide both a text and gui user interface.
#     * Provide for an empty or null answer.
#
#   This is a first step to meeting these goals.


help()
# synopsis:
#   help variable
#
# description:
#   Prints the corresponding help text from helpfile to stdout.
#
{
  local readonly helpfile="/usr/doc/wn/wnconfig.help"

  if [ -f "${helpfile}" ]
  then
    #first escape regexp special characters in the argument:
    var=$(echo "${1}"|sed 's/[][\/.^${*}]/\\&/g')
    #now pick out the right help text:
    text=$(sed -n "/^$var[ 	]*\$/,\${
			/^$var[ 	]*\$/b
			/^#.*/b
			/^[ 	]*\$/q
			p
		    }" "${helpfile}")
    if [ -z "${text}" ]
    then
      echo; echo "  Sorry, no help available for this option yet.";echo
    else
      (echo; echo "${text}"; echo) | ${PAGER:-more}
    fi
  else
    echo;
    echo "  Can't access the file ${helpfile} which"
    echo "  should contain the help texts."
    echo
  fi
}


readln()
# synopsis:
#   readln prompt default oldval
#
# description:
#   Reads a line into global variable ${ans}.
{
  if [ "${DEFAULT}" = "-d"  -a  -n "${3}" ]
  then
    echo "${1}"
    ans="${2}"
  else
    echo -n "${1}"
    if [ -z "${3}" ]
    then
      echo -n "(NEW) "
    fi
    IFS='@' read ans </dev/tty || exit 1
    if [ -z "${ans}" ]
    then
      ans="${2}"
    fi
  fi
}


define_bool()
# synopsis:
#   define_bool define value
#
# description:
#   Sets the value of a boolean argument.
#
{
  local tmp

  case "${2}" in
    \"y\" )
      echo "${1}=\"y\"" >>$CONFIG
      ;;
    \"n\" )
      echo "# ${1} is not set" >>$CONFIG
      ;;
  esac

  tmp="\${2}"
  eval "${1}=${tmp}"
}


bool()
# synopsis:
#   bool question define
#
# description:
#   Processes a boolean argument.
#
{
  old=$(eval echo "\${$2}")
  def=${old:-'n'}
  case "$def" in
    "\"y\"" )
      defprompt="\"Y\"/\"n\"/?"
      def="\"y\""
      ;;
    "\"n\"" )
      defprompt="\"N\"/\"y\"/?"
      def="\"n\""
      ;;
  esac

  while :
  do
    readln "${1} (${2}) [${defprompt}] " "${def}" "${old}"

    case "${ans}" in
      \"y\" | \"Y\" | \"yes\" | \"Yes\" )
        define_bool "${2}" "\"y\""
        break
        ;;
      \"n\" | \"N\" | \"no\" | \"No\" )
        define_bool "${2}" "\"n\""
        break
        ;;
      * )
        help "${2}"
        ;;
    esac
  done
}


define_file()
{
  local tmp

  echo "${1}=${2}" >>${CONFIG}
  tmp="\${2}"
  eval "${1}=${tmp}"
}


file()
# synopsis:
#   file prompt choices 
#
# description:
#   Reads a response that must be a absolute path file name.
#
#   The response must be inclosed in quotes and no ending /.
#
# bugs:
#   This isn't enough to guarantee a valid file.
{
  old=$(eval echo "\${$2}")
  def=${old:-${3}}
  while :
  do
    readln "${1} (${2}) [${def}] " "${def}" "${old}"

    local readonly tmp0="${ans%%\"*}"
    local readonly tmp1="${ans##*/\"}"
    local readonly tmp2="${ans##*\"}"

    if [ -z "${tmp0}"  -a  -n "${tmp1}"  -a  -z "${tmp2}" ]
    then
      define_file "${2}" "${ans}"
      break
    else
      help "${2}"
    fi
  done
}


define_string()
{
  local tmp

  echo "${1}=${2}" >>${CONFIG}

  tmp="\${2}"
  eval "${1}=${tmp}"
}


string()
# synopsis:
#   string question define
#
# description:
#   Query user with "question" using "define" as default
{
  old=$(eval echo "\${$2}")
  def=${old:-${3}}
  while :
  do
    readln "${1} (${2}) [${def}] " "${def}" "${old}"

    if [ "?" != "${ans}" ]
    then
      if $(echo "${ans}" | grep -q "^\".*\"$")
      then
        # ans must have a beginning and ending '"'.

        define_string "${2}" "${ans}"
        break
      else
        help "${2}"
      fi
    else
      help "${2}"
    fi
  done
}


choice()
# synopsis:
#   choice question choice-list default
#
# description:
#   The choice list has a syntax of:
#	NAME WHITESPACE VALUE { WHITESPACE NAME WHITESPACE VALUE }
#   The user's answer must unambigously match one of the NAMEs for
#   this function to return.  The corresponding VALUE will be set
#   to the NAME.
{
  question="$1"
  choices="$2"
  old=
  def=$3

  # determine default answer:
    names=""
    set -- $choices
    firstvar=$2
    while [ -n "$2" ]
    do
      if [ -n "$names" ]
      then
        names="$names, $1"
      else
        names="$1"
      fi

      if [ "$(eval echo \"\${$2}\")" = "${1}" ]
      then
        old=$1
        def=$1
      fi
      shift; shift
    done

  val=""
  while [ -z "$val" ]
  do
    ambg=n
    readln "$question ($names) [$def] " "$def" "$old"
    set -- $choices
    while [ -n "$1" ]
    do
      name=${1}
      case "$name" in
        "$ans"* )
          if [ "$name" = "$ans" ]
          then
            val="$2"
            name_candidate=${name}
            break  # stop on exact match
          fi

          if [ -n "$val" ]
          then
            echo
            echo \
              "  Sorry, \"$ans\" is ambiguous; please enter a longer string."
            echo
            val=""
            ambg=y
            break
          else
            val="$2"
            name_candidate=${name}
          fi
          ;;
      esac
      shift; shift
    done

    if [ "$val" = "" -a "$ambg" = "n" ]
    then
      help "$firstvar"
    fi
  done

  define_string "${val}" "${name_candidate}"
}


state_old_set()
# synopsis:
#   state_old_set
#
# description:
#   Determines the current configuration of the host system and wn.
#
{
  if [ -r "/etc/wn/wn.conf" ]
  then
    . /etc/wn/wn.conf
  fi

  state_old_wn_www_root="\"${wn_www_root}\""
  state_old_wn_daemon="\"${wn_daemon}\""
  state_old_wn_user="\"${wn_user}\""
  state_old_wn_log_format="\"${wn_log_format}\""
  state_old_wn_log_format_debug="\"${wn_log_format_debug}\""
  state_old_wn_log_syslog="\"${wn_log_syslog}\""
  state_old_wn_log_file_access="\"${wn_log_file_access}\""
  state_old_wn_log_file_error=\""${wn_log_file_error}\""

  if [ -d "/usr/doc" ]
  then
    state_old_wn_doc="\"/usr/doc\""
  fi

  if [ -d "/usr/lib/cgi-bin" ]
  then
    state_old_wn_cgibin="\"/usr/lib/cgi-bin\""
  fi

  if [ -n "${wn_www_root}"  -a  -d "${wn_www_root}/doc" ]
  then
    state_old_wn_doc_access="\"dir\""
  elif [ -n "${wn_www_root}"  -a  -L "${wn_www_root}/doc" ]
  then
    state_old_wn_doc_access="\"link\""
  else
    state_old_wn_doc_access="\"\""
  fi

  if [ -n "${wn_www_root}"  -a  -d "${wn_www_root}/cgi-bin" ]
  then
    state_old_wn_cgibin_access="\"dir\""
  elif [ -n "${wn_www_root}"  -a  -L "${wn_www_root}/cgi-bin" ]
  then
    state_old_wn_cgibin_access="\"link\""
  else
    state_old_wn_cgibin_access="\"\""
  fi


  state_old_wn_www_root_page="\"\""
  if [ -d ${wn_www_root} ]
  then
    if [ -r ${wn_www_root}/index.html ]
    then
      state_old_wn_www_root_page="\"${wn_www_root}/index.html\""
    fi
  fi

  state_old_wn_www_root_index="\"\""
  if [ -d ${wn_www_root} ]
  then
    if [ -r ${wn_www_root}/index ]
    then
      state_old_wn_www_root_index="\"${wn_www_root}/index\""
    fi
  fi
}


state_new_set()
# synopsis:
#   state_new_set config
#
# description:
#   Sets the new configuration desired.
#
{
  case "${1}" in
    "state_std" | "state_old" )
      state_new_wn_www_root="$(eval echo \"\${${1}_wn_www_root}\")"
      state_new_wn_www_root_page="$(eval echo \"\${${1}_wn_www_root_page}\")"
      state_new_wn_www_root_index="$(eval echo \"\${${1}_wn_www_root_index}\")"
      state_new_wn_doc="$(eval echo \"\${${1}_wn_doc}\")"
      state_new_wn_cgibin="$(eval echo \"\${${1}_wn_cgibin}\")"
      state_new_wn_daemon="$(eval echo \"\${${1}_wn_daemon}\")"
      state_new_wn_user="$(eval echo \"\${${1}_wn_user}\")"
      state_new_wn_log_format="$(eval echo \"\${${1}_wn_log_format}\")"
      state_new_wn_log_format_debug="$(eval echo \"\${${1}_wn_log_format_debug}\")"
      state_new_wn_log_syslog="$(eval echo \"\${${1}_wn_log_syslog}\")"
      state_new_wn_log_file_access="$(eval echo \"\${${1}_wn_log_file_access}\")"
      state_new_wn_log_file_error="$(eval echo \"\${${1}_wn_log_file_error}\")"
      state_new_wn_doc_access="$(eval echo \"\${${1}_wn_doc_access}\")"
      state_new_wn_cgibin_access="$(eval echo \"\${${1}_wn_cgibin_access}\")"
      ;;
    "state_new" )
      file 'Debian WWW root directory' state_new_wn_www_root

      choice 'Debian WWW root page' \
        "\"/usr/lib/wn/index.html.dpkg-default\" state_new_wn_www_root_page \
         ${state_old_wn_www_root_page}             state_new_wn_www_root_page \
         \"\"                                      state_new_wn_www_root_page" \
         state_new_wn_www_root_page

      choice 'Debian WWW root index' \
        "\"/usr/lib/wn/index.dpkg-default\" state_new_wn_www_root_index \
         ${state_old_wn_www_root_index}       state_new_wn_www_root_index \
         \"\"                                 state_new_wn_www_root_index" \
         state_new_wn_www_root_index

      file 'Debian package documentation directory' state_new_wn_doc
      file 'Debian WWW cgi-bin directory' state_new_wn_cgibin

      choice 'WN daemon' \
        "\"/usr/sbin/wnd\"  state_new_wn_daemon \
         \"/usr/sbin/wnsd\" state_new_wn_daemon" "/usr/sbin/wnd"

      string 'WN user access control options' state_new_wn_user

      choice 'WN log format' \
        "\"verbose\" state_new_wn_log_format \
         \"ncsa\"    state_new_wn_log_format \
         \"common\"  state_new_wn_log_format" "verbose"

      bool 'WN log include debugging info' state_new_wn_log_format_debug

      bool 'WN logging to UNIX syslog' state_new_wn_log_syslog

      file 'WN access log file' state_new_wn_log_file_access
      file 'WN_error log file' state_new_wn_log_file_error

      choice 'WN access to Debian package documentation directory' \
        "\"link\" state_new_wn_doc_access \
         \"dir\"  state_new_wn_doc_access \
         \"\" state_new_wn_doc_access"      "dir"

      choice 'WN access to Debian WWW cgi-bin directory' \
        "\"link\" state_new_wn_cgibin_access \
         \"dir\"  state_new_wn_cgibin_access \
         \"\" state_new_wn_cgibin_access"      "dir"
      ;;
  esac
}


state_std_set()
{
  state_std_wn_www_root="\"/var/www\""
  state_std_wn_www_root_page="\"/usr/lib/wn/index.html.dpkg-default\""
  state_std_wn_www_root_index="\"/usr/lib/wn/index.dpkg-default\""
  state_std_wn_doc="\"/usr/doc\""
  state_std_wn_cgibin="\"/usr/lib/cgi-bin\""
  state_std_wn_daemon="\"/usr/sbin/wnd\""
  state_std_wn_log_format="\"verbose\""
  state_std_wn_log_format_debug="\"n\""
  state_std_wn_log_syslog="\"y\""
  state_std_wn_log_file_access="\"\""
  state_std_wn_log_file_error="\"\""
  state_std_wn_user="\"-a 0\""
  state_std_wn_doc_access="\"dir\""
  state_std_wn_cgibin_access="\"dir\""
}


state_display()
# synopsis:
#   state_display state
#
# description:
#   Displays state on standard output.
# 
{
  echo    "*"
  echo    "* WN ${1} configuration"
  echo    "*"
  echo -n "Debian WWW root directory: "
  echo    "$(eval echo \"\${${1}_wn_www_root}\")"
  echo -n "Debian WWW root page: "
  echo    "$(eval echo \"\${${1}_wn_www_root_page}\")"
  echo -n "Debian WWW root index: "
  echo    "$(eval echo \"\${${1}_wn_www_root_index}\")"
  echo -n "Debian package documentation directory: "
  echo    "$(eval echo \"\${${1}_wn_doc}\")"
  echo -n "Debian WWW cgi-bin directory: "
  echo    "$(eval echo \"\${${1}_wn_cgibin}\")"
  echo -n "WN daemon: "
  echo    "$(eval echo \"\${${1}_wn_daemon}\")"
  echo -n "WN user access control options: "
  echo    "$(eval echo \"\${${1}_wn_user}\")"
  echo -n "WN log format: "
  echo    "$(eval echo \"\${${1}_wn_log_format}\")"
  echo -n "WN log include debugging info: "
  echo    "$(eval echo \"\${${1}_wn_log_format_debug}\")"
  echo -n "WN logging to UNIX syslog: "
  echo    "$(eval echo \"\${${1}_wn_log_syslog}\")"
  echo -n "WN access log file: "
  echo    "$(eval echo \"\${${1}_wn_log_file_access}\")"
  echo -n "WN error log file: "
  echo    "$(eval echo \"\${${1}_wn_log_file_error}\")"
  echo -n "WN access to Debian package documentation directory: "
  echo    "$(eval echo \"\${${1}_wn_doc_access}\")"
  echo -n "WN access to Debian WWW cgi-bin directory: "
  echo    "$(eval echo \"\${${1}_wn_cgibin_access}\")"
}


state_save()
# synopsis:
#   state_save state
#
# description:
#   Configures running system to desired state.
#
#   Two eval are needed to strip off the extra " when creating directories.
{
  local wn_conf_tmp


  if [ ! -d $(eval echo \"\${${1}_wn_www_root}\") ]
  then
    if [ "y" = "${debug}" ]
    then
      echo -n "Creating Debian WWW root directory: "
      echo    "$(eval echo \"\${${1}_wn_www_root}\")"
    else
      tmp=$(eval eval echo \"\${${1}_wn_www_root}\")
      install -o root -g www-data -m 0775 -d "${tmp}"
    fi
  fi

  if [ ! -d $(eval echo \"\${${1}_wn_doc}\") ]
  then
    if [ "y" = "${debug}" ]
    then
      echo -n "Creating Debian package documentation directory: "
      echo    "$(eval echo \"\${${1}_wn_doc}\")"
    else
      tmp=$(eval eval echo \"\${${1}_wn_doc}\")
      install -o root -g root -m 0755 -d ${tmp}
    fi
  fi


  if [ ! -d $(eval echo \"\${${1}_wn_cgibin}\") ]
  then
    if [ "y" = "${debug}" ]
    then
      echo -n "Creating Debian WWW cgi-bin directory: "
      echo    "$(eval echo \"\${${1}_wn_cgibin}\")"
    else
      tmp=$(eval eval echo \"\${${1}_wn_cgibin}\")
      install -o root -g root -m 0755 -d ${tmp}
    fi
  fi


  index_add \
    "$(eval echo \"\${${1}_wn_www_root}\")" \
    "$(eval echo \"\${${1}_wn_www_root_page}\")" \
    "$(eval echo \"\${${1}_wn_www_root_index}\")"


  access_add \
    "$(eval echo \"\${${1}_wn_doc}\")" \
    "$(eval echo \"\${${1}_wn_www_root}\")" \
    "doc" \
    "$(eval echo \"\${${1}_wn_doc_access}\")"

  access_add \
    "$(eval echo \"\${${1}_wn_cgibin}\")" \
    "$(eval echo \"\${${1}_wn_www_root}\")" \
    "cgi-bin" \
    "$(eval echo \"\${${1}_wn_cgibin_access}\")"


  #
  # Disable use of logfiles till I've worked out how to do this with
  # correct ownership.
  #
  # logfile_add $(eval echo \"\${${1}_wn_log_file_access}\")
  # logfile_add $(eval echo \"\${${1}_wn_log_file_error}\")


  if [ ! -d /etc/wn ]
  then
    if [ "y" = "${debug}" ]
    then
      echo -n "Creating WN configuration directory: "
      echo    "/etc/wn"
    else
      install -o root -g root -m 0755 -d /etc/wn
    fi
  fi

  if [ ! -f /etc/wn/wn.conf ]
  then
    if [ "y" = "${debug}" ]
    then
      echo -n "Creating WN configuration file: "
      echo    "/etc/wn/wn.conf"
    else
      wn_conf_default ${1} /etc/wn/wn.conf
      chown root.root /etc/wn/wn.conf
      chmod 0644 /etc/wn/wn.conf
    fi
  else
    if [ "y" = "${debug}" ]
    then
      echo -n "Backing-up WN configuration file: "
      echo    "/etc/wn/wn.conf.dpkg-old"
    else
      if [ -e /etc/wn/wn.conf.dpkg-old ]
      then
        rm -f -r /etc/wn/wn.conf.dpkg-old
      fi

      install \
        -o root \
        -g root \
        -m 0644 \
        /etc/wn/wn.conf \
        /etc/wn/wn.conf.dpkg-old
    fi
  fi


  if [ "y" = "${debug}" ]
  then
    echo -n "Updating WN configuration file: "
    echo    "/etc/wn/wn.conf"
  else
    readonly wn_conf_tmp=$(tempfile --directory /etc/wn --mode 0600)


    cat /etc/wn/wn.conf > ${wn_conf_tmp}

    if ! $(grep -q "^ *wn_www_root=" ${wn_conf_tmp})
    then
      echo \
        "  wn_www_root=$(eval echo \"\${${1}_wn_www_root}\")" \
        >> ${wn_conf_tmp}
    fi

    if ! $(grep -q "^ *wn_daemon=" ${wn_conf_tmp})
    then
      echo \
        "  wn_daemon=$(eval echo \"\${${1}_wn_daemon}\")" \
        >> ${wn_conf_tmp}
    fi

    if ! $(grep -q "^ *wn_user=" ${wn_conf_tmp})
    then
      echo \
        "  wn_user=$(eval echo \"\${${1}_wn_user}\")" \
        >> ${wn_conf_tmp}
    fi

    if ! $(grep -q "^ *wn_log_format=" ${wn_conf_tmp})
    then
      echo \
        "  wn_log_format=$(eval echo \"\${${1}_wn_log_format}\")" \
        >> ${wn_conf_tmp}
    fi

    if ! $(grep -q "^ *wn_log_format_debug=" ${wn_conf_tmp})
    then
      echo \
        "  wn_log_format_debug=$(eval echo \"\${${1}_wn_log_format_debug}\")" \
        >> ${wn_conf_tmp}
    fi

    if ! $(grep -q "^ *wn_log_syslog=" ${wn_conf_tmp})
    then
      echo \
        "  wn_log_syslog=$(eval echo \"\${${1}_wn_log_syslog}\")" \
        >> ${wn_conf_tmp}
    fi

    if ! $(grep -q "^ *wn_log_file_error=" ${wn_conf_tmp})
    then
      echo \
        "  wn_log_file_error=$(eval echo \"\${${1}_wn_log_file_error}\")" \
        >> ${wn_conf_tmp}
    fi

    if ! $(grep -q "^ *wn_log_format=" ${wn_conf_tmp})
    then
      echo \
        "  wn_log_format=$(eval echo \"\${${1}_wn_log_format}\")" \
        >> ${wn_conf_tmp}
    fi


    sed -e " \
      s^wn_www_root=.\+^wn_www_root=$(eval echo \"\${${1}_wn_www_root}\")^; \
      s^wn_daemon=.\+^wn_daemon=$(eval echo \"\${${1}_wn_daemon}\")^; \
      s^wn_user=.\+^wn_user=$(eval echo \"\${${1}_wn_user}\")^; \
      s^wn_log_format=.\+^wn_log_format=$(eval echo \"\${${1}_wn_log_format}\")^; \
      s^wn_log_format_debug=.\+^wn_log_format_debug=$(eval echo \"\${${1}_wn_log_format_debug}\")^; \
      s^wn_log_syslog=.\+^wn_log_syslog=$(eval echo \"\${${1}_wn_log_syslog}\")^; \
      s^wn_log_file_access=.\+^wn_log_file_access=$(eval echo \"\${${1}_wn_log_file_access}\")^; \
      s^wn_log_file_error=.\+^wn_log_file_error=$(eval echo \"\${${1}_wn_log_file_error}\")^; \
    " \
    ${wn_conf_tmp} > /etc/wn/wn.conf

    chown root.root /etc/wn/wn.conf
    chmod 0644 /etc/wn/wn.conf

    rm -f ${wn_conf_tmp}
  fi

  if [ "y" = "${debug}" ]
  then
    echo "Updating Debian services inetd and init."
  else
    services_add "${1}"
  fi


  if [ "y" = "${debug}" ]
  then
    echo "Executing wn cron program."
  else
    if [ -x "/etc/cron.daily/wn" ]
    then
      /etc/cron.daily/wn
    fi
  fi
}


wn_conf_default()
# synopsis:
#   wn_conf_default state file
#
# description:
#   Creates a simple default configuration.
{
  echo "# -*-Mode: Shell-script;-*-" > ${2}
  echo "#" >> ${2}
  echo "# revision:" >> ${2}
  echo "#   @(#) wn.wnconfig.in,v 1.5 1998/12/20 17:57:03 jplejacq Exp" >> ${2}
  echo "#" >> ${2}
  echo "# copyright:" >> ${2}
  echo "#   Copyright (C) 1998 Jean Pierre LeJacq <jplejacq@quoininc.com>" >> ${2}
  echo "#" >> ${2}
  echo "#   Distributed under the GNU GENERAL PUBLIC LICENSE." >> ${2}
  echo "#" >> ${2}
  echo "# description:" >> ${2}
  echo "#   Configuration variables for wn." >> ${2}
  echo "" >> ${2}
  echo "# mandatory:" >> ${2}
  echo "  wn_www_root=\"$(eval echo \"\${${1}_wn_www_root}\")\"" >> ${2}
  echo "  wn_daemon=\"$(eval echo \"\${${1}_wn_daemon}\")\"" >> ${2}
  echo " >> ${2}
  echo "# optional: >> ${2}
  echo "  wn_user=\"$(eval echo \"\${${1}_wn_user}\")\"" >> ${2}
  echo "" >> ${2}
  echo "  wn_log_format=\"$(eval echo \"\${${1}_wn_log_format}\")\"" >> ${2}
  echo "  wn_log_format_debug=\"$(eval echo \"\${${1}_wn_log_format_debug}\")\"" >> ${2}
  echo "  wn_log_syslog=\"$(eval echo \"\${${1}_wn_log_syslog}\")\"" >> ${2}
  echo "  wn_log__file_access=\"$(eval echo \"\${${1}_wn_log_file_access}\")\"" >> ${2}
  echo "  wn_log_file_error=\"$(eval echo \"\${${1}_wn_log_file_error}\")\"" >> ${2}
}


access_remove()
# synopsis:
#   access_remove directory
#
# description:
#   Recurses through directory removing index and index.cache files.
#
# bugs:
#   This is too drastic.  Some packages may define their own
#   index or index.cache files.  Should examine the files
#   themselves to see if they were created by WN package.
{
  find ${1} \
    -type f -name index -exec rm -f {} \; -o \
    -type f -name index.cache -exec rm -f {} \;
}


access_add()
# synopsis:
#   access_add source_dir www_root target target_type
#
# description:
#   Enables access to "source_dir" under "www_root" using name
#   "target" of type "target_type" which is assumed to be one of [
#   "link" | "dir" | "" ].
{
  # strip off enclosing '"':
    local readonly source_dir=$(echo ${1} | sed -e "s%^\"\(.*\)\"$%\1%;")
    local readonly target_dir=$(echo ${2} | sed -e "s%^\"\(.*\)\"$%\1%;")
    local readonly target_name=$(echo ${3} | sed -e "s%^\"\(.*\)\"$%\1%;")

  local readonly target_type="${4}"
  local readonly target="${target_dir}/${target_name}"

  case "${target_type}" in
    "\"link\"")
      if [ -d "${target}" ]
      then
        if [ "y" = "${debug}" ]
        then
          echo "Renaming existing WN access (dir) to: ${source_dir}"
        else
          if [ -e ${target}.dpkg-old ]
          then
            rm -f -r ${target}.dpkg-old
          fi

          mv "${target}" "${target}.dpkg-old"
        fi
      fi

      if [ ! -L "${target}" ]
      then
        if [ "y" = "${debug}" ]
        then
          echo "Creating WN access (link) to: ${source_dir}"
        else
          ln -s "${source_dir}" "${target}"
        fi
      fi
      ;;
    "\"dir\"")
      if [ -L "${target}" ]
      then
        if [ "y" = "${debug}" ]
        then
          echo "Removing existing WN access (link) to: ${source_dir}"
        else
          access_remove "${target}"
          rm -f "${target}"
        fi
      fi

      if [ -d "${target}" ]
      then
        if [ "y" = "${debug}" ]
        then
          echo "Renaming existing WN access (dir) to: ${source_dir}"
        else
          if [ -e "${target}.dpkg-old" ]
          then
            rm -f -r ${target}.dpkg-old
          fi

          mv "${target}" "${target}.dpkg-old"
        fi
      fi

      install -o root -g root -d ${target}
      ;;
    \""\"")
      if [ -L "${target}" ]
      then
        if [ "y" = "${debug}" ]
        then
          echo "Removing existing WN access (link) to: ${source_dir}"
        else
          access_remove "${target}"
          rm -f "${target}"
        fi
      elif [ -d "${target}" ]
      then
        if [ "y" = "${debug}" ]
        then
          echo "Renaming existing WN access (dir) to: ${source_dir}"
        else
          if [ -e "${target}.dpkg-old" ]
          then
            rm -f -r ${target}.dpkg-old
          fi

          mv "${target}" "${target}.dpkg-old"
        fi
      fi
      ;;
  esac
}


logfile_add()
# synopsis:
#   logfile_add file
#
# description:
#   Creates directory for file if required and sets proper
#   permissions.
#
# warning:
#   DON'T USE FOR NOW UNTIL LOGFILE OWNERSHIP ISSUES SOLVED.
{
  if [ -n "${1}" ]
  then
    # strip off enclosing '"':
      local readonly log_file=$(echo ${1} | sed -e "s%^\"\(.*\)\"$%\1%;")

    # find directory by striping off ending '/*':
      local readonly log_dir=$(echo ${log_file} | sed -e "s%^\(.*\)/[^/]*$%\1%;")

    if [ ! -d ${log_dir} ]
    then
      install -o root -g adm -m 2775 -d ${log_dir}
    fi

    if [ ! -r ${log_file} ]
    then
      touch ${log_file}
      chown root.adm ${log_file}
      chmod 0600 ${log_file}
    fi
  fi
}


index_add()
# synopsis:
#   index_add www_root www_root_page www_root_index
#
# description:
#   Installs www_root_page and www_root_index into www_root
{
  # strip off enclosing '"':
    local readonly dir=$(echo ${1} | sed -e "s%^\"\(.*\)\"$%\1%;")
    local readonly page=$(echo ${2} | sed -e "s%^\"\(.*\)\"$%\1%;")
    local readonly index=$(echo ${3} | sed -e "s%^\"\(.*\)\"$%\1%;")

  if [ -d "${dir}" ]
  then
    if [ ! -f "${dir}/index.html"  -a  -f "${page}" ]
    then
      install -o root -g root -m 0644 "${page}" "${dir}/index.html"
    fi

    if [ ! -f "${dir}/index"  -a  -f "${index}" ]
    then
      install -o root -g root -m 0644 "${index}" "${dir}/index"
    fi

    if [ ! -f "${dir}/index.cache"  -a  -r "${dir}/index" ]
    then
      /usr/sbin/wndex -q -d "${dir}"
    fi
  fi
}


services_add()
# synopsis:
#   services_add state
#
# description:
#   Configures support for the standard Debian services inetd and init.
{
  case $(eval echo \"\${${1}_wn_daemon}\") in
    "\"/usr/sbin/wnsd\"" )
      if $(grep -q "www" /etc/inetd.conf)
      then
        update-inetd --disable www
      fi

      if [ -f "/etc/init.d/wn"  -a  -x "/etc/init.d/wn" ]
      then
        /etc/init.d/wn restart
      else
        if [ -f "/usr/lib/wn/init.d.dpkg-default" ]
        then
          install -m 0755\
            /usr/lib/wn/init.d.dpkg-default\
            /etc/init.d/wn

          if [ "/etc/init.d/wn" != "/etc/init.d/wn" ]
          then
            ln -f -s /etc/init.d/wn /etc/init.d/wn
          fi

          /etc/init.d/wn start
        else
          echo -n "${0}: /etc/init.d/wn or /usr/lib/wn/init.d.dpkg-default"
          echo "not found."
          echo "Must supply this file before starting wnsd."
          exit 1
        fi
      fi
      ;;
    "\"/usr/sbin/wnd\"" )
      if [ -x "/etc/init.d/wn" ]
      then
        set +e
        /etc/init.d/wn stop
        set -e
      fi

      if $(grep -q "www" /etc/inetd.conf)
      then
        update-inetd --enable www
      else
        update-inetd --add \
          "www		stream	tcp	nowait	nobody	/usr/sbin/tcpd	/etc/wn/wn.rc"
      fi
      ;;
  esac
}


# main:
  set -e


  # global variables:
    CONFIG="/dev/null"
    debug="n"


  echo "*"
  echo "* Debian WN configuration program"
  echo "*"
  help "wn_config_intro"


  state_std_set
  state_old_set

  wn_config="old"
  state_new_wn_www_root=${state_old_wn_www_root}
  state_new_wn_www_root_page=${state_old_wn_www_root_page}
  state_new_wn_www_root_index=${state_old_wn_www_root_index}
  state_new_wn_doc=${state_old_wn_doc}
  state_new_wn_cgibin=${state_old_wn_cgibin}
  state_new_wn_daemon=${state_old_wn_daemon}
  state_new_wn_user=${state_old_wn_user}
  state_new_wn_log_format=${state_old_wn_log_format}
  state_new_wn_log_format_debug=${state_old_wn_log_format_debug}
  state_new_wn_log_syslog=${state_old_wn_log_syslog}
  state_new_wn_log_file_access=${state_old_wn_log_file_access}
  state_new_wn_log_file_error=${state_old_wn_log_file_error}
  state_new_wn_doc_access=${state_old_wn_doc_access}
  state_new_wn_cgibin_access=${state_old_wn_cgibin_access}

  while [ "exit" != "${wn_config}" ]
  do
    echo
    echo "*"
    echo "* Debian WN configuration menu"
    echo "*"
    choice 'Debian WN configuration' \
      "std  wn_config \
       old  wn_config \
       new  wn_config \
       show wn_config \
       save wn_config \
       exit wn_config" new

    case "${wn_config}" in
      "std" )
        state_new_set state_std
        ;;
      "old" )
        state_new_set state_old
        ;;
      "new" )
        state_new_set state_new
        ;;
      "show" )
        state_display state_new
        ;;
      "save" )
        state_save state_new
        ;;
      "exit" )
        ;;
    esac
  done


  exit 0
