#!/bin/sh

PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin

OURCVSCONF_VERSION="1"

#TMPFILE=/tmp/$$.cvsconfig-tmp
#rm -f $TMPFILE
#:>$TMPFILE
#chmod 600 $TMPFILE

TMPFILE=`tempfile -m 600`

# Check the situation with /etc/cvs.conf.dpkg-dist
if test -f /etc/cvs.conf.dpkg-dist; then
	if test -f /etc/cvs.conf.dpkg-old; then
		savelog -l /etc/cvs.conf.dpkg-old
	fi
	mv -f /etc/cvs.conf /etc/cvs.conf.dpkg-old 
	mv -f /etc/cvs.conf.dpkg-dist /etc/cvs.conf
	echo "You have a newer version of /etc/cvs.conf in /etc/cvs.conf.dpkg-dist."
	echo "Your old configuration file has been backed up, and the new one"
	echo "will be used in place."
fi

if test ! -f /etc/cvs.conf; then
	echo "You have no current CVS configuration file (/etc/cvs.conf)"
	echo "I will create a new one with appropriate defaults for you."
	cat <<EOF >/etc/cvs.conf
#
# Configuration file for the Debian CVS-related scripts
#

# Please do not touch this - it enables the postinst scripts detection
# of whether the "cvsconfig" script needs to be run or not.
CVSCONF_VERSION="none"

# 1. Where are your repositories?
CVS_REPOS=""

# 2. Do you want history files rotated?
# 2a. If, so, only those in different dirs, or in all the dirs above?

# Set CVS_ROTATE to a colon-separated list of the rotations to keep
# (the default is seven).
CVS_ROTATE=""
CVS_ROTDIRS="\$CVS_REPOS"

# The CVSROOT directories pserver is allowed to read from as standard
CVS_ENAB_PSERV=""
CVS_PSERV_REPOS="\$CVS_REPOS"

# End of file.
EOF
else
	. /etc/cvs.conf
fi

if test -f /etc/cvs.conf.dpkg-old; then
	echo "Loading your old configuration defaults..."
	. /etc/cvs.conf.dpkg-old
	sed -ne 's\([^=]\+\)="\([^"]*\)"\(.*\)\1 \2 \3gp' \
		/etc/cvs.conf.dpkg-old > $TMPFILE
	SEDCMD=""
	while read LINE; do
		set -- $LINE
		SEDCMD="$SEDCMD -e 's\($1=\"\)[^\"]*\(\".*\)\1$2\2$3g'"
	done < $TMPFILE
	eval sed $SEDCMD /etc/cvs.conf >$TMPFILE
fi

do_questions ()
{
	cat <<EOF

Debian CVS configuration

This script will allow you to configure the CVS package under Debian to
work for you.

You may run this configuration program again at any time by running the
program /usr/sbin/cvsconfig. You may also run this script as cvsconfig -u
to update the configuration files (eg /etc/inetd.conf) only, after you have
edited /etc/cvs.conf.

EOF
	# Search for repositories in some standard places
	for i in /SourceControl /var/cvs /usr/local/cvsroot; do
		if echo "$CVS_REPOS" | grep -q $i; then
			continue
		fi
		if test -f $i/CVSROOT/modules,v; then
			if test "$CVS_REPOS" = ""; then
				CVS_REPOS="$i"
			else
				CVS_REPOS="$CVS_REPOS:$i"
			fi
		fi
	done
	
	cat <<EOF
1. Where are your repositories?

The names of these repositories can be used to be exported by the pserver,
to have their history files rotated automatically every week, and for general
repository security checks.

EOF
	
	if test -n "$CVS_REPOS"; then
		echo "Detected in $CVS_REPOS"
		echo
		echo "Enter your repositories, separated by colons. The character '&' will"
		echo "be substituted with the default. To insert a real '&' character, use"
		echo "'\&'. To insert a backslash, use '\\'. Just press enter to use"
		echo "the default."
		echo -n "> "
	else
		echo "Enter your repositories, separated by colons:"
		echo -n "> "
	fi
	
	read CVS_NREPOS
	
	if test "$CVS_NREPOS" != ""; then
		CVS_REPOS="`echo "$CVS_NREPOS" | sed -e 's\(^\|[^\\]\)&'"$CVS_REPOS"'g' \
			-e 's:\\\\&:\\&:g' -e 's:\\\\\\\\:\\\\:g'`"
	fi
	
	echo
	echo -n "Checking the repositories: "
	OLDIFS="$IFS"
	IFS=": 	"
	for i in $CVS_REPOS; do
		if test ! -f $i/CVSROOT/modules,v; then
			echo -n "$i (Warning: may not be a CVS repository) "
		else
			echo -n "$i "
		fi
	done
	IFS="$OLDIFS"
	echo "- OK."
	
	cat <<EOF

2. Do you want the history files in these repositories rotated weekly?

This option is primarily useful for servers with a lot of activity. The script
/etc/cron.weekly/cvs will rotate the "history" files in the repositories you
select. For more customization, edit the file /etc/cvs.conf.

You may specify "yes", "no", or a list of numbers of history files to keep
(default 7) per repository specified above (or 'n' for no rotation), for
example:-

"yes", or
"7:7:3:4:n"

Note: if the first value should be 'n', enter a colon first, as in
":n:7:5:6". The colon will be removed by the script automatically.

EOF
	
	echo -n "> "
	read CVS_YN
	
	case "$CVS_YN" in
		[yY]*)
		CVS_ROTATE="yes"
		;;
		
		[nN]*)
		CVS_ROTATE="no"
		;;
		
		"")
		if test "$CVS_ROTATE" = ""; then
			CVS_ROTATE="no"
		else
			echo "Keeping default of '$CVS_ROTATE'."
		fi
		;;
		
		[0-9:]*)
		echo "Using advanced syntax."
		if test "`echo $CVS_YN | cut -b1`" = ":"; then
			CVS_ROTATE="`echo $CVS_YN | cur -b2-`"
		else
			CVS_ROTATE="$CVS_YN"
		fi
		;;
		
		*)
		echo "Warning: Couldn't understand your reponse '$CVS_YN'."
		echo "Not rotating - rerun this script, or edit /etc/cvs.conf to"
		echo "change this."
		CVS_ROTATE=""
		;;
	esac
	
	echo
	if test "$CVS_ROTATE" = "yes"; then
		echo "Will rotate history files normally."
	elif test "$CVS_ROTATE" = "no"; then
		echo "Not rotating history files."
	else
		echo "Rotating using advanced syntax format '$CVS_ROTATE'."
	fi
	
	if test -x /usr/sbin/update-inetd; then
		cat <<EOF

