###############
# General Setup
###############
c = BuildmasterConfig = {}

# Import our passwords
passwd_file = open('passwd.py')
exec passwd_file
passwd_file.close()

# What port to listen on, used for try attempts as well.
c['slavePortnum'] = 9899

c['projectName'] = "Aegisub"
c['projectURL'] = "http://devel.aegisub.org/"
c['buildbotURL'] = "http://buildbot.aegisub.org/"

# Used for accepting wchange notifications (svn-commit-hook) _and_ try attempts.
from buildbot.changes.pb import PBChangeSource
c['change_source'] = PBChangeSource()

####################################
# Check if a file is worth a rebuild
####################################

re_common = [
	'^trunk/aegisub/.*\.c$',
	'^trunk/aegisub/.*\.cpp$',
	'^trunk/aegisub/.*\.h$',
	'^trunk/aegisub/FFmpegSource2/.*',
	'^trunk/aegisub/src/include/aegisub/.*',
	'^trunk/aegisub/src/boost/.*',
	'^trunk/aegisub/src/bitmaps/.*',
	'^trunk/aegisub/src/config/.*',
	'^trunk/aegisub/universalchardet/.*',
	'^trunk/aegisub/src/gl/.*',
]

re_common_unix = [
	'^trunk/aegisub/configure.in$',
	'^trunk/aegisub/libass/.*',
	'^trunk/aegisub/.*Makefile.am$',
	'^trunk/aegisub/m4macros/.*',
	'^trunk/aegisub/scripts/unix-.*',
	'^trunk/aegisub/src/libresrc/.*',
	'^trunk/aegisub/po/.*',
	'^trunk/aegisub/acinclude.m4$',
	'^trunk/aegisub/desktop/.*'
]

re_osx = [
	'^trunk/aegisub/src/libosxutil/.*',
	'^trunk/aegisub/packages/osx_.*',
	'^trunk/aegisub/scripts/osx-.*'
]

re_windows = [
	'^trunk/aegisub/build/.*',
	'^trunk/contrib/csri/.*',
	'^trunk/contrib/lua51/.*',
	'^trunk/contrib/hunspell/.*',
	'^trunk/aegisub/packages/win_installer/.*',
	'^trunk/aegisub/tinderbox/windows/.*',
	'^trunk/aegisub/src/msvc/.*'
]

re_doxygen = [
	'^trunk/aegisub/docs/doxygen/.*',
	'^trunk/aegisub/src/.*\.c$',
	'^trunk/aegisub/src/.*\.cpp$',
	'^trunk/aegisub/src/.*\.h$',
	'^trunk/aegisub/src/libosxutil/.*\.h$',
	'^trunk/aegisub/src/libosxutil/.*\.c$',
	'^trunk/aegisub/src/include/aegisub/.*'
	'^trunk/aegisub/reporter/.*\.c$',
	'^trunk/aegisub/reporter/.*\.cpp$',
	'^trunk/aegisub/reporter/.*\.h$',
]


import re, string
def check_changes(files, re_array):
	re_compiled = re.compile(string.joinfields(re_array, "|"), re.I)
	for changed_file in files:
		if re.match(re_compiled, changed_file):
			return True
		return False

def interested_windows(change):
	return check_changes(change.files, re_common + re_windows)

def interested_unix(change):
	return check_changes(change.files, re_common + re_common_unix)

def interested_osx(change):
	return check_changes(change.files, re_common + re_common_unix + re_osx)

def interested_doxygen(change):
	return check_changes(change.files, re_doxygen)



#############
# BUILDSLAVES
#############

from buildbot.buildslave import BuildSlave
c['slaves'] = [
	BuildSlave("mochi", passwd['mochi'], max_builds=1, properties={'os':'Debian', 'version':'5.0.1', 'osname':'Lenny'}),
	BuildSlave("kokoro", passwd['kokoro'], max_builds=1, properties={'os':'OSX', 'version':'10.5.6', 'osname':'Leopard'}),
	BuildSlave("anpan", passwd['anpan'], max_builds=1, properties={'os':'Windows', 'version':'SP2', 'osname':'Vista'}),
	BuildSlave("tako", passwd['tako'], max_builds=1, properties={'os':'FreeBSD', 'version':'7.1', 'osname':'Stable'}),
	BuildSlave("nori", passwd['nori'], max_builds=1, properties={'os':'Ubuntu', 'version':'9.04', 'osname':'Jaunty'}),
	BuildSlave("mokona", passwd['mokona'], max_builds=1, properties={'os':'OSX', 'version': '10.4.11', 'osname':'Tiger'}),
	BuildSlave("shoyu", passwd['shoyu'], max_builds=2, properties={'os':'Linux', 'version': '???', 'osname':'???'})
]


