#!/bin/sh
#
# dwwwconfig -- configure dwww
# Lars Wirzenius
# "@(#)dwww:$Id: dwwwconfig,v 1.1 1997/03/21 18:10:01 jim Exp $"
#

set -e

ask() {
	echo "Enter HTTP server's document root directory:"
	read docroot
	echo "Enter HTTP server's cgi-bin directory:"
	read cgidir
}

try() {
	$1 Cern /usr/sbin/cern-httpd /home/httpd-data /usr/lib/cern-httpd/cgi-bin
	$1 Apache /usr/sbin/apache-httpd /var/web/webspace /var/web/cgi-bin
	$1 Apache /usr/sbin/apache /home/www-data/webspace /usr/lib/httpd/cgi-bin
}

do_links() {
	rm -f "$1/dwww" "$2/dwww"
	ln -s /var/lib/dwww/html "$1/dwww"
	ln -s /usr/lib/dwww/dwww.cgi "$2/dwww"
}

try_install() {
	if [ -x "$2" -a -d "$3/." -a -d "$4/." ]
	then
		echo "Found $1, linking dwww to $3 and $4"
		do_links "$3" "$4"
		found_httpd=yes
	fi
}

search_apache() {
	if [ -x /usr/sbin/apache -o -x /usr/sbin/apache-httpd ]; then
	fi
}

do_install() {
	echo "Trying to install dwww links to HTTP server directories"

	found_httpd=no
        dwww_html_dir=""
        dwww_cgi_dir=""
        dwww_cgi_user=""
#	try try_install
	
	if [ "$interactive" = yes ]
	then
		ask
		try_install Unknown /bin/sh "$docroot" "$cgidir"
	fi
	
	if [ "$found_httpd" = no ]
	then
		echo ""
		echo "No HTTP server directories found, no dwww links installed."
		echo "This may be because you have configured your server to use"
		echo "non-default locations, or because the default locations have"
		echo "changed after this configuration script was last updated."
		echo "If the latter, please report it is as a bug using the Debian"
		echo "bug reporting system. If the former, you need to run this"
		echo "script by hand using the following command (as root):"
		echo ""
		echo "    /usr/sbin/dwwwconfig"
		echo ""
		echo -n "Press enter to continue: "
		read dummy
	fi
}

try_remove() {
	if [ -x "$2" -a -L "$3/dwww" -a -L "$4/dwww" ]
	then
		echo "Found $1, removing dwww from $3 and $4"
		rm -f "$3/dwww" "$4/dwww"
		found_httpd=yes
	fi
}

do_remove() {
	echo "Trying to remove dwww links from HTTP server directories"
	found_httpd=no
	try try_remove
	if [ "$interactive" = yes ]
	then
		ask
		try_remove Unknown /bin/sh "$docroot" "$cgidir"
	fi
	
	if [ "$found_httpd" = no ]
	then
		echo "Did not find any dwww links, nothing removed."
	fi
}


case "$1" in
	--auto) interactive=no; shift ;;
	*) interactive=yes ;;
esac

case "$1" in
	--remove) do_remove ;;
	*) do_install ;;
esac
