#!/bin/bash

# Copyright (c) 2007 Torbjörn Svensson <azoff@se.linux.org>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

PROJECT=$1
if [ -z $PROJECT ]; then
	echo -n "Type a name of the project: "
	read PROJECT
fi
BASEDIR=/home/projects

URI_TRAC=/trac/$PROJECT
URI_SVN=/svn/$PROJECT

SERVER_NAME=$(hostname --fqdn)

FOLDER_SVN=$BASEDIR/$PROJECT/svn
FOLDER_TRAC=$BASEDIR/$PROJECT/trac

FILE_PASSWD=$BASEDIR/$PROJECT/.htpasswd
FILE_AUTHZ=$BASEDIR/$PROJECT/.authz
FILE_APACHE=$BASEDIR/$PROJECT/apache.conf
FILE_GLOBAL_APACHE=/etc/apace2/conf.d/my_trac.conf

BIN_SVNADMIN=svnadmin
BIN_TRACADMIN=trac-admin
BIN_HTPASSWD=htpasswd2

echo "Creating file structure"
mkdir -p $FOLDER_SVN $FOLDER_TRAC
echo
echo

echo "Creating subversion data"
$BIN_SVNADMIN create $FOLDER_SVN
echo
echo

echo "Creating trac data"
$BIN_TRACADMIN $FOLDER_TRAC initenv
echo
echo

echo "Fixing permissions"
chown -R wwwrun $FOLDER_TRAC/db/
echo
echo

echo "
# trac
<Location $URI_TRAC>
	SetHandler mod_python
	PythonHandler trac.web.modpython_frontend
	PythonOption TracEnv $FOLDER_TRAC
	PythonOption TracUriRoot $URI_TRAC

	# auth
	AuthType Basic
	AuthName \"$PROJECT\"
	AuthUserFile $FILE_PASSWD
	Require valid-user
</Location>

# svn
<Location $URI_SVN>
	DAV svn

	# Set this to the path to your repository
	SVNPath $FOLDER_SVN

	# auth
	AuthType Basic
	AuthName \"$PROJECT\"
	AuthUserFile $FILE_PASSWD
	Require valid-user
	
	# Uncomment the following line to enable Authz Authentication
	# AuthzSVNAccessFile $FILE_AUTHZ
</Location>
" > $FILE_APACHE

echo "
Type in username:password to use with subversion and trac,
one entry on each row. End input with EOF on a single row.

Example:
jondoe:this-is-my-secret-password
EOF

"
users=""
while true; do
	# read the line
	read row

	# break if EOF (last line)
	if [ $row == "EOF" ]; then break; fi
	
	# did we get line in user:pass format?
	echo $row | grep -q ':' || {
		echo "Error, unable to find : in \"$row\", ignoring";
		continue;
	}

	user=$(echo $row | cut -d':' -f1)
	pass=$(echo $row | cut -d':' -f2)
	users="$user, $users"
	$BIN_HTPASSWD -nb $user $pass >> $FILE_PASSWD
done
echo -e "Added: $users\b\b"
echo

echo "Adding $FILE_APACHE to apache"
echo "Include $FILE_APACHE" >> $FILE_GLOBAL_APACHE
/etc/init.d/apache2 reload
echo

echo "All setup, please point your browser to http://$SERVER_NAME$URI_TRAC"
echo "and start using this project."
echo "Subversion is located on http://$SERVER_NAME$URI_SVN"

echo
echo "To add another user to this project, use this command: "
echo "# $BIN_HTPASSWD -n <username> >> $FILE_PASSWD"
echo 
echo
echo "To make users able to use samba, you need to add them there aswell."
echo "# smbpasswd -a <username>"
echo
echo "NOTE that the user needs to be added to the system before you run"
echo "this command. To add a user, use the GUIs available in openSuse."
echo

exit 0