###########
# FACTORIES
###########

from buildbot.steps import source, shell
from buildbot.steps.shell import Configure, Compile, WithProperties
from buildbot.process import factory
from buildbot import locks
from agi_upload import agi_upload
from OOCompile import OOCompile

# General settings
source_unix = "http://svn.aegisub.org/trunk/aegisub/"
source_windows = "http://svn.aegisub.org/branches/windows"
stub = factory.BuildFactory()


##############
# Factory
# General Unix
##############
flinux = factory.BuildFactory()
flinux.addStep(source.SVN(mode="clobber", svnurl=source_unix))

flinux.addStep(shell.ShellCommand(
	command=["./autogen.sh", "--skip-configure"],
	env={
		'ACLOCAL_FLAGS': '-I /home/verm/build/wx/share/aclocal'
	},
	name = "autogen",
	description = ["autogening"],
	descriptionDone = ["autogen"],
	haltOnFailure=True
))

flinux.addStep(shell.ShellCommand(
	command=["./configure",
		WithProperties("--with-build-credit=BuildBot: %s (%s)", "buildername", "slavename"),
		"--disable-check-wx-stc",
		"--disable-check-wx-opengl",
		"--with-wx-config=/home/verm/build/wx/lib/wx/config/gtk2-unicode-debug-2.9"
	],
	env={
		'CC': '/usr/lib/ccache/gcc',
		'CXX': '/usr/lib/ccache/g++',
		'LDFLAGS': '-Wl,-rpath=/home/verm/build/wx/lib',
		'CCACHE_PATH': '/usr/bin',
		'CCACHE_DIR': '/home/verm/.ccache',
		'LUA_CFLAGS': '-I/usr/include/lua5.1',
		'LUA_LDFLAGS': '-llua5.1'
	},
	name = "configure",
	description = ["configuring"],
	descriptionDone = ["configure"],
	logfiles={
		'config.log': 'config.log',
		'config.status': 'config.status'
	},
	haltOnFailure=True
))

flinux.addStep(OOCompile(
	command=["make"],
	env={
		'CCACHE_PATH': '/usr/bin',
		'CCACHE_DIR': '/home/verm/.ccache'
	},
	name = "build",
	description = ["building"],
	descriptionDone = ["build"],
	haltOnFailure=True
))


#########
# Factory
# FreeBSD
#########
ffbsd = factory.BuildFactory()
ffbsd.addStep(source.SVN(mode="clobber", svnurl=source_unix))

ffbsd.addStep(shell.ShellCommand(
	name = "autogen",
	description = "autogening",
	descriptionDone = "autogen",
	haltOnFailure=True,

	command=["./autogen.sh", "--skip-configure"],
	env={
		'ACLOCAL_FLAGS': '-I /usr/local/share/aclocal -I /usr/home/verm/build/wx/share/aclocal',
		'ACLOCAL': 'aclocal-1.10',
		'AUTOCONF': 'autoconf-2.62',
		'AUTOHEADER': 'autoheader-2.62',
		'AUTOMAKE': 'automake-1.10',
		'LIBTOOLIZE': 'libtoolize'
	}
))

