#!/bin/sh
#
# This script installs the Meta-HTML web site for you, in the location that
# you specify.  You are prompted for the locations of various parts of the
# distribution before any copying is done.
#
set -e

if echo '\c' | grep -s c >/dev/null 2>&1
then
        ECHO_N="echo -n"
        ECHO_C=""
else
        ECHO_N="echo"
        ECHO_C='\c'
fi

# Set default locations.
servername=`hostname`
serverport=80
install_server=
install_engine=

echo ""
echo "This script installs the Meta-HTML web site for you, in the location"
echo "that you specify.  You are prompted for the locations of various parts"
echo "of the distribution before any copying is done."
echo ""
echo "The last step before actually installing is to tell you that this"
echo "will be done.  This way, you always get a chance to change your mind."
echo ""
echo "First, are you interested in installing the Meta-HTML Server or are"
echo "you interested in installing the Meta-HTML CGI Engine?"
echo ""
echo "   1) I would like to install the Meta-HTML Server."
echo "   2) I would like to install the Meta-HTML CGI Engine."
echo ""
${ECHO_N} "Please enter \`1' or \`2'. [default: 1]: ${ECHO_C}"
read temp
if [ "$temp" = "" ]; then temp=1; fi
if [ "$temp" = "1" ]; then
   install_server=true;
   installname="Meta-HTML Server";
else
   install_engine=true;
   installname="Meta-HTML CGI Engine";
fi

echo ""
echo "Okay, we'll install the $installname."
echo ""

echo "Enter a name, or press RETURN for the default."
${ECHO_N} "What name should/does the server answer to [$servername]? ${ECHO_C}"
read temp
if [ "$temp" = "" ]; then temp=$servername; fi
servername="$temp"

echo ""
echo "Enter a port number, or press RETURN for the default."
${ECHO_N} "What port should/does the server answer on [${serverport}]? ${ECHO_C}"
read temp
if [ "$temp" = "" ]; then temp=${serverport}; fi
serverport="$temp"

if [ "${bindir}" = "" ]; then bindir=/www/bin; fi
rootdir=/www/${servername}
confdir=${rootdir}/conf
logdir=${rootdir}/logs
docdir=${rootdir}/docs
cgidir=${docdir}/cgi-bin

if [ "$CALLED_FROM_Install" = "" ]; then
   echo ""
   echo "    ${bindir}:"
   echo "        This is the directory under which the Meta-HTML binaries"
   echo "        have been installed.  If you have installed the binaries"
   echo "        in a different location, you should say so when asked."
   echo ""
   ${ECHO_N} "    Enter a new value, or press RETURN to accept: ${ECHO_C}"
   read temp
   if [ "$temp" = "" ]; then temp=${bindir}; fi
   bindir=$temp
fi

if [ "$install_server" ]; then
   if [ ! -x ${bindir}/mhttpd ]; then
      echo ""
      echo "                  INSTALL ABORTED!"
      echo ""
      echo "The file \`${bindir}/mhttpd' isn't executable."
      echo "We cannot install completely without this file."
      echo ""
      exit 1
   fi

   echo ""
   echo "    ${rootdir}:"
   echo "        This is the directory under which the \`conf' and \`logs'"
   echo "        directories are to be installed."
   echo ""
   ${ECHO_N} "    Enter a new value, or press RETURN to accept: ${ECHO_C}"
   read temp
   if [ "$temp" = "" ]; then temp=${rootdir}; fi
   rootdir=$temp

   confdir=${rootdir}/conf
   logdir=${rootdir}/logs
   docdir=${rootdir}/docs

   echo ""
   echo "    ${confdir}:"
   echo "        This is the directory where the configuration files will"
   echo "        be installed."
   echo ""
   ${ECHO_N} "    Enter a new value, or press RETURN to accept: ${ECHO_C}"
   read temp
   if [ "$temp" = "" ]; then temp=${confdir}; fi
   confdir=$temp

   echo ""
   echo "    ${logdir}:"
   echo "        This is the directory under which the log files will be stored."
   echo ""
   ${ECHO_N} "    Enter a new value, or press RETURN to accept: ${ECHO_C}"
   read temp
   if [ "$temp" = "" ]; then temp=${logdir}; fi
   logdir=$temp
fi

if [ "$install_engine" ]; then
   if [ ! -x ${bindir}/engine ]; then
      echo ""
      echo "                  INSTALL ABORTED!"
      echo ""
      echo "The file \`${bindir}/engine' isn't executable."
      echo "We cannot install completely without this file."
      echo ""
      exit 1
   fi
fi

echo ""
echo "    ${docdir}:"
echo "        This is the directory under which your HTML documents will be"
echo "        served.  This install script creates sub-directories here"
${ECHO_N} "        called "
if [ -d PowerStrip ]; then
   ${ECHO_N} "\`System', \`tagsets', \`includes', "
