import socket
import sys
from quixote.server.scgi_server import run

import larpe

def start(args):
    port = 3007
    extra_dirs = []
    single_host = False

    i = 0
    while i < len(args):
        if args[i] == '--port':
            port = int(args[i+1])
            i += 1
        elif args[i] == '--extra':
            extra_dirs.append(args[i+1])
            i += 1
        elif args[i] == '--single-host':
            single_host = True
        i += 1

    larpe.extra_dirs = extra_dirs

    try:
        if single_host:
            run(larpe.create_publisher, port=port, script_name = '')
        else:
            run(larpe.create_vhost_publisher, port=port, script_name = '')
    except socket.error, e:
        if e[0] == 98:
            print >> sys.stderr, 'address already in use'
            sys.exit(1)
        raise
    except KeyboardInterrupt:
        sys.exit(1)
