#!/bin/bash
#  trac.in - Trac support

set -e

lib_load 'sys/tool/virtualenv'

######
# Settings

net_tool_trac_config_init() {
	lib_setting_vars trac_host trac_root trac_admin
	lib_setting_vars --null trac_group

	lib_setting_vars -ro trac_owner trac_db

	lib_setting_vars -ro trac_wsgi_script
	trac_wsgi_script="wsgi/trac.wsgi"

	lib_setting_arrays -ro trac_site_dirs
	trac_site_dirs=( conf eggs logs data wsgi )
}

net_tool_trac_config_check() {
	export PYTHON_EGG_CACHE="$trac_root/eggs"
}


######
# Native dependencies

# FIXME: need wsgi deps
trac_server_packages=( virtualenv libapache2-mod-python )


######
# Admin

trac_admin() {
	ve_activate "$trac_root"
	run trac-admin $(pwd) "$@"
}


######
# Site

is_site_owner() { [ "$USER" = "$trac_owner" ]; }
is_site_dir() { test -d "${1:-.}/wsgi"; }

trac_site_wsgi() { echo "$trac_root/wsgi/$1.py"; }

trac_site_init() {
	# create and change to site directory
	[ -d "$trac_root" ] || run_mkdir "$trac_root"
	run_pushd "$trac_root"
	run_mkdir "${site_dirs[@]}"

	# create/update virtual machine
	(
		ve_create "$trac_root"
		ve_activate "$trac_root"
		ve_install Babel Trac
	) >logs/init.log 2>&1
	run_popd
}

trac_site_delete() {
	run rm -rfv "$trac_root"
}


######
# Projects


trac_project_dir() { echo "$trac_root/data/$1"; }

trac_project_config_file() {
	local dir
	dir=$(project_dir "$1")
	echo "$dir/conf/trac.ini"
}

trac_project_new() {
	has_args 2 "$@"
	local name=$1
	local desc=$2

	if trac_project_exists "$name"; then
		error "$name: project exists"
	fi

	app_echo "$name: creating project directory"
	run_pushd data
	run mkdir "$name"
	run cd "$name"

	app_echo "$name: running trac-admin to create project"
	local log=$(cmd_tempfile)
	trac_admin initenv "$desc" "$trac_db" 2>&1 | tee "$log"
	run mv "$log" log/init.log

	run_popd
}

trac_project_delete() {
	has_args 1 "$@"
	local name=$1
	trac_project_disable "$name"
	run rm -rf "$(trac_project_dir $name)"
}

trac_project_enable() {
	has_args 1 "$@"
	local name=$1
	local wsgi
	wsgi=$(trac_site_wsgi "$name")
	if [ -L "$wsgi" ]; then
		warn "$name: project already enabled"
	else
		run ln -s "$(basename "$trac_wsgi_script")" "$wsgi"
		info "$name: project enabled"
	fi
}

trac_project_disable() {
	has_args 1 "$@"
	local name=$1
	local wsgi
	wsgi=$(trac_site_wsgi "$name")
	if [ -L "$wsgi" ]; then
		run rm "$wsgi"
		info "$name: project disabled"
	else
		warn "$name: project was not enabled"
	fi
}


######
# CLI

trac_dispatch() {
	is_site_owner || error "must be run by Trac site owner"
	is_site_dir || error "must be run in the trac site directory"
	ve_activate
	cmd_dispatch "$@"
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Thu May 4 19:00:14 PDT 2017 by mcsh i7 v0.19.0.