#!/bin/bash
set -e

echo -n "where shall the repository be created? (.) "
read REPOSDIR
if [ -z "$REPOSDIR" ] ; then
	REPOSDIR="."
fi

if [ -e "$REPOSDIR" -a ! -d "$REPOSDIR" ] ; then
	echo "\"$REPOSDIR\" exists and is not a directory, aborting" >&2
	exit 1
fi
if [ -e "$REPOSDIR/README.txt" ] && grep -q "http://subversion.apache.org/" "$REPOSDIR/README.txt" ; then
	echo -ne "\"$REPOSDIR\" seems to already contain a subversion repository\nremove directory contents? (y/n) "
	read REMOVE
	while [ "$REMOVE" != "y" -a \
		"$REMOVE" != "Y" -a \
		"$REMOVE" != "n" -a \
		"$REMOVE" != "N" ] ; do
		echo -n "please type \"y\" or \"n\" "
		read REMOVE
	done
	if [ "$REMOVE" != "y" -a "$REMOVE" != "Y" ] ; then
		echo "aborting" >&2
		exit 1
	else
		if [ "$REPOSDIR" = "." ] ; then
			rm -rf *
		else
			rm -rf "$REPOSDIR"
		fi
	fi
fi

svnadmin create "$REPOSDIR"
if [ "${REPOSDIR:0:1}" = "/" ] ; then
	REPOSURL="file://$REPOSDIR"
else
	REPOSURL="file://`pwd`/$REPOSDIR"
fi
svn mkdir "$REPOSURL/trunk" "$REPOSURL/branches" "$REPOSURL/tags" -m "created directories 'trunk', 'branches', 'tags'"
