#!/bin/sh -e

unset HOME TMPDIR ||:

if [ -z "$CVS_CONF" ]; then
	if [ -f /etc/cvs/pserver.conf ]; then
		CVS_CONF=/etc/cvs/pserver.conf
	elif [ -f /etc/cvs/cvs.conf ]; then
		CVS_CONF=/etc/cvs/cvs.conf
	fi
fi

if [ -z "$CVS_CONF" ]; then
	echo "No CVS pserver config file defined." >&2
	exit 1
fi

if [ ! -r "$CVS_CONF" ]; then
	echo "CVS pserver config file '$CVS_CONF' not found." >&2
	exit 1
fi

. $CVS_CONF

unset roots ||:

OLDIFS="$IFS"
IFS=":$IFS"
for d in $CVS_REPOS; do
	IFS="$OLDIFS"
	if [ -d "$d/CVSROOT" ]; then
		roots="$roots --allow-root=$d"
	fi
done
IFS="$OLDIFS"

unset tmps ||:
[ -z "$CVS_TMPDIR" ] || tmps="-T $CVS_TMPDIR"

unset CVS_REPOS CVS_TMPDIR ||:

if [ -n "$roots" ]; then
	exec /usr/bin/cvs -f $tmps $roots pserver
else
	echo "Repository not configured in $CVS_CONF" >&2
	exit 2
fi
