#!/bin/bash -e
#Copyright (c) 2009 Alon Swartz <alon@turnkeylinux.org> - all rights reserved

fatal() {
    echo "fatal: $@" 1>&2
    exit 1
}

init_trac_project() {
    echo "initializing trac project: $VC"

    trac-admin $PROJ_LIB initenv "$PROJ_NAME" sqlite:db/trac.db $VC $PROJ_REPO
    trac-admin $PROJ_LIB permission add $ADMIN_USER TRAC_ADMIN
    trac-admin $PROJ_LIB wiki import WikiStart $TRAC_ETC/WikiStart.Template

    mv $PROJ_LIB/conf/trac.ini $INI
    ln -s $INI $PROJ_LIB/conf/trac.ini
    chown www-data:www-data $INI

    trac-admin $PROJ_LIB config set header_logo alt trac_logo
    trac-admin $PROJ_LIB config set header_logo height 73
    trac-admin $PROJ_LIB config set header_logo link "$PROJ"
    trac-admin $PROJ_LIB config set header_logo width 236
    trac-admin $PROJ_LIB config set header_logo src "site/logo.png"
    trac-admin $PROJ_LIB config set project icon "images/$VC.png"
    trac-admin $PROJ_LIB config set project descr "$PROJ_DESC"
    trac-admin $PROJ_LIB config set notification smtp_enabled true
    trac-admin $PROJ_LIB config set notification smtp_replyto root@localhost

    rmdir $PROJ_LIB/htdocs
    ln -s $TRAC_SHARE/htdocs/site $PROJ_LIB/htdocs

    rmdir $PROJ_LIB/plugins
    ln -s $TRAC_SHARE/plugins $PROJ_LIB/plugins

    chown -R www-data:www-data $TRAC_LIB
}

if [ $# -ne "2" ]; then
    echo "Syntax:  $0 git|svn|hg NAME"
    echo "Example: $0 git foobar"
    echo 
    echo "Environment variables:"
    echo "    GIT_PUBLIC_WRITE    enabled public write access (default: yes)"
    exit 1
fi

VC=$1
NAME=$2

ADMIN_USER=admin

TRAC_ETC=/etc/trac
TRAC_LIB=/var/local/lib/trac
TRAC_SHARE=/usr/local/share/trac

PROJ=$VC-$NAME
PROJ_LIB=$TRAC_LIB/$PROJ
PROJ_REPO=/srv/repos/$VC/$NAME
PROJ_NAME="$NAME"
PROJ_DESC="$VC $NAME project"
INI=$TRAC_ETC/$PROJ.ini

[ -e $INI ] && fatal "project $PROJ already exists"

set ${GIT_PUBLIC_WRITE:=yes}

case "$VC" in
    git)
        # initialize empty repository
        mkdir -p $PROJ_REPO
        cd $PROJ_REPO
        [ -d ".git" ] || git init

        touch $PROJ_REPO/.git/git-daemon-export-ok
        ln -s $PROJ_REPO/.git /var/lib/git/$NAME.git
        echo $name > $PROJ_REPO/.git/description

        # allow public git-push to repo
        if [ "$GIT_PUBLIC_WRITE" == "yes" ]; then
            echo "[daemon]" >> $PROJ_REPO/.git/config
            echo "        uploadpack = true" >> $PROJ_REPO/.git/config
            echo "        uploadarchive = true" >> $PROJ_REPO/.git/config
            echo "        receivepack = true" >> $PROJ_REPO/.git/config
        fi

        # initialize trac project
        init_trac_project
        GIT_REPO=/var/lib/git/$NAME.git
        trac-admin $PROJ_LIB config set trac repository_dir "$GIT_REPO"
        trac-admin $PROJ_LIB config set trac git_bin "/usr/bin/git"
        trac-admin $PROJ_LIB config set components tracopt.versioncontrol.git.* enabled
        ;;
    hg)
        # initialize empty repository
        mkdir -p $PROJ_REPO
        cd $PROJ_REPO
        [ -d ".hg" ] || hg init

        # initialize trac project
        init_trac_project
        trac-admin $PROJ_LIB config set components tracext.hg.* enabled
        ;;
    svn)
        # initialize empty repository
        mkdir -p $PROJ_REPO
        [ -e "$PROJ_REPO/conf/svnserve.conf" ] || svnadmin create $PROJ_REPO
        
        # initialize trac project
        init_trac_project
        trac-admin $PROJ_LIB config set components tracopt.versioncontrol.svn.* enabled
        ;;
    *)
        fatal "unsupported VC: $VC"
        ;;
esac