ffbsd.addStep(shell.ShellCommand(
	command=["/usr/local/bin/bash", "./configure",
		"--prefix=/usr/local",
		WithProperties("--with-build-credit=BuildBot: %s (%s)", "buildername", "slavename"),
		"--with-wx-config=/usr/home/verm/build/wx/lib/wx/config/gtk2-unicode-debug-2.9",
		"--enable-maintainer-mode",
		"--disable-check-wx-opengl",
		"--disable-check-wx-stc"
	],
	env={
		'CCACHE_PATH': '/usr/bin:/usr/local/bin',
		'CCACHE_DIR': '/usr/home/verm/.ccache',
		'CC': '/usr/local/libexec/ccache/gcc',
		'CXX': '/usr/local/libexec/ccache/g++',
		'CFLAGS': '-I/usr/local/include',
		'CXXFLAGS': '-I/usr/local/include',
		'LDFLAGS': '-L/usr/local/lib -rpath /usr/home/verm/build/wx/lib',
		'LUA_CFLAGS': '-I/usr/local/include/lua51',
		'LUA_LDFLAGS': '-L/usr/local/lib -llua-5.1'
	},
	name = "configure",
	description = ["configuring"],
	descriptionDone = ["configure"],
	logfiles={
		'config.log': 'config.log',
		'config.status': 'config.status'
	},
	haltOnFailure=True
))

ffbsd.addStep(OOCompile(
	command=["gmake"],
	name = "build",
	description = ["building"],
	descriptionDone = ["build"],
	env={
		'CCACHE_PATH': '/usr/bin:/usr/local/bin',
		'CCACHE_DIR': '/usr/home/verm/.ccache'
	},
	haltOnFailure=True
))


###############
# Factory
# Unix Snapshot
###############

funix_dist = factory.BuildFactory()
funix_dist.addStep(source.SVN(mode="clobber", svnurl=source_unix))
funix_dist.addStep(shell.ShellCommand(
	name = "dist",
	description = "disting",
	descriptionDone = "dist",
	command=[
		"sh",
		"-x",
		"tinderbox/unix/dist.sh",
		WithProperties("%s", "got_revision"),
		WithProperties("%s", "slavename"),
	],
	haltOnFailure=True
))

funix_dist.addStep(agi_upload(
	blocksize=640*1024,
	slavesrc="dist.tar.bz2",
	mode=0644,
	masterdest=WithProperties("tinderbox/snapshot/aegisub-snap-r%s.tar.bz2", "got_revision")),
	name = "upload",
	description = ["uploading"],
	descriptionDone = ["uploaded"],
	haltOnFailure=False,
	flunkOnFailure=False
)


############
# Factory
# OS X Intel
############

fosx = factory.BuildFactory()
fosx.addStep(source.SVN(mode="clobber", svnurl=source_unix))

fosx.addStep(shell.ShellCommand(
	name = "autogen",
	description = "autogening",
	descriptionDone = "autogen",
	haltOnFailure=True,
	command=["./autogen.sh", "--skip-configure"],
	env={
		'ACLOCAL_FLAGS': '-I /Users/verm/prefix/share/aclocal -I /Developer/usr/share/aclocal -I /opt/local/share/aclocal',
		'AUTOMAKE': '/Users/verm/prefix/bin/automake-1.10',
		'ACLOCAL': '/Users/verm/prefix/bin/aclocal-1.10',
		'AUTOCONF': '/Users/verm/prefix/bin/autoconf',
		'AUTOHEADER': '/Users/verm/prefix/bin/autoheader',
		'BIN_CONVERT': '/Users/verm/prefix/bin/convert'
	}
))

fosx.addStep(shell.ShellCommand(
	command=["./configure",
		"--prefix=/Users/verm/prefix",
		WithProperties("--with-build-credit=BuildBot: %s (%s)", "buildername", "slavename"),
		"--with-apple-opengl-framework",
		"--with-wx-config=/Users/verm/prefix/lib/wx/config/osx_cocoa-unicode-debug-2.9",
		"--enable-debug",
		"--disable-check-wx-opengl",
		"--disable-check-wx-stc"
	],
	env={
		'CCACHE_PATH': '/usr/bin',
		'CCACHE_DIR': '/Users/verm/.ccache',
		'CC': '/opt/local/libexec/ccache/gcc',
		'CXX': '/opt/local/libexec/ccache/g++',
		'PKG_CONFIG': '/Users/verm/prefix/bin/pkg-config',
		'PKG_CONFIG_PATH': '/Users/verm/prefix/lib/pkgconfig',
		'CXXFLAGS': '-I/Users/verm/prefix/include',
		'LDFLAGS': '-L/Users/verm/prefix/lib',
		'LUA_CFLAGS': '-I/Users/verm/prefix/include/lua51',
		'LUA_LDFLAGS': '-L/Users/verm/prefix/lib/lua51 -llua',
		'OPENAL_CFLAGS': '-framework OpenAL',
		'OPENAL_LIBS': '-framework OpenAL',
		'CFLAGS': '-I/Developer/SDKs/MacOSX10.5.sdk/usr/include',
		'ICONV_LDFLAGS': '/usr/lib/libiconv.dylib',
	},
	name = "configure",
	description = ["configuring"],
	descriptionDone = ["configure"],
	logfiles={
		'config.log': 'config.log',
		'config.status': 'config.status'
	},
	haltOnFailure=True
))

