#!/bin/sh
#
# source:
#   /var/cvs/projects/debian/wn/debian/dpkg.src/wn.wnupdatewww.in,v
#
# revision:
#   @(#) wn.wnupdatewww.in,v 1.1 1998/05/05 03:03:20 jplejacq Exp
#
# synopsis:
#   wnupdatewww {-d | -c | -o <dir>} {-i | -m <file> [ -a <name> ]}
#
# description:
#   Enable access to files in directory tree for wn http server.
#
# see also:
#   /etc/wn/wn.conf
#   /usr/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 -a -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}


         /usr/sbin/wndex -q -d ${www_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=${www_dir}/index

   echo "# description:"                                > ${www_index}
   echo "#   wn index file."                           >> ${www_index}
   echo "#"                                            >> ${www_index}
   echo "#   Created by wnupdatewww on $(date)."       >> ${www_index}
   echo                                                >> ${www_index}
   echo "Attribute=serveall"                           >> ${www_index}
   echo "Subdirs=all"                                  >> ${www_index}

   chown root.root ${www_index}
   chmod 0644 ${www_index}

   /usr/sbin/wndex -q -d ${www_dir}
}


install_index_redirect()
{
   local www_dir=${1}
   local dir=${2}

   local www_index=${www_dir}/index

   echo "# description:"                                > ${www_index}
   echo "#    wn index file."                          >> ${www_index}
   echo "#"                                            >> ${www_index}
   echo "#    Created by wnupdatewww on $(date)."      >> ${www_index}
   echo                                                >> ${www_index}
   echo "Owner=mailto:webmaster@${wn_mailname}"        >> ${www_index}
   echo "Attribute=serveall"                           >> ${www_index}
   echo "File-Module=/usr/lib/cgi-bin/wn/wncat ${dir}"   >> ${www_index}
   echo "Subdirs=all"                                  >> ${www_index}

   chown root.root ${www_index}
   chmod 0644 ${www_index}

   /usr/sbin/wndex -q -d ${www_dir}
}


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


# 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:" 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
          ;;
        "?")
          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
      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