fi
${ECHO_N} "\`release', and \`cgi-bin'."

echo ""
${ECHO_N} "    Enter a new value, or press RETURN to accept: ${ECHO_C}"
read temp
if [ "$temp" = "" ]; then temp=${docdir}; fi
docdir=$temp
cgidir=${docdir}/cgi-bin

if [ "$install_engine" ]; then
   echo ""
   echo "    ${cgidir}:"
   echo "        This is the directory where the CGI Engine will reside."
   echo ""
   ${ECHO_N} "    Enter a new value, or press RETURN to accept: ${ECHO_C}"
   read temp
   if [ "$temp" = "" ]; then temp=${cgidir}; fi
   cgidir=$temp;
fi

echo ""
echo "   Okay, we are about to install the ${installname}.  Please check"
echo "   the following locations, and make sure that they are correct. "
echo "   If you are happy with the locations, type \"yes\" as your answer."
echo "   Anything else means that you aren't ready, or need to change the"
echo "   values that you have entered. "
echo ""
echo "  Meta-HTML binaries installed in: ${bindir}"
echo "  Your server answers to the name: ${servername}"
echo "        Your server talks on port: ${serverport}"
echo "    \`release' directory placed in: ${docdir}/release"

if [ -d PowerStrip ]; then
   echo "   PowerStrip Framework placed in: ${docdir}/System"
fi

if [ "$install_server" ]; then
   echo "    Server config files placed in: ${confdir}"
   echo "       Server log files placed in: ${logdir}"
fi

echo "   CGI-BIN programs are placed in: ${cgidir}"
echo ""
${ECHO_N} "    Use these values to install the ${installname} (yes or no)? ${ECHO_C}"
read temp
echo ""

if [ "$temp" != "yes" -a "$temp" != "YES" ]; then
   echo "                    INSTALL STOPPED!"
   echo ""
   echo " Type \`./install-site\' to start over, or to enter new values."
   echo ""
   exit 1
fi

# Start by installing the distribution website.  No matter whether the
# user is installing the server or the engine, this site will be installed.
#

# Make the install directories if they do not already exist.
# Make the release dir.
set `echo ${docdir} | sed -e 's@/@ @g'`
fullpath=/
for dir in $*; do
   if [ ! -d ${fullpath}${dir} ]; then
      echo "   mkdir ${fullpath}${dir}"
      mkdir ${fullpath}${dir}
   fi
   fullpath=${fullpath}${dir}/
done

# Copy the current release over to the install area.
umask 000
${ECHO_N} "   Installing the release documentation tree...${ECHO_C}"
cp -r release ${docdir}/
echo "done."

# Copy the "Welcome.mhtml" file to top level.
${ECHO_N} "   Installing the default top-level Welcome file...${ECHO_C}"
cp Welcome.mhtml ${docdir}/
echo "done."

