#!/bin/sh
##
##  trac-setup.sh -- Trac Environment Setup Utility
##

#   command line argument sanity check
prg="$0"
if [ $# -eq 0 ]; then
    echo "$prg:ERROR: invalid command line" 1>&2
    echo "$prg:USAGE: $prg create <short-name> <long-name> <username> <password>" 1>&2
    echo "$prg:USAGE: $prg destory <short-name>" 1>&2
    exit 1
fi

#   dispatch operation
cmd="$1"
shift
case "$cmd" in
    create )
        #   create new project environment
        name_short="$1"
        name_long="$2"
        admin_user="$3"
        admin_pass="$4"
        trac_admin () {
            @l_prefix@/bin/trac-admin @l_prefix@/var/trac/env/$name_short "$@"
        }
        trac_admin initenv "$name_long" 'sqlite:db/trac.db' >/dev/null
        trac_admin permission add $admin_user TRAC_ADMIN >/dev/null
        trac_admin config set logging log_type file >/dev/null
        trac_admin config set header_logo alt "$name_long" >/dev/null
        trac_admin config set inherit plugins_dir   @l_prefix@/share/trac/plugins   >/dev/null
        trac_admin config set inherit templates_dir @l_prefix@/share/trac/templates >/dev/null
        @l_prefix@/sbin/htdigest -c \
            @l_prefix@/var/trac/env/$name_short/.htdigest \
            $name_short "$admin_user" "$admin_pass"
        chown -R @l_rusr@:@l_rgrp@ @l_prefix@/var/trac/env/$name_short >/dev/null 2>&1 || true
        ;;
    destroy )
        #   destroy existing project environment
        rm -rf @l_prefix@/var/trac/env/$name_short >/dev/null || true
        ;;
esac