fosx.addStep(OOCompile(
	command=["make"],
	name = "build",
	description = ["building"],
	descriptionDone = ["build"],
	env={
		'CCACHE_PATH': '/usr/bin',
		'CCACHE_DIR': '/Users/verm/.ccache'
	},
	haltOnFailure=True
))

fosx.addStep(shell.ShellCommand(
	command=["make", "osx-tinderbox-bundle"],
	env={
		'T_BUNDLE': WithProperties("aegisub-%s-%s-%s-r%s", "buildername", "slavename", "buildnumber", "got_revision"),
	},
	name = "bundle",
	description = ["bundling"],
	descriptionDone = ["bundle"],
	haltOnFailure=True
))

fosx.addStep(shell.ShellCommand(
	command=["make", "osx-tinderbox-dmg"],
	env={
		'T_BUNDLE': WithProperties("aegisub-%s-%s-%s-r%s", "buildername", "slavename", "buildnumber", "got_revision"),
		'T_DMG': WithProperties("aegisub-%s-%s-%s-r%s", "buildername", "slavename", "buildnumber", "got_revision"),
	},
	name = "dmg",
	description = ["dmging"],
	descriptionDone = ["dmg"],
	haltOnFailure=True
))

fosx.addStep(agi_upload(
	blocksize=640*1024,
	slavesrc="bundle.dmg",
	mode=0644,
	masterdest=WithProperties("tinderbox/osx/aegisub-%s-%s-%s-r%s.dmg", "buildername", "slavename", "buildnumber", "got_revision")),
	name = "upload",
	description = ["uploading"],
	descriptionDone = ["uploaded"],
	haltOnFailure=False,
	flunkOnFailure=False
)


##########
# Factory
# OS X PPC
##########
fosx_ppc = factory.BuildFactory()
fosx_ppc.addStep(source.SVN(mode="clobber", svnurl=source_unix))

fosx_ppc.addStep(shell.ShellCommand(
	name = "autogen",
	description = "autogening",
	descriptionDone = "autogen",
	haltOnFailure=True,
	command=["./autogen.sh", "--skip-configure"],
	env={
		'ACLOCAL_FLAGS': '-I /Users/verm/prefix/share/aclocal',
		'AUTOMAKE': '/Users/verm/prefix/bin/automake-1.10',
		'ACLOCAL': '/Users/verm/prefix/bin/aclocal-1.10',
		'AUTOCONF': '/Users/verm/prefix/bin/autoconf-2.61',
		'AUTOHEADER': '/Users/verm/prefix/bin/autoheader-2.61',
		'BIN_CONVERT': '/Users/verm/prefix/bin/convert'
	}
))

