#!/bin/sh
#
#	$Source: /mit/wwwdev/mkserv/RCS/web.add,v $
#	$Author: bert $
#	$Id: web.add,v 1.3 1996/08/31 01:28:38 bert Exp $
#	$Revision: 1.3 $
#
#	$Platforms: sun4,decmips $

### extract the version of this mkserv script

# this will make VERSION look something like "1.0".
Revision=""; dummy=""
for VERSION in $Revision: 1.3 $dummy; do :; done

MACHTYPE="`machtype`"

### configuration parameters: various files to use, etc.

# ${DATA} is a file that stores persistent installation information.
DATA="${CONFIGDIR}/mkserv.web"

# ${SLAVE} is a file that may contain instructions for installation options.
SLAVE="${CONFIGDIR}/mkserv.web.slave"

# ${FORCE} is a file whose existence causes a forced re-install.
FORCE="${CONFIGDIR}/.force.web"

# default for server installation directory
defWEBROOT="${SITE}/local/httpd"

# domain name for the server (lowercase)
DOMAIN=".mit.edu"

# the directory with configuration templates
TEMPLATEDIR="/mit/wwwdev/conf/httpd"

# the initial pathname+filename fragment for httpd.conf templates
HTTPD_CONF="${TEMPLATEDIR}/httpd.conf."

# the initial pathname+filename fragment for index.html templates
INDEX_HTML="${TEMPLATEDIR}/index.html."

# the place where the binary distribution lives.
DIST="/mit/wwwdev/mkserv/dist/web.${MACHTYPE}.tar.Z"

# make sure path contains stuff we'll need
PATH=${PATH}:/usr/ucb:/usr/bsd:/usr/ccs/bin; export PATH

# programs
install=/usr/athena/bin/install
uncompress=uncompress
sed=sed
tarXF="tar xf"

### sanity check.

if [ ! -r "$DIST" ]; then
    cat << END
***********************************************************************
Whoops!  I can't find ${DIST}!  'mkserv web' is broken.
		  Please notify <cwis-dev@mit.edu>.
***********************************************************************
END
    exit 123
fi

### check for a previous installation.

if [ -r "$DATA" ]; then
    . ${DATA}
    PREVIOUS="$WEBROOT"
    if [ "$INSTALLED_VERSION" != "$VERSION" ]; then
	echo "(web service [${INSTALLED_VERSION}] already installed, updating)"
	UPDATE="needed"
    else
	echo "web service [${INSTALLED_VERSION}] is already installed."
	UPTODATE=1
    fi
fi

### check for "slave" installation instructions.

if [ -r "$SLAVE" ]; then
    WEBHOST=""; WEBPORT=""; TEMPLATE=""
    . ${SLAVE}
    case "$PREVIOUS" in
     "") ;;
     "$WEBROOT")
	echo "`Slave' mkserv: reinstalling the existing web server in ${WEBROOT}."
	UPDATE="slave"
	;;
     *)
	if [ -t 0 ]; then	# stdin is a tty
	    cat << END
Warning: if you continue with this installation, the web server previously
         installed in ${PREVIOUS} will *not* be de-installed, and
         mkserv will no longer be aware of its existence.
         I would suggest that you exit this installation and run "mkserv -web".
         Alternatively, you can proceed and remove the old server manually.

Proceed with this installation [no]:
END
	    read proceed
	    case "$proceed" in
	      y*|Y*)	;;
	      *)	exit 1 ;;
	    esac
	fi
	echo "===> Manually remove the web server installation in ${PREVIOUS}." >> $LOGFILE
	;;
    esac
    SLAVE_INSTALL=1
fi

### if we're up to date and not forcing a slave installation, quit.

if [ "${UPTODATE}" -a ! "${SLAVE_INSTALL}" -a ! -w "${FORCE}" ]; then
    echo "mkserv web: nothing to do, exiting."
    echo "(To force an update, touch ${FORCE})"
    exit 0
fi

### ask silly questions

if [ "$WEBROOT" ]; then
    echo "Root for server installation: $WEBROOT"
else
    if [ -t 0 ]; then	# stdin is a tty
	echo
	echo "Please enter the root directory for installation [$defWEBROOT]:"
	read WEBROOT
    fi
    if [ -z "$WEBROOT" ]; then WEBROOT="$defWEBROOT"; fi
fi

sname=`uname -n | tr A-Z a-z | awk -F. '{print $1}'`
lname="${sname}${DOMAIN}"
if [ -z "$WEBHOST" ]; then
    if [ -t 0 ]; then	# stdin is a tty
	echo "Please enter the advertised host name for your server [$lname]:"
	read WEBHOST
    fi
    if [ -z "$WEBHOST" ]; then WEBHOST=$lname; fi
fi
shost=`echo $WEBHOST | tr A-Z a-z | awk -F. '{print $1}'`

if [ -z "$WEBPORT" ]; then
    if [ -t 0 ]; then	# stdin is a tty
	echo "Please enter the port number for your server [80]:"
	read WEBPORT
    fi
    if [ -z "$WEBPORT" ]; then WEBPORT="80"; fi
fi

if [ "$TEMPLATE" -a -r "${HTTPD_CONF}${TEMPLATE}" ]; then
    echo "Using configuration template: $TEMPLATE"
else
    if [ -r "${HTTPD_CONF}${shost}" ]; then
	deftmpl="$shost"
    else
	deftmpl="plain"
	if [ ! -r "${HTTPD_CONF}${deftmpl}" ]; then
	    cat << END
***********************************************************************
Whoops!  I can't find ${HTTPD_CONF}${deftmpl}!  'mkserv web' is broken.
		  Please notify <cwis-dev@mit.edu>.
***********************************************************************
END
	    exit 124
	fi
    fi
    if [ -t 0 ]; then	# stdin is a tty
	cat << END

This script can automatically generate a custom configuration file
from one of several "templates", each of which embodies a specific
variation of WWW service.  Some of the available templates include:

  plain     A simple, no-frills configuration for easy tweaking
              (no "short URL" expansion, no AFS gateway to the world,
	       no proxying and no IP restrictions)
  web       Configuration analogous to one installed on web.mit.edu
  search    Configuration which supports a Harvest installation
	      
Please enter the server configuration template [$deftmpl]:
END
	read TEMPLATE
    else
	TEMPLATE=""
    fi
    if [ -z "$TEMPLATE" ]; then TEMPLATE="$deftmpl"; fi
    while [ ! -r "${HTTPD_CONF}${TEMPLATE}" ]; do
	if [ ! -t 0 ]; then	# stdin is *not* a tty
	    cat << END
***********************************************************************
Whoops!  Where did that template come from?  'mkserv web' is broken.
		  Please notify <cwis-dev@mit.edu>.
***********************************************************************
END
	    exit 125
	fi
	echo "Cannot find ${TEMPLATE} (file: ${HTTPD_CONF}${TEMPLATE})"
	echo "Please enter the server configuration template [$deftmpl]:"
	read TEMPLATE
	if [ -z "$TEMPLATE" ]; then TEMPLATE="$deftmpl"; fi
    done
fi

convert="sed -e s/@HOST@/${WEBHOST}/g -e s/@PORT@/${WEBPORT}/g -e s#@SRVDIR@#${WEBROOT}#g"
echo "Starting installation for ${WEBHOST}:${WEBPORT}."
echo
    
### Make sure the install directory is there.

if [ ! -d "${WEBROOT}" ]; then
    if mkdir -p "${WEBROOT}"; then
	echo "Created ${WEBROOT}."
    else
	echo "Can't mkdir -p ${WEBROOT}. =("
	exit 3
    fi
fi

### Install everything we can from a tarfile.

umask 022

cd ${WEBROOT}
$uncompress -c ${DIST} | $tarXF -

### Now, customize and install the rest

if [ ! "${UPDATE}" -o -w "${FORCE}" ]; then

# httpd.conf
    echo "convert: ${HTTPD_CONF}${TEMPLATE} -> httpd.conf"
    $convert < "${HTTPD_CONF}${TEMPLATE}" > /tmp/httpd.conf
    $install -m 644 /tmp/httpd.conf ${WEBROOT}/conf

