def main():
    try:
        config = ConfigParser()
        config.read(["config.cfg"])
        config.options("master-public-keys")
    except NoSectionError:
        print("Non-existant configuration file `config.cfg`")
        return
    try:
        rpc_u = config.get("bitcoin-rpc", "rpc_user")
        rpc_p = config.get("bitcoin-rpc", "rpc_password")
    except NoOptionError:
        rpc_u, rpc_p = server.obtain_rpc_username_password(config.get(
            "bitcoin-rpc", "datadir"))
    if rpc_u == None:
        return
    rpc = JsonRpc(host = config.get("bitcoin-rpc", "host"),
        port = int(config.get("bitcoin-rpc", "port")),
        user = rpc_u, password = rpc_p,
        wallet_filename=config.get("bitcoin-rpc", "wallet_filename").strip())
    user_input = input("Enter earliest wallet creation date (DD/MM/YYYY) "
        "or block height to rescan from: ")
    try:
        height = int(user_input)
    except ValueError:
        height = search_for_block_height_of_date(user_input, rpc)
        if height == -1:
            return
        height -= 2016 #go back two weeks for safety

    if input("Rescan from block height " + str(height) + " ? (y/n):") != 'y':
        return
    rpc.call("rescanblockchain", [height])
    print("end")
def main():
    global debug_fd
    if len(sys.argv) == 2:
        if sys.argv[1] == "--help":
            print("Usage: ./server.py <path/to/current/working/dir>\nRunning" +
                " without arg defaults to the directory you're in right now")
            return
        else:
            os.chdir(sys.argv[1])
    debug_fd = open("debug.log", "w")
    debug("current working directory is: " + os.getcwd())
    try:
        config = ConfigParser()
        config.read("config.cfg")
        config.options("master-public-keys")
    except NoSectionError:
        log("Non-existant configuration file `config.cfg`")
        return
    try:
        rpc_u = config.get("bitcoin-rpc", "rpc_user")
        rpc_p = config.get("bitcoin-rpc", "rpc_password")
        debug("obtaining auth from rpc_user/pass")
    except NoOptionError:
        rpc_u, rpc_p = obtain_rpc_username_password(config.get(
            "bitcoin-rpc", "datadir"))
        debug("obtaining auth from .cookie")
    if rpc_u == None:
        return
    rpc = JsonRpc(host = config.get("bitcoin-rpc", "host"),
        port = int(config.get("bitcoin-rpc", "port")),
        user = rpc_u, password = rpc_p,
        wallet_filename=config.get("bitcoin-rpc", "wallet_filename").strip())

    #TODO somewhere here loop until rpc works and fully sync'd, to allow
    # people to run this script without waiting for their node to fully
    # catch up sync'd when getblockchaininfo blocks == headers, or use
    # verificationprogress
    printed_error_msg = False
    while bestblockhash[0] == None:
        try:
            bestblockhash[0] = rpc.call("getbestblockhash", [])
        except JsonRpcError as e:
            if not printed_error_msg:
                log("Error: " + repr(e))
                log("Error with bitcoin rpc, check host/port/user/password")
                printed_error_msg = True
            time.sleep(5)

    import_needed, relevant_spks_addrs, deterministic_wallets = \
        get_scriptpubkeys_to_monitor(rpc, config)
    if import_needed:
        transactionmonitor.import_addresses(rpc, relevant_spks_addrs, debug,
            log)
        log("Done.\nIf recovering a wallet which already has existing " +
            "transactions, then\nrun the rescan script. If you're confident " +
            "that the wallets are new\nand empty then there's no need to " +
            "rescan, just restart this script")
    else:
        txmonitor = transactionmonitor.TransactionMonitor(rpc,
            deterministic_wallets, debug, log)
        if not txmonitor.build_address_history(relevant_spks_addrs):
            return
        hostport = (config.get("electrum-server", "host"),
                int(config.get("electrum-server", "port")))
        poll_interval_listening = int(config.get("bitcoin-rpc",
            "poll_interval_listening"))
        poll_interval_connected = int(config.get("bitcoin-rpc",
            "poll_interval_connected"))
        certfile = config.get("electrum-server", "certfile")
        keyfile = config.get("electrum-server", "keyfile")
        run_electrum_server(hostport, rpc, txmonitor, poll_interval_listening,
            poll_interval_connected, certfile, keyfile)