fosx_ppc.addStep(shell.ShellCommand(
	command=["./configure",
		"--prefix=/Users/verm/prefix",
		WithProperties("--with-build-credit=BuildBot: %s (%s)", "buildername", "slavename"),
		"--with-apple-opengl-framework",
		"--with-wx-config=/Users/verm/prefix/lib/wx/config/osx_cocoa-unicode-debug-2.9",
		"--enable-debug"
		"--prefix=/Users/verm/i",
		"--disable-check-wx-opengl",
		"--disable-check-wx-stc",
	],
	env={
		'CCACHE_PATH': '/usr/bin',
		'CCACHE_DIR': '/Users/verm/.ccache',
		'CC': '/opt/local/libexec/ccache/gcc',
		'CXX': '/opt/local/libexec/ccache/g++',
		'PKG_CONFIG': '/Users/verm/prefix/bin/pkg-config',
		'PKG_CONFIG_PATH': '/Users/verm/prefix/lib/pkgconfig',
		'CXXFLAGS': '-I/Users/verm/prefix/include',
		'LDFLAGS': '-L/Users/verm/prefix/lib',
		'LUA_CFLAGS': '-I/Users/verm/prefix/include/lua51',
		'LUA_LDFLAGS': '-L/Users/verm/prefix/lib/lua51 -llua',
		'OPENAL_CFLAGS': '-framework OpenAL',
		'OPENAL_LIBS': '-framework OpenAL',
		'CFLAGS': '-I/Developer/SDKs/MacOSX10.5.sdk/usr/include',
		'ICONV_LDFLAGS': '/usr/lib/libiconv.dylib',
	},
	name = "configure",
	description = ["configuring"],
	descriptionDone = ["configure"],
	logfiles={
		'config.log': 'config.log',
		'config.status': 'config.status'
	},
	haltOnFailure=True
))

fosx_ppc.addStep(OOCompile(
	command=["make"],
	name = "build",
	env={
		'CCACHE_PATH': '/usr/bin',
		'CCACHE_DIR': '/Users/verm/.ccache'
	},
	description = ["building"],
	descriptionDone = ["build"],
	haltOnFailure=True
))


fosx_ppc.addStep(shell.ShellCommand(
	command=["make", "osx-tinderbox-bundle"],
	env={
		'T_BUNDLE': WithProperties("aegisub-%s-%s-%s-r%s", "buildername", "slavename", "buildnumber", "got_revision"),
	},
	name = "bundle",
	description = ["bundling"],
	descriptionDone = ["bundle"],
	haltOnFailure=True
))

fosx_ppc.addStep(shell.ShellCommand(
	command=["make", "osx-tinderbox-dmg"],
	env={
		'T_BUNDLE': WithProperties("aegisub-%s-%s-%s-r%s", "buildername", "slavename", "buildnumber", "got_revision"),
		'T_DMG': WithProperties("aegisub-%s-%s-%s-r%s", "buildername", "slavename", "buildnumber", "got_revision"),
	},
	name = "dmg",
	description = ["dmging"],
	descriptionDone = ["dmg"],
	haltOnFailure=True
))

fosx_ppc.addStep(agi_upload(
	blocksize=640*1024,
	slavesrc="bundle.dmg",
	mode=0644,
	masterdest=WithProperties("tinderbox/osx/aegisub-%s-%s-%s-r%s.dmg", "buildername", "slavename", "buildnumber", "got_revision")),
	name = "upload",
	description = ["uploading"],
	descriptionDone = ["uploaded"],
	haltOnFailure=False,
	flunkOnFailure=False
)


#########
# Factory
# Windows
#########
fwin = factory.BuildFactory()
fwin.addStep(source.SVN(mode="clobber", svnurl=source_windows))

fwin.addStep(shell.ShellCommand(
	command=["copy aegisub\\tinderbox\\windows\\config_windows.h aegisub\\src\\config\\config_windows.h"],
	name = "configure",
	description = ["configuring"],
	descriptionDone = ["configure"],
	haltOnFailure=True
))

fwin.addStep(OOCompile(
	command=["aegisub\\tinderbox\\windows\\anpan.bat", "Release", "Win32"],
	name = "build",
	description = ["building"],
	descriptionDone = ["build"],
	haltOnFailure=True
))

fwin.addStep(shell.ShellCommand(
	command=[WithProperties("aegisub\\tinderbox\\windows\\dist.bat %s %s %s %s", "buildername", "slavename", "buildnumber", "got_revision")],
	name = "dist",
	description = ["dist"],
	descriptionDone = ["dist"],
	haltOnFailure=True
))

fwin.addStep(agi_upload(
	blocksize=640*1024,
	slavesrc="dist.7z",
	mode=0644,
	masterdest=WithProperties("tinderbox/windows/aegisub-%s-%s-%s-r%s.7z", "buildername", "slavename", "buildnumber", "got_revision")),
	name = "upload",
	description = ["uploading"],
	descriptionDone = ["uploaded"],
	haltOnFailure=False,
	flunkOnFailure=False
)


fwin64 = factory.BuildFactory()
fwin64.addStep(source.SVN(mode="clobber", svnurl=source_windows))

