#!/usr/bin/env python
# Copyright(C) 2012 thomasv@gitorious

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/agpl.html>.

import time, sys, traceback
import ConfigParser

def attempt_read_config(config, filename):
    try:
        with open(filename, 'r') as f:
            config.readfp(f)
    except IOError:
        print "Could not read %s. Falling back." % filename

def create_config():
    config = ConfigParser.ConfigParser()
    # set some defaults, which will be overwritten by the config file
    config.add_section('server')
    config.set('server','banner', 'Welcome to Electrum!')
    config.set('server', 'host', 'localhost')
    config.set('server', 'native_port', '50000')
    config.set('server', 'stratum_tcp_port', '50001')
    config.set('server', 'stratum_http_port', '8081')
    config.set('server', 'password', '')
    config.set('server', 'irc', 'yes')
    config.set('server', 'irc_nick', '')
    config.add_section('database')
    config.set('database', 'type', 'psycopg2')
    config.set('database', 'database', 'abe')
    config.set('server', 'backend', 'abe')

    for path in ('', '/etc/'):
        filename = path + 'electrum.conf'
        attempt_read_config(config, filename)

    try:
        with open('/etc/electrum.banner', 'r') as f:
            config.set('server','banner', f.read())
    except IOError:
        pass

    return config

def run_rpc_command(command, stratum_http_port):
    import jsonrpclib
    server = jsonrpclib.Server('http://%s:%s'%(host, stratum_http_port))
    if command == 'stop':
        result = server.server.stop(password)
    else:
        result = "Unknown command: '%s'" % command
    print result

if __name__ == '__main__':
    config = create_config()
    password = config.get('server', 'password')
    host = config.get('server', 'host')
    native_port = config.get('server', 'native_port')
    stratum_tcp_port = config.get('server', 'stratum_tcp_port')
    stratum_http_port = config.get('server', 'stratum_http_port')

    if len(sys.argv) > 1:
        run_rpc_command(sys.argv[1], stratum_http_port)
        sys.exit(0)

    from processor import Dispatcher
    from transports.stratum_http import HttpServer
    from transports.stratum_tcp import TcpServer
    from transports.native import NativeServer

    from modules.irc import ServerProcessor
    backend_name = config.get('server', 'backend')
    if backend_name == "libbitcoin":
        # NativeServer cannot be used with libbitcoin
        native_port = None
        config.set('server', 'native_port', '')
        from modules.python_bitcoin import BlockchainProcessor
    elif backend_name == "abe":
        from modules.abe import AbeProcessor as BlockchainProcessor
    else:
        sys.stderr.write('Unknown backend specified\n')
        sys.exit(-1)

    # Create hub
    dispatcher = Dispatcher()
    shared = dispatcher.shared

    # Create and register processors
    chain_proc = BlockchainProcessor(config)
    dispatcher.register('blockchain', chain_proc)

    server_proc = ServerProcessor(config)
    dispatcher.register('server', server_proc)

    transports = []
    # Create various transports we need
    if native_port is not None:
        server_banner = config.get('server','banner')
        native_server = NativeServer(shared, chain_proc, server_proc,
                                     server_banner, host, int(native_port))
        transports.append(native_server)

    if stratum_tcp_port is not None:
        tcp_server = TcpServer(dispatcher, host, int(stratum_tcp_port))
        transports.append(tcp_server)

    if stratum_http_port is not None:
        http_server = HttpServer(dispatcher, host, int(stratum_http_port))
        transports.append(http_server)

    for server in transports:
        server.start()

    print "Starting Electrum server on", host
    while not shared.stopped():
        time.sleep(1)
    print "Server stopped"


@@ -923,58 +923,11 @@
    p
-rint %22Could not read %25s. Falling back.%22 %25 filename
+ass
 %0A%0Ade
@@ -1664,19 +1664,19 @@
 in (
-'', 
 '/etc/'
+, ''
 ):%0A 

load /etc/electrum.conf first, then ./electrum.conf (takes high precedence over /etc)