#! /bin/sh
# postinst script for websvn

set -e

conffile="/etc/websvn/svn_deb_conf.inc"

case "$1" in
	configure)
		. /usr/share/debconf/confmodule || exit
		db_version 2.0
		db_title WebSVN

		db_get "websvn/configuration" || true
		if [ "$RET" = "true" ]; then


			# install/update config file
			tmpfile=`mktemp /tmp/websvn.inc.XXXXXX`

			# create new file
			echo "<?php" > $tmpfile
			echo "// please edit /etc/websvn/config.php " >> $tmpfile
			echo "// or use dpkg-reconfigure websvn" >> $tmpfile

			db_get "websvn/parentpath" || true
			repositories="$RET"
			if [ "x$repositories" != "x" ]; then
			  echo $repositories | sed -e 's/,/\n/g' | \
			  while read dirname; do
			    echo "\$config->parentPath(\"$dirname\");" >> $tmpfile
			  done
			fi

			db_get "websvn/repositories" || true
			i=1
			repositories="$RET"
			if [ "x$repositories" != "x" ]; then
			  echo $repositories | sed -e 's/,/\n/g' | \
			  while read dirname; do
			    echo "\$config->addRepository(\"repos $i\", \"file://$dirname\");" >> $tmpfile
			    i=$(($i + 1))
			  done
			fi

			searchpath="/usr/bin /bin /usr/local/bin"
			has_enscript=no
			has_sed=no

			# auto-detect enscript
			if ! grep -s -q '^$config->setEnscriptPath' /etc/websvn/config.inc ; then
				for path in $searchpath; do
					if [ -x $path/enscript ]; then
						echo "\$config->setEnscriptPath(\"$path\");" >> $tmpfile
						has_enscript=yes
						break
					fi
				done
			fi

			# auto-detect sed
			if ! grep -s -q '^$config->setSedPath' /etc/websvn/config.inc ; then
				for path in $searchpath; do
					if [ -x $path/sed ]; then
						echo "\$config->setSedPath(\"$path\");" >> $tmpfile
						has_sed=yes
						break
					fi
				done
			fi

			if [ "$has_enscript" = "yes" ] && [ "$has_sed" = "yes" ]; then
				echo '$config->useEnscript();' >>$tmpfile
			fi

			# end of file config
			echo "?>" >> $tmpfile

			# fix permissions
			chmod 640 $tmpfile
			chown root:www-data $tmpfile

			# merge config
			# because of debconf, make ucf read from /dev/tty
			#ucf --three-way $tmpfile $conffile < /dev/tty
			ucf --debconf-ok --three-way $tmpfile $conffile

			# fix permissions
			chmod g+r $conffile
			chgrp www-data $conffile

			rm -f $tmpfile

			# webservers config
			db_get "websvn/webservers"
			webservers="$RET"
			restart=""

			# Currently only apache is supported
			for webserver in $webservers ; do
				webserver=${webserver%,}
				test -x /usr/sbin/$webserver || continue
				case "$webserver" in
				apache|apache-perl|apache-ssl|apache2)
					if [ ! -f /etc/$webserver/conf.d/websvn ] && [ ! -h /etc/$webserver/conf.d/websvn ]; then
						ln -s /etc/websvn/apache.conf /etc/$webserver/conf.d/websvn
						restart="$restart $webserver"
					fi
				;;
				esac
			done
			for webserver in $restart; do
				webserver=${webserver%,}
				if [ -x /usr/sbin/invoke-rc.d ]; then
                			invoke-rc.d $webserver restart
				else
					/etc/init.d/$webserver restart
				fi
			done

			db_stop

		fi

	;;
	abort-upgrade|abort-remove|abort-deconfigure)
	;;
	*)
		echo "postinst called with unknown argument \`$1'" >&2
		exit 1
	;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0