fwin64.addStep(shell.ShellCommand(
	command=["copy aegisub\\tinderbox\\windows\\config_windows.h aegisub\\src\\config\\config_windows.h"],
	name = "configure",
	description = ["configuring"],
	descriptionDone = ["configure"],
	haltOnFailure=True
))

fwin64.addStep(OOCompile(
	name = "build",
	command=["aegisub\\tinderbox\\windows\\anpan.bat", "Release", "x64"],
	description = ["building"],
	descriptionDone = ["build"],
	haltOnFailure=True
))

fwin64.addStep(shell.ShellCommand(
	command=[WithProperties("aegisub\\tinderbox\\windows\\dist.bat %s %s %s %s", "buildername", "slavename", "buildnumber", "got_revision")],
	name = "dist",
	description = ["dist"],
	descriptionDone = ["dist"],
	haltOnFailure=True
))

fwin64.addStep(agi_upload(
	blocksize=640*1024,
	slavesrc="dist.7z",
	mode=0644,
	masterdest=WithProperties("tinderbox/windows/aegisub-%s-%s-%s-r%s.7z", "buildername", "slavename", "buildnumber", "got_revision")),
	name = "upload",
	description = ["uploading"],
	descriptionDone = ["uploaded"],
	haltOnFailure=False,
	flunkOnFailure=False
)


#########
# Factory
# WWW
#########

fwww_dox = factory.BuildFactory()
fwww_dox.addStep(source.SVN(mode="clobber", svnurl=source_unix))

fwww_dox.addStep(shell.ShellCommand(
	name = "aegisub",
	description = "generating",
	descriptionDone = "aegisub",
	logfiles={
		'doxygen.log': 'doxygen.log'
	},
	command=[
		"sh",
		"-x",
		"tinderbox/www/doxygen.sh",
		"aegisub"
	],
	haltOnFailure=True
))

fwww_dox.addStep(shell.ShellCommand(
	name = "reporter",
	description = "generating",
	descriptionDone = "reporter",
	logfiles={
		'doxygen.log': 'doxygen.log'
	},
	command=[
		"sh",
		"-x",
		"tinderbox/www/doxygen.sh",
		"reporter"
	],
	haltOnFailure=True
))


##########
# BUILDERS
##########

slave_win = ["anpan"]
slave_freebsd = ["tako"]
slave_debian = ["mochi"]
slave_ubuntu = ["nori"]
slave_osx = ["kokoro"]
slave_osx_ppc = ["mokona"]
slave_www = ['shoyu']

Windowsx8632   = { 'name': "Windows-x86_32", 'slavenames': slave_win,     'builddir': "full_Windows-x86_32",  'factory': fwin }
Windowsx8664   = { 'name': "Windows-x86_64", 'slavenames': slave_win,     'builddir': "full_Windows-x86_64", 'factory': fwin64 }
DarwinPPC      = { 'name': "Darwin-PPC",     'slavenames': slave_osx_ppc, 'builddir': "full_Darwin-PPC",      'factory': fosx_ppc }
Darwinx8664    = { 'name': "Darwin-x86_64",  'slavenames': slave_osx,     'builddir': "full_Darwin-x86_64",   'factory': fosx }
FreeBSDx8664   = { 'name': "FreeBSD-x86_64", 'slavenames': slave_freebsd, 'builddir': "full_FreeBSD-x86_64",  'factory': ffbsd }
Ubuntux8664    = { 'name': "Ubuntu-x86_64",  'slavenames': slave_ubuntu,  'builddir': "full_Ubuntu-x86_64",   'factory': flinux }
Debianx8632    = { 'name': "Debian-x86_32",  'slavenames': slave_debian,  'builddir': "full_Debian-x86_32",   'factory': flinux }

UNIXDist    = { 'name': "UNIX Dist",  'slavenames': slave_debian + slave_ubuntu + slave_freebsd,  'builddir': "UNIX_Dist",   'factory': funix_dist }
Doxygen     = { 'name': "Doxygen", 'slavenames': slave_www, 'builddir': "www_Doxygen", 'factory': fwww_dox }