3. CVS pserver configuration

CVS pserver is a client-to-server mechanism which can be used by CVS as
a replacement for the standard "server" method, which uses "rsh", or an
rsh compatible program, such as ssh. It is more efficient than the standard
server protocol, also supporting its own password files, making it more
secure. However, it may be a security risk, and used to contain a security
problem whereby a remote connection may have been able to read the passwd
or other security-related files on the system.

For this reason, it is recommended that you switch this option OFF, unless
you know that you want it specifically. CVS now only allows access to
particular repositories specified on the command line. To change the
repositories it has permissions to from the list of repositories given
above, edit the file /etc/cvs.conf manually.

CVS pserver will be installed in inetd, using tcpd wrappers if you answer
"yes" to this question. The default (just pressing enter) will be your
previous value, or, if you have not configured CVS before, will be set
to "OFF".

EOF
		
		echo -n "Do you want pserver enabled? "
		
		read CVS_YN
		
		echo
		case "$CVS_YN" in
			[yY]*)
			CVS_ENAB_PSERV="yes"
			;;
			
			[nN]*)
			CVS_ENAB_PSERV="no"
			;;
			
			*)
			if test "$CVS_ENAB_PSERV" = ""; then
				echo "Defaulting to OFF."
				CVS_ENAB_PSERV="no"
			else
				echo "Defaulting to old value '$CVS_ENAB_PSERV'."
			fi
			;;
		esac
	fi
}

if test "$1" = "-u"; then
	if test "$CVSCONF_VERSION" != "$OURCVSCONF_VERSION"; then
		do_questions
	fi
else
	do_questions
fi

sed -e 's:\(CVSCONF_VERSION=\"\)[^\"]*\(\".*\):\1'"${OURCVSCONF_VERSION}"'\2:g' \
	-e 's\(CVS_REPOS=\"\)[^\"]*\(\".*\)\1'"${CVS_REPOS}"'\2g' \
	-e 's\(CVS_ROTATE=\"\)[^\"]*\(\".*\)\1'"${CVS_ROTATE}"'\2g' \
	-e 's\(CVS_ENAB_PSERV=\"\)[^\"]*\(\".*\)\1'"${CVS_ENAB_PSERV}"'\2g' \
	/etc/cvs.conf >> $TMPFILE

if ! cmp -s $TMPFILE /etc/cvs.conf; then
	cp $TMPFILE /etc/cvs.conf
fi

rm -f $TMPFILE

# Set up the inetd pserver stuff
. /etc/cvs.conf

if test -x /usr/sbin/update-inetd; then
	if test "$CVS_ENAB_PSERV" = "yes"; then
		/usr/sbin/update-inetd --remove "cvspserver.*"
		ALLOWROOT=""
		OLDIFS="$IFS"
		IFS=": 	"
		for i in $CVS_PSERV_REPOS; do
			ALLOWROOT="$ALLOWROOT --allow-root=$i"
		done
		IFS="$OLDIFS"
		/usr/sbin/update-inetd --group OTHER --add \
	"cvspserver	stream	tcp	nowait	root	/usr/sbin/tcpd	/usr/bin/cvs -b /usr/bin${ALLOWROOT} pserver"
	else
		/usr/sbin/update-inetd --disable cvspserver
	fi
fi

if test -f /etc/cvs.conf.dpkg-old; then
	echo "Your configuration file has been updated successfully."
	echo "Do you wish to remove the backup dpkg created of your old"
	echo "file, /etc/cvs.conf.dpkg-old?"
	echo -n "> "
	read CVS_YN
	case "$CVS_YN" in
		[Yy]*)
		echo "OK, removing it..."
		rm -f /etc/cvs.conf.dpkg-old
		;;
		
		*)
		echo "Keeping it."
		;;
	esac
fi

# End of file.
