#!/usr/bin/env python
"""Register with a facilitator using an indirect URL."""

import argparse
import flashproxy
import sys
import urlparse

from flashproxy.keys import ensure_M2Crypto
from flashproxy.reg import DEFAULT_FACILITATOR_URL, build_reg_b64enc

parser = argparse.ArgumentParser(
    usage="%(prog)s [OPTIONS] REMOTE[:PORT]",
    description="Print a URL, which, when retrieved, will cause the input "
    "client address to be registered with the flash proxy facilitator.")
flashproxy.reg.add_registration_args(parser)
parser.add_argument("-f", "--facilitator", metavar="URL",
    help="register with the given facilitator, default %(default)s.",
    default=DEFAULT_FACILITATOR_URL)

options = parser.parse_args(sys.argv[1:])

ensure_M2Crypto()

if not options.remote_addr[0]:
    print >> sys.stderr, "An IP address (not just a port) is required."
    sys.exit(1)

reg = build_reg_b64enc(options.remote_addr, options.transport, urlsafe=True)
print urlparse.urljoin(options.facilitator, "reg/" + reg)