# index.html
    if [ -r "${INDEX_HTML}${TEMPLATE}" ]; then
	echo "convert: ${INDEX_HTML}${TEMPLATE} -> index.html"
	$convert < ${INDEX_HTML}${TEMPLATE} > /tmp/index.html
	$install -m 644 /tmp/index.html ${WEBROOT}/root
    fi

# start/restart scripts
    for prog in START_HTTPD RESTART_HTTPD; do
	echo "convert: ${TEMPLATEDIR}/${prog}.template -> ${prog}"
	$convert < "${TEMPLATEDIR}/${prog}.template" > /tmp/${prog}
	$install -m 755 /tmp/${prog} ${WEBROOT}/bin
    done

# boot-time startup stuff

    if [ -d /etc/init.d ]; then	             # (sysV-style startup of services)
	echo "convert: ${TEMPLATEDIR}/init_d.http.template -> /etc/init.d/http"
	$convert < ${TEMPLATEDIR}/init_d.http.template > /tmp/http
	rm -f /etc/init.d/http /etc/rc3.d/S96http /etc/rc3.d/K96http
	$install -m 744 /tmp/http /etc/init.d
	ln -s ../init.d/http /etc/rc3.d/S96http
	ln -s ../init.d/http /etc/rc3.d/K96http
    else                                      # (BSD-style startup of serveces)
	rm -f /etc/athena/START_HTTPD
	ln -s ${WEBROOT}/bin/START_HTTPD /etc/athena/START_HTTPD
	if [ ! -r /etc/rc.local ]; then
	    echo "*** /etc/rc.local doesn't exist, creating."
	    touch /etc/rc.local
	    chmod u+x /etc/rc.local
	fi
	grep /etc/athena/START_HTTPD /etc/rc.local > /dev/null
	if [ "$?" = 1 ]; then
	    cp /etc/rc.local /etc/rc.local.old
	    cat <<END_OF_INSERT >>/etc/rc.local
### Web server startup (to disable, remove or rename /etc/athena/START_HTTPD)
if [ -f /etc/athena/START_HTTPD ]; then
    # [ -x ... ] doesn't work under Ultrix!!!  Is there a GNU /bin/test? =(
    echo "Starting web service."
    /etc/athena/START_HTTPD
fi
END_OF_INSERT
	else
	    echo "*** Web startup seems to already exist in /etc/rc.local."
	fi
    fi


fi

### add entry for www to /etc/{passwd,shadow}, if it isn't there already.

# echo "adduser hesiod www" >> ${CONFCNG}
echo "adduser nohesiod www:*:62:62:WWW server stuff:/:/dev/null" >> ${CONFCNG}

### add entry for www to /etc/group.  Too bad there is no "addgroup".

group=www;  group_entry="www::62:www"
for file in /etc/group /etc/group.local; do
    if [ -w $file ]; then
	(grep -v "^${group}:" $file; echo "$group_entry") > ${file}.new
	mv ${file}.new $file
    fi
done

### save installation options

if [ ! -d "${CONFIGDIR}" ]; then
    if mkdir -p "${CONFIGDIR}"; then
	echo "Created ${CONFIGDIR}."
    else
	echo "Can't mkdir -p ${CONFIGDIR}. =("
	exit 3
    fi
fi

rm -f ${DATA}.old
mv -f ${DATA} ${DATA}.old >/dev/null 2>&1

now=`date`

cat > ${DATA} << END_OF_DATA
# This file contains persistent data used by "mkserv web".
# Do not modify it unless you know *exactly* what you are doing.
#
# generated: $now
#

INSTALLED_VERSION="${VERSION}"

WEBROOT="${WEBROOT}"
WEBHOST="${WEBHOST}"
WEBPORT="${WEBPORT}"
TEMPLATE="${TEMPLATE}"

INITIAL_INSTALLATION_DATE="${INITIAL_INSTALLATION_DATE-$now}"

# additional information (mostly useful for mkserv web testing):
# slave mode:    ${SLAVE_INSTALL-0}
# update status: ${UPDATE-(not updating)}${UPDATE+ [$INSTALLED_VERSION]}
END_OF_DATA

# remove ${FORCE} file if it exists...
if [ -w "${FORCE}" ]; then
    echo "mkserv web: done forcing an update, removing ${FORCE}."
    rm -f "${FORCE}"
fi

exit 0
