#!/bin/sh
#
#	aegis - project change supervisor
#	Copyright (C) 2002 Peter Miller;
#	All rights reserved.
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
#
# MANIFEST: script to install aegis.cgi in web server's cgi-bin directory
#
bindir=@bindir@

#
# read command line arguments
#
symlink=no
quiet=no
while test $# -gt 0
do
	case "$1" in
	-s)
		symlink=yes
		;;
	-q)
		quiet=yes
		;;
	*)
		echo "Usage: $0 [ -s ][ -q ]" 1>&2
		exit 1
		;;
	esac
	shift
done

#
# Look for the httpd config files, so that we can find where to
# put the CGI script, and other HTML files.
#
_cv_web=no
_cv_httpd=no
for ac_dir in \
	/etc/apache \
	/etc/httpd \
	/opt/apache \
	/opt/httpd \
	/usr/apache \
	/usr/httpd \
	/usr/local/apache \
	/usr/local/apache/etc \
	/usr/local/etc/apache \
	/usr/local/etc/httpd \
	/usr/local/httpd \
	/usr/local/httpd/etc \
	/var/apache \
	/var/httpd \
	/var/lib/apache \
	/var/lib/httpd
do
	if test -r $ac_dir/conf/httpd.conf -o -r $ac_dir/httpd.conf
	then
		_cv_web=yes
		_cv_httpd=$ac_dir
		break
	fi
done

if test "$_cv_web" != "yes"
then
	echo "Web server configuration file not found." 1>&2
	exit 1
fi

_cv_ScriptAlias=no
for f in $_cv_httpd/conf/srm.conf $_cv_httpd/conf/httpd.conf \
	 $_cv_httpd/srm.conf $_cv_httpd/httpd.conf
do
	if test -r $f
	then
		tmp=`awk '/^[ 	]*ScriptAlias.*cgi-bin/{ \
		    dir=$3;				\
		    sub(/"/, "", dir);			\
		    sub(/"/, "", dir);			\
		    sub(/\/*$/, "", dir);		\
		    print dir				\
		    }' $f | head -1`
		test "x$tmp" != "x" && _cv_ScriptAlias="$tmp"
	fi
done

if test "$_cv_ScriptAlias" = "no"
then
	echo "No ScriptAlias found in $_cv_httpd/conf/httpd.conf" 2>&1
	exit 1
fi

#
# Remove thatever is there at the moment.
#
test "$quiet" = "no" && echo rm $_cv_ScriptAlias/aegis.cgi
rm -f $_cv_ScriptAlias/aegis.cgi

if test "$symlink" = "yes"
then
	#
	# Put a symbolic link from the cgi-bin directory to aegis.cgi.
	#
	test "$quiet" = "no" && \
	echo ln -s $bindir/aegis.cgi $_cv_ScriptAlias/aegis.cgi
	ln -s $bindir/aegis.cgi $_cv_ScriptAlias/aegis.cgi
	test "$?" != 0 && exit 1
else
	#
	# Copy aegis.cgi into the cgi-bin directory.
	#
	test "$quiet" = "no" && \
	echo cp $bindir/aegis.cgi $_cv_ScriptAlias/aegis.cgi
	cp $bindir/aegis.cgi $_cv_ScriptAlias/aegis.cgi
	test "$?" != 0 && exit 1
fi

#
# Report success.
#
test "$quiet" = "no" && echo "Done."
exit 0
