#!/usr/local/bin/python2.7
#
# Copyright 2011 by Object Craft P/L, Melbourne, Australia.
#
# LICENCE - see LICENCE file distributed with this software for details.
#
import sys, os

from albatross.cmdline.common import *

if len(sys.argv) < 2:
    cmd = 'help'
elif ourname == 'al-session-daemon':
    cmd = 'session-server'
elif ourname == 'al-httpd':
    cmd = 'serve'
else:
    cmd = sys.argv.pop(1)
    if cmd == '--help':
        cmd = 'help'
    if cmd == 'help' and len(sys.argv) > 1:
        cmd = sys.argv[1]
        sys.argv[1] = '-h'

try:
    import albatross
except ImportError:
    sys.exit('Albatross not installed')

main = None
try:
    modname = 'albatross.cmdline.' + cmd.replace('-', '_')
    module = __import__(modname, None, None, 'main')
except ImportError:
    pass
else:
    try:
        main = module.main
    except AttributeError:
        pass
if main is None:
    sys.exit('Unknown cmd %r. See "%s help"' % (cmd, ourname))

try:
    main(sys.argv[1:])
except Error, e:
    sys.exit('%s %s: %s' % (ourname, cmd, e))