# Install PowerStrip if it is present.
if [ -d PowerStrip ]; then
   ${ECHO_N} "   Installing the PowerStrip framework...${ECHO_C}"
   cp -r PowerStrip/* ${docdir}/
   mv ${docdir}/welcome.mhtml ${docdir}/powerstrip.mhtml
   # Unpack the initial database.
   (cd ${docdir}/System/data/system/STARTUP; ${bindir}/dbpack *.packed;
    mv *.db ..)
   echo "done."
fi

# Install the engine or server.
echo "Installing the ${installname}"

if [ "$install_engine" ]; then
   # Make the install directories if they do not already exist.
   # Make the CGIBIN dir.
   set `echo ${cgidir} | sed -e 's@/@ @g'`
   fullpath=/
   for dir in $*; do
      if [ ! -d ${fullpath}${dir} ]; then
	 echo "   mkdir ${fullpath}${dir}"
	 mkdir ${fullpath}${dir}
      fi
      fullpath=${fullpath}${dir}/
   done

   # Install the nph-engine file there if not already installed.
   if [ ! -f ${cgidir}/nph-engine ]; then
      echo "   cp ${bindir}/engine ${cgidir}/nph-engine"
      cp ${bindir}/engine ${cgidir}/nph-engine
   fi

   # Install the engine.conf file with the desired values.

   # Save the old one if necessary.
   if [ -f ${cgidir}/engine.conf ]; then
      echo "   Saving old engine.conf..."
      echo "      cp ${cgidir}/engine.conf ${cgidir}/engine.conf.BAK"
      cp ${cgidir}/engine.conf ${cgidir}/engine.conf.BAK
   fi

   # Now make the new one.
   ${ECHO_N} "   Creating new engine.conf...${ECHO_C}"
   cat >${cgidir}/engine.conf <<EOF
;;; engine.conf: -*- Meta-HTML -*-  Typical startup file for (nph-)Engine.
;;;
;;; Copyright (c) 1996 Brian J. Fox
;;; Author: Brian J. Fox (bfox@ai.mit.edu) Sat Sep 14 03:51:33 1996.

;;; You set the variable MHTML::DOCUMENT-ROOT to tell the engine
;;; where your documents are being served from.  If the location
;;; of the engine is "/www/metahtml/docs/cgi-bin/nph-engine", then
;;; the default document root is "/www/metahtml/docs".  This may or
;;; may not be correct for your site.  Simply set the variable here if
;;; it is not.
<set-var mhtml::document-root = ${docdir}>

;;; The user that you would like the engine to run under.
;;; This only has an effect if the engine is already running as the
;;; root user.
;;; <set-var mhtml::default-user = nobody>

;;; Use cookies that appear permanent to the client.
<set-var mhtml::permanent-cookies = true>

;;; If you wish to allow your users to store documents underneath
;;; their home directories, then set this variable to be the name of
;;; the subdirectory under which users store their documents.  For
;;; example, if the URL "/~bfox/welcome.mhtml" should be served from
;;; a subdirectory of Brian Fox's home directory called "public_html",
;;; then you would do:
;;;
;;; <defvar mhtml::~directory  public_html>

;;; MHTML::EXEC-PATH is what gets passed in the environment to CGI
;;; programs as PATH.  Choose carefully.
<defvar mhtml::exec-path
  <concat /opt/metahtml/bin:${bindir}:/<get-var mhtml::document-root>/cgi-bin
	  :/bin:/usr/bin:/usr/local/bin:/usr/ucb>>

;;; Top level cgi-directory.  Actually an array of possible cgi directories.
<set-var mhtml::cgi-directories[0]=/cgi-bin/>

;;; Useful macros that you cannot live without.

;;; Always reference Meta-HTML as <meta-html>
<defsubst meta-html whitespace=delete>
  <concat
   <font color="#00309B">Meta-</font>HTML
   <if <not <get-var mhtml::printed-tm>>
       <prog <sup><font size=-2>TM</font></sup>
         <increment mhtml::printed-tm>>>>
</defsubst>
<defsubst meta-html-notm><meta-html></defsubst>

;;; The current document, without PATH_INFO or QUERY_STRING.
<defsubst thisdoc whitespace=delete>
  <get-var mhtml::url-to-dir>/<get-var mhtml::current-doc>
</defsubst>

;;; The full pathname of the current directory.
<defsubst thisdir whitespace=delete>
  <get-var mhtml::include-prefix mhtml::relative-prefix>
</defsubst>

;;; The full pathname of the current file.
<defsubst thisfile whitespace=delete>
  <concat <get-var mhtml::include-prefix mhtml::relative-prefix>
	  /<get-var mhtml::current-doc>>
</defsubst>

;;; The PTEXT macro is heavily used.
<defmacro ptext whitespace=delete>
  <plain-text first-char=<font size="+2"> nobr=true nolower=true>
    %body
  </plain-text>
</defmacro>

;;;  This page enforces a login before pages which contain sensitive
;;;  information may be viewed.
<defun signon-required>
  <if <not <get-session-var site::user-id>>
      <redirect "<get-var mhtml::http-prefix>/System/Login.mhtml">>
</defun>

;;; Analogous to MHTML::PROLOGUE-DOCUMENT is MHTML::PER-PAGE-FUNCTION.
;;; You probably only need one or the other, but not both.  If this
;;; variable is set to the name of a defun, defsubst, or defmacro,
;;; then that function is called with no arguments.  Here is the
;;; default that we recommend -- it makes all posted data innocuous.
;;;
;;; If you turn this off, you will have to make sure that your pages
;;; are written in a secure fashion.  Please see the manual
;;; description of <get-var-once> for more details.
;;;
<defun mhtml::post-secure>
  <set-var names[]=<package-vars posted strip=true>>
  <set-var i=0>
  <while <get-var-once names[i]>>
    <when <match <symbol-info posted::<get-var-once names[i]>> STRING>>
      <copy-var POSTED::<get-var-once names[i]>
		DEFAULT::<get-var-once names[i]>>
      <subst-in-var default::<get-var-once names[i]>
         "&" "&amp;" "<" "&lt;" ">" "&gt;">
    </when>
    <increment i>
  </while>
</defun>

<set-var mhtml::per-page-function = mhtml::post-secure>
EOF
   if [ -r ./MHTML.KEY ]; then
      cat MHTML.KEY >>${cgidir}/engine.conf;
   fi

   # Make sure that everyone can read this file.
   chmod a+r ${cgidir}/engine.conf
   echo "done."
fi

# If the user wanted to install the server, do the server specific parts
# now.
if [ "$install_server" ]; then
   # Make the directories where the config files live.
   echo "Creating the path to the configuration directory."
   set `echo ${confdir} | sed -e 's@/@ @g'`
   fullpath=/
   for dir in $*; do
      if [ ! -d ${fullpath}${dir} ]; then
	 echo "   mkdir ${fullpath}${dir}"
	 mkdir ${fullpath}${dir}
      fi
      fullpath=${fullpath}${dir}/
   done

   # Copy the config directory into place.
   ${ECHO_N} "Copying the configuration files...${ECHO_C}"
   cp -r server/conf/* ${confdir}
   echo "done."

   # Modify the "identity" configuration file.
   idfile="${confdir}/identity.local"
   echo "<set-var mhtml::server-name = ${servername}>"  >${idfile}
   echo "<set-var mhtml::server-port = ${serverport}>" >>${idfile}
   echo "<set-var mhtml::document-root = ${docdir}>"   >>${idfile}
   echo "<set-var mhtml::server-root = ${rootdir}>"   >>${idfile}

   # Make sure that the directory containing the utility programs is
   # in the mhtml::exec-path.
   echo ";;; MHTML::EXEC-PATH is what gets passed in the environment to CGI" >>${confdir}/advanced.local
   echo ";;; programs as PATH.  Choose carefully." >>${confdir}/advanced.local
   echo "<defvar mhtml::exec-path"		   >>${confdir}/advanced.local
   echo "   <concat ${bindir}:/opt/metahtml/bin:/www/bin:" >>${confdir}/advanced.local
   echo "           /<get-var mhtml::document-root>/cgi-bin" >>${confdir}/advanced.local
   echo "          :<get-var mhtml::server-root>/bin:/bin:/usr/bin:/usr/local/bin" >>${confdir}/advanced.local
   echo "          :/usr/ucb>>" >>${confdir}/advanced.local

   # Modify the "logging" configuration file.
   lgfile="${confdir}/logging.local"
   echo "<set-var mhtml::access-log = ${logdir}/access_log>"    >${lgfile}
   echo "<set-var mhtml::error-log = ${logdir}/error_log>"     >>${lgfile}
   echo "<set-var mhtml::referer-log = ${logdir}/referer_log>" >>${lgfile}
   echo ";;; <set-var mhtml::debug-log = ${logdir}/debug_log>" >>${lgfile}

   # Maybe create the "activation.key" file.
   if [ -r MHTML.KEY ]; then
      if [ -r ${confdir}/activation.key ]; then
	 :
      else
	 cat MHTML.KEY >${confdir}/activation.key
      fi
   fi

   # Make the directories where the log files live.
   echo "Creating the path to the log files directory."
   set `echo ${logdir} | sed -e 's@/@ @g'`
   fullpath=/
   for dir in $*; do
      if [ ! -d ${fullpath}${dir} ]; then
	 echo "   mkdir ${fullpath}${dir}"
	 mkdir ${fullpath}${dir}
      fi
      fullpath=${fullpath}${dir}/
   done
fi

# Unpack any packed databases that need unpacking...
echo "Unpacking packed databases..."
(cd ${docdir}/release;
 find . -name "*.db.packed" -exec ${bindir}/dbpack \{\} \; -print)
echo "Done."

# I think that we are done!  Let's tell them that, and split.
echo ""
echo "                     INSTALLATION COMPLETE!"
echo ""
echo "You have successfully installed the ${installname} on your system."
echo ""
if [ "$install_engine" ]; then
   echo "Now that the Engine is installed, you must ensure that your server"
   echo "can access it using normal style URLs.  For documentation on "
   echo "exactly what you need to type in your server configuration files,"
   echo "connect to the following URL:"
   echo ""
   echo "    http://${servername}/release/5.00/meta-html/manual/install.mhtml"
   echo ""
   echo "and follow the specific instructions for server."
   echo ""
fi

if [ "$install_server" ]; then
   echo "Now that the Server is installed, you are ready to start it running."
   echo ""
   echo "If you are running the server on a privileged port, such as \`80',"
   echo "you will have to start the server as the superuser.  Currently,"
   echo "you have specified to run the server on port ${serverport}."
   echo ""
   echo "To start the server, simply type:"
   echo ""
   echo "     ${bindir}/mhttpd --config ${confdir}/mhttpd.conf"
   echo ""
   echo "After you have successfully started the server, use your browser"
   echo "to connect to:"
   echo ""
   echo "     http://${servername}:${serverport}/"
   echo ""
fi

echo "Congratulations on getting <M>powered!"
echo ""
echo "Happy Hacking!"
echo ""
