#!/usr/bin/env python

import sys
import os

try:
	from MetadataService.MasterService import MasterService
except KeyboardInterrupt:
	# FIXME probably what's wanted is that nothing is spit out
	sys.stderr.write("Initialization interrupted while loading components\n");
	sys.stderr.flush()
	sys.exit(os.EX_TEMPFAIL)
except ImportError:
	sys.stderr.write("Initialization aborted: missing components\n\nThis usually means that either the metadata service libraries, ZODB, PostgreSQL, and/or the required software cannot be found.  Please check the metadata service installation, check and install required compopnents and try again. You might need to set the PYTHONPATH environment variable to the metadata service library directory and ZODB's lib/python directory if either the metadata service or ZODB are installed in a non-standard location.\n\nYou might also (very improbable) not have enough memory to load all the required components.  Here is a full traceback:\n\n")
	import traceback
	traceback.print_exc()
	sys.stderr.write("\nInitialization aborted.  Read the explanation above.\n")
	sys.stderr.flush()
	sys.exit(os.EX_UNAVAILABLE)

def main(argv):

	main.app = MasterService()
	# this will exit after two or three seconds due to the fork() call
	sys.exit(main.app.run())


if __name__ == "__main__":
	try:
		main(sys.argv)
	except KeyboardInterrupt:
		sys.stderr.write("Initialization aborted while loading the metadata service\n");
		sys.stderr.flush()
