#!/bin/sh
#
# info2wwwconfig -- configure info2www
# Lars Wirzenius
# "@(#)dwww:$Id$"
#

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/info2www" "$2/info2www"
	ln -s /usr/lib/info2www "$1/info2www"
	ln -s /usr/lib/info2www/info2www "$2/info2www"
}

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

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

	found_httpd=no
	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 info2www 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/info2wwwconfig"
		echo ""
		echo -n "Press enter to continue: "
		read dummy
	fi
}

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

do_remove() {
	echo "Trying to remove info2www 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 info2www links, nothing removed."
	fi
}


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

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