#! /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 [ ! "$(id -ru )" = 0 ]; 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

distro=default
[ -f /etc/debian_version ] && distro=debian

PATH=$PATH:/usr/sbin:/sbin
export PATH
# httpd or apache?
httpd -v >/dev/null && httpdname=httpd
if [ -z "$httpdname" ]; then apache -v >/dev/null && httpdname=apache; fi
if [ -z "$httpdname" ]; then apache2 -v >/dev/null && httpdname=apache2; fi
if [ -z "$httpdname" ]; then httpd2 -v >/dev/null && httpdname=httpd2; fi
if [ -z "$httpdname" ]; then
 cat <<@

Apache is not found on your system.
WIMS requires a running web server Apache.
Please first install Apache (>=1.3).

@
 exit
fi
conffile=`$httpdname -V | grep SERVER_CONFIG_FILE | awk -F'=' '{print $2}' \
 | tr -d ' "'`
confdir=`dirname $conffile`
if [ -z "$confdir" ]; then
 cat <<@

Unknown web server. Version too early?
Please first update your web server to Apache >=1.3.

@
 exit
fi

case $distro in
 debian) conffile=$confdir/conf.d/wims.conf
 
 ;;
esac

cd `dirname $0`/..
wimshome=`pwd`

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

httpdroot=`$httpdname -V | grep HTTPD_ROOT | awk -F'=' '{print $2}' \
 | tr -d ' "'`
if [ ! -z "$httpdroot" ]; then cd $httpdroot; fi

tmpfile=/tmp/wims-apache.conf
vtest=`$httpdname -v | grep Apache`
if [ -z "$vtest" ]; then
 echo
 echo You are not running Apache web server!
 echo
 echo You must configure manually your web server, this script cannot be used.
 echo
 exit
fi

conftest=`grep -i manually_configured_for_wims $conffile`
if [ ! -z "$conftest" ]; then
 echo Manually configured $conffile 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"

case $distro in
 debian) rm -f $tmpfile
 ;;
 *) awk 'BEGIN {a=1}; 
	/'"$startl"'/ {a=0};
	a==1 {print};
	/'"$endl"'/ {a=1};' $conffile >$tmpfile
 ;;
esac

if [ "$1" = "uninstall" ]; then
 case $distro in
  debian) rm -f $conffile $tmpfile
  ;;
  *) mv $tmpfile $conffile
  ;;
 esac
 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
  allow from all
  </Directory>
  <Directory $wimshome/public_html/modules>
  Options FollowSymLinks
  AllowOverride Limit
  </Directory>

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

mv $tmpfile $conffile

initdir=/etc/init.d
[ -d $initdir ] || initdir=/etc/rc.d/init.d
initscript=$initdir/httpd
[ -f $initscript ] || initscript=$initdir/apache
if [ -f $initscript ]; then
 $initscript 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