c['builders'] = [
Windowsx8632,
Windowsx8664,
DarwinPPC,
Darwinx8664,
FreeBSDx8664,
Ubuntux8664,
Debianx8632,
UNIXDist,
Doxygen
]

builder_name_windows = [
"Windows-x86_32",
"Windows-x86_64"
]

builder_name_unix = [
"FreeBSD-x86_64",
"Ubuntu-x86_64",
"Debian-x86_32"
]

builder_name_darwin = [
"Darwin-PPC",
"Darwin-x86_64"
]

builder_name_www = [
"Doxygen"
]


############
# SCHEDULERS
############ 

from buildbot.scheduler import Scheduler
from buildbot.scheduler import Dependent

full_windows = Scheduler(name="full_windows",
			branch=None,
			treeStableTimer=901,
			builderNames=builder_name_windows,
			fileIsImportant=interested_windows)

full_unix = Scheduler(name="full_unix",
			branch=None,
			treeStableTimer=901,
			builderNames=builder_name_unix,
			fileIsImportant=interested_unix)

full_darwin = Scheduler(name="full_darwin",
			branch=None,
			treeStableTimer=901,
			builderNames=builder_name_darwin,
			fileIsImportant=interested_osx)

full_www = Scheduler(name="full_www",
			branch=None,
			treeStableTimer=901,
			builderNames=builder_name_www,
			fileIsImportant=interested_doxygen)
	
unix_dist = Dependent("unix_dist",
			full_unix,
			builderNames=["UNIX Dist"])



from buildbot.scheduler import Try_Jobdir
try_job = Try_Jobdir("try", builder_name_windows + builder_name_unix + builder_name_darwin, jobdir="jobdir")

c['schedulers'] = [full_windows, full_unix, full_darwin, full_www, unix_dist]

from buildbot import scheduler



################
# STATUS TARGETS
################

c['status'] = []

# Website http://builder.aegisub.org/
from buildbot.status import html
c['status'].append(html.WebStatus(http_port="tcp:8001:interface=127.0.0.1", allowForce=1))


# Send out status emails.
from buildbot.status import mail
from buildbot.status.builder import Results
from datetime import datetime
def message(attrs):
	text = list()

	text.append("STATUS     : %s" % attrs['result'].title())
	text.append("REVISION   : %s" % attrs['revision'])
	text.append("BUILD URL  : %s" % attrs['buildURL'])
	text.append("COMMITTERS : %s" % ' '.join(sorted(attrs['responsibleUsers'])))
	text.append("")
	text.append("LOG FILES")
	for log in attrs['logs']:
		name, url, lines, logstatus = log
		text.append("  %s (%s):\n    %s" % (name, Results[logstatus], url))
	text.append("")

	if attrs['changes']:
		text.append("CHANGES")

	for change in attrs['changes']:
		text.append("  Committer : %s" % change.who)
		text.append("  Revision  : %s" % change.revision)
		text.append("  Changeset : http://devel.aegisub.org/changeset/%s" % change.revision)
		text.append("  Time      : %s" % datetime.fromtimestamp(change.when).strftime("%Y-%m-%d %H:%M:%S"))
		text.append("")
		text.append("  Files")
		for file in change.files:
			text.append("    %s" % file)
		text.append("  Log:\n%s" % '    ' + change.comments.replace("\n", "\n    "))
		text.append("")
		text.append("---------------------------------------------------------------")
		text.append("")
	text.append("")
		
	text.append("- %s" % attrs['buildbotURL'])
	return ("\n".join(text), 'plain')

c['status'].append(mail.MailNotifier(fromaddr="tinderbox@aegisub.org",
                                      extraRecipients=["aegisub-tinderbox@aegisub.org"],
                                      sendToInterestedUsers=False,
                                      customMesg=message,
                                      mode="problem",
                                      extraHeaders={'Reply-To':'aegisub-tinderbox@aegisub.org'}))

# IRC!
from buildbot.status import words
c['status'].append(words.IRC(
	host="irc.rizon.net",
	nick="aegibuild",
	channels=["#aegisub"],
	notify_events={
		'exception': 1,
		'successToFailure': 1,
		'failureToSuccess': 1
	}
))

# Realtime client
# from buildbot.status import client
# c['status'].append(client.PBListener(9988))