#!/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_BINS_DIR/wnupdatewww.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:
#    Enable access to files in directory tree for wn http server.
#
# see:
#    /etc/wn/wn.conf
#    /usr/share/doc/wn/debian.html



insert_into_tree()
{
   local www_dir=${1}

   install_index ${www_dir}

   local file
   for file in $(ls ${www_dir})
   do
      if [ -d ${www_dir}/${file} ]
      then
         insert_into_tree ${www_dir}/${file}
      fi
   done
}


mirror_tree()
{
   local www_root=${1}
   local www_base=${2}
   local doc_root=${3}
   local doc_base=${4}

   local entry=${doc_root}/${doc_base}
   local www_entry=${www_root}/${www_base}

   if [ -f ${entry} ]
   then
      if [ -x ${entry} ]
      then
         ln -s -f ${entry} ${www_entry}
      elif [ "${entry}" != "${www_entry}" ]
      then
         # bugs:
         #    Really should add an entry in index
         #    to support renaming files.

         ln -s -f ${entry} ${www_entry}
      fi
   else
      if [ -r "${entry}/index.cache" ]
      then
         ln -s -f ${entry} ${www_entry}
      else
         install -o root -g root -m 0755 -d ${www_entry}

         install_index_redirect ${www_entry} ${entry}


         local entry_sub
         for entry_sub in $(ls ${entry})
         do
            if   [ -d ${entry}/${entry_sub} ]
            then
               mirror_tree ${www_root} \
                             ${www_base}/${entry_sub} \
                             ${doc_root} \
                             ${doc_base}/${entry_sub}
            elif [ -x ${entry}/${entry_sub} ]
            then
               ln -s -f ${entry}/${entry_sub} ${www_entry}/${entry_sub}
            fi
         done
      fi
   fi
}


install_index()
{
   local www_dir="${1}"
   local www_index_cache="${www_dir}/index.cache"


   /usr/bin/wndex -q -d "${www_dir}" -x >"${www_index_cache}" <<EOF
      Attribute=serveall
      Subdirs=all
EOF
}


install_index_redirect()
{
   local www_dir="${1}"
   local dir="${2}"
   local www_index_cache="${www_dir}/index.cache"


   /usr/bin/wndex -q -d "${www_dir}" -x >"${www_index_cache}" <<EOF
      Owner=mailto:webmaster@${wn_mailname}
      Attribute=serveall
      File-Module=/usr/lib/cgi-bin/wn/wncat ${dir}
      Subdirs=all
EOF
}


usage()
{
   echo -n "${0}: Usage: ${0} (-d | -c | -o <dir>) "
   echo    "(-i | -m <file> [ -a <name> ] [ -r ])"
}


# main:
  set -e


  # host environment:
    if [ -r "/etc/mailname" ]
    then
      wn_mailname=$(cat /etc/mailname)
    else
      wn_mailname=localhost
    fi


    if [ -r "/etc/wn/wn.conf" ]
    then
      . /etc/wn/wn.conf
    else
      std_www_root=/var/www
      wn_www_root=${std_www_root}
    fi


  # inputs:
    wn_method=
    www_root=
    www_base=
    doc_root=
    doc_base=

    OPTERR=0
    while getopts "cdo:im:a:r" OPT
    do
      case "${OPT}" in
        "c")
          if [ -z "${www_root}" ]
          then
            www_root="${wn_www_root}/cgi-bin"

            if [ ! -d "${www_root}" ]
            then
              echo "${0}: ${www_root}: No such directory"
              exit 1
            fi
          else
            usage
            exit 1
          fi
          ;;
        "d")
          if [ -z "${www_root}" ]
          then
            www_root="${wn_www_root}/doc"

            if [ ! -d "${www_root}" ]
            then
              echo "${0}: ${www_root}: No such directory"
              exit 1
            fi
          else
            usage
            exit 1
          fi
          ;;
        "o")
          if [ -z "${www_root}"  -a  -n "${OPTARG}" ]
          then
            if [ -d "${OPTARG}" ]
            then
              www_root="$(cd ${OPTARG} && pwd)"
            else
              echo "${0}: ${OPTARG}: No such directory"
              exit 1
            fi
          else
            usage
            exit 1
          fi
          ;;
        "i")
          if [ -z "${wn_method}" ]
          then
            wn_method="insert"
          else
            usage
            exit 1
          fi
          ;;
        "m")
          if [ -z "${wn_method}"  -a  -n "${OPTARG}" ]
          then
            if [ -f ${OPTARG}  -o  -d ${OPTARG} ]
            then
              wn_method="mirror"

              if [ -d "${OPTARG}" ]
              then
                doc=$(cd ${OPTARG} && pwd)
                doc_root=${doc%/*}
                doc_base=${doc##*/}
              else
                doc_root=${OPTARG%/*}
                if [ "${doc_root}" = "${OPTARG}" ]
                then
                  doc_root=$(pwd)
                else
                  doc_root=$(cd ${doc_root} && pwd)
                fi

                doc_base=${OPTARG##*/}
              fi
              www_base=${doc_base}
            else
              echo "${0}: ${OPTARG}: No such file or directory"
              exit 1
            fi
          else
            usage
            exit 1
          fi
          ;;
        "a")
          if [ "mirror" = "${wn_method}" -a -n "${OPTARG}" ]
          then
            if [ ! $(echo "${OPTARG}" | grep "/" -q) ]
            then
              www_base="${OPTARG}"
            else
              echo "${0}: ${OPTARG}: alias may not contain \"/\""
              exit 1
            fi
          else
            usage
            exit 1
          fi
          ;;
        "r")
          if [ "mirror" = "${wn_method}" ]
          then
            wn_method="mirror_cleanbefore"
          else
            usage
            exit 1
          fi
          ;;
        "?")
          usage
          exit 1
          ;;
      esac
    done


  # additional input constraints:
    if [ -z "${www_root}"  -o  -z "${wn_method}" ]
    then
      usage
      exit 1
    fi


  # process:
    if [ "mirror" = "${wn_method}" ]
    then
      mirror_tree ${www_root} ${www_base} ${doc_root} ${doc_base}
    elif [ "mirror_cleanbefore" = "${wn_method}" ]
    then
      rm -r -f ${www_root}/${www_base}
      mirror_tree ${www_root} ${www_base} ${doc_root} ${doc_base}
    else
      insert_into_tree ${www_root}
    fi


  exit 0
