#! /bin/sh
#
#	This script adds WIMS-related definitions to Apache config.
#	It must be run as root.
#
#	If you want to prohibit the action of this script,
#	insert the following word to httpd.conf (in a commented line):
#	manually_configured_for_wims
#

if [ ! `whoami` = "root" ]; then
 echo
 echo This script adds WIMS-related definitions to Apache config.
 echo It must be run as root.
 echo
 echo su to root before running this script!
 echo
 echo
 exit
fi

# httpd or apache?
if [ -d /etc/httpd/conf ]; then
 httpdname=httpd
 confdir=/etc/httpd/conf
else
 if [ -e /etc/httpd/httpd.conf ]; then
  httpdname=httpd
  confdir=/etc/httpd
 else
  httpdname=apache
  confdir=/etc/apache/conf
 fi
fi

PATH=$PATH:/usr/sbin:/sbin
export PATH
vtest=`$httpdname -v | grep Apache`
if [ -z "$vtest" ]; then
 echo
 echo You are not running Apache httpd!
 echo
 echo You must configure manually your httpd, this script cannot be used.
 echo
 exit
fi

cd `dirname $0`/..
if [ ! -e public_html/wims.cgi ] || [ ! -e bin/apache-config ]; then
 echo Error: wrong directory. File wims.cgi not found.
 echo
 exit
fi

conffile=$confdir/httpd.conf
tmpfile=$confdir/tmp.conf
wimshome=`pwd`

conftest=`grep -i manually_configured_for_wims $conffile`
if [ ! -z "$conftest" ]; then
 echo Manually configured httpd.conf not modified.
 exit
fi

# The next two definitions should NEVER be changed.
startl="WIMS definitions start. Do not alter this line"
  endl="WIMS definitions end. Do not alter this line"

awk 'BEGIN {a=1}; 
	/'"$startl"'/ {a=0};
	a==1 {print};
	/'"$endl"'/ {a=1};' $conffile >$tmpfile

if [ "$1" = "uninstall" ]; then
 mv $tmpfile $conffile
 killall -HUP $httpdname
 exit
fi

cat >>$tmpfile <<@
###### $startl! ######
#
# If you manually add things concerning WIMS into this configuration file,
# add them BEFORE the 'WIMS definition start' line.
# Otherwise the addition will be erased in next update.
#
  AddHandler cgi-script .cgi
@

vtest=`echo $vtest | grep 'pache/1\.2'`
if [ ! -z "$vtest" ]; then
 cat >>$tmpfile <<@
  ScriptAlias /wims/wims.cgi $wimshome/public_html/wims.cgi
  ScriptAlias /wims/wims.html $wimshome/public_html/wims.cgi
  ScriptAlias /wims/index.html $wimshome/public_html/wims.cgi
  ScriptAlias /wims/wims.gif $wimshome/public_html/wims.cgi
  ScriptAlias /wims/wims.png $wimshome/public_html/wims.cgi
  ScriptAlias /wims/wims.jpeg $wimshome/public_html/wims.cgi
  ScriptAlias /wims/wims.jpg $wimshome/public_html/wims.cgi
@
else	# newer versions of Apache
 cat >>$tmpfile <<@
  ScriptAliasMatch ^/~wims/wims\.(.*) $wimshome/public_html/wims.cgi
  ScriptAliasMatch ^/wims/wims\.(.*) $wimshome/public_html/wims.cgi
  ScriptAliasMatch ^/~wims/index\.(.*) $wimshome/public_html/wims.cgi
  ScriptAliasMatch ^/wims/index\.(.*) $wimshome/public_html/wims.cgi
  ScriptAliasMatch ^/~wims/.._(.*).html $wimshome/public_html/wims.cgi
  ScriptAliasMatch ^/wims/.._(.*).html $wimshome/public_html/wims.cgi
  ScriptAliasMatch ^/~wims/getfile/(.*) $wimshome/public_html/wims.cgi
  ScriptAliasMatch ^/wims/getfile/(.*) $wimshome/public_html/wims.cgi
  ScriptAlias /wims/....\.cgi $wimshome/public_html/wims.cgi
@
fi
cat >>$tmpfile <<@
  Alias /wims $wimshome/public_html
  Alias /~wims $wimshome/public_html

  <Directory $wimshome/public_html>
  Options FollowSymLinks ExecCGI -Indexes
  AllowOverride All
  </Directory>
  <Directory $wimshome/public_html/modules>
  Options FollowSymLinks
  AllowOverride Limit
  </Directory>

###### $endl! ######
@

mv $tmpfile $conffile
if [ -f /etc/rc.d/init.d/httpd ]; then
 /etc/rc.d/init.d/httpd restart
else
 echo
 echo Now you must restart your httpd to make the new configuration
 echo take effect.
 echo If you don\'t know how to do so: just shut down the system then
 echo restart it.
 echo
fi

