1. Determine protocol

def setup_database(database_location):
    """ 1. Determine protocol"""
    try:
	# When no protocol is given:
    	if database_location.find('://') == -1:
           msg = "Error: Please specify protocol in your --segment-url argument in the format PROTOCOL://HOST"
	   msg +="\nFor example: --segment-url https://segdb.ligo.caltech.edu"
    	   msg += "\nSupported protocols include: http, https, ldbd, ldbdi"
    	   msg += "\nRun with --help for usage"
	   raise ValueError(msg)

	# When wrong protocol is given:
	protocol = database_location[:database_location.find('://')].lower()
	if protocol not in ("http","https","ldbd","ldbdi"): 
	   msg = "Error: protocol %s not supported" % protocol
           msg += "\nPlease specify correct protocol in your --segment-url argument in the format PROTOCOL://HOST"
    	   msg += "\nSupported protocols include: http, https, ldbd, ldbdi"
	   msg += "\nRun with --help for usage"
	   raise ValueError(msg)
    except ValueError, e:
      	print >>sys.stderr, str(e) 
      	sys.exit(1)

    """ 2. Determine host and port"""
    host_and_port = database_location[(len(protocol)+3):]
    if host_and_port.find(':') < 0:
       # if no port number given, set default port respectively:
       host = host_and_port
       if protocol == 'http':
         port = 80
       elif protocol == 'https':
         port = 443
       elif protocol == 'ldbd':
         port = 30015
       elif protocol == 'ldbdi':
         port = 30016
    else:
       host, portString = host_and_port.split(':')
       port = int(portString)

    if port == 30020:
       print >>sys.stderr, "Error: PORT 30020 no longer provide segment database service"
       sys.exit(1)

    """ 3. Set up connection to LDBD(W)Server """
    client = None
    identity = "/DC=org/DC=doegrids/OU=Services/CN=ldbd/"

    if protocol.startswith('http'):
      if protocol == "https":
         identity += host
      else:
         identity = None
      try:
        from pycbc_glue import LDBDWClient
        client = LDBDWClient.LDBDClient(host,port,protocol,identity)
      except Exception, e:
        print >>sys.stderr, "Unable to connect to LDBD Server at %s://%s:%d " % (protocol,host, port) + str(e)
        sys.exit(1)

    elif protocol.startswith('ldbd'):
      if protocol == "ldbd":
         identity += host
         from pycbc_glue import gsiserverutils
      else:
         identity = None
      try:
        from pycbc_glue import LDBDClient
        client = LDBDClient.LDBDClient(host,port,identity)
      except Exception, e:
        print >>sys.stderr, "Unable to connect to LDBD Server at %s://%s:%d" % (protocol,host, port) + str(e)
        try:
          if gsiserverutils.checkCredentials():
             print >>sys.stderr, "Got the following error : " + str(e)
             print >>sys.stderr, "Run wiht --help for usage"
        except UnboundLocalError:
          pass
        sys.exit(1)

    else:
        raise ValueError( "invalid url for segment database" )
    

    return client