import datetime

from libcloud import __version__ as libcloud_version
from libcloud.dns.types import RecordType

from libcloud_to_bind.utils import get_record_id

__version__ = '0.1.0'
__all__ = [
    '__version__',
    'libcloud_zone_to_bind_zone_file'
]


def get_record_line(record):
    parts = []

    if record.name:
        name = '%(name)s.%(domain)s' % {'name': record.name,
                                        'domain': record.zone.domain}
    else:
        name = record.zone.domain

    name += '.'

    ttl = record.extra['ttl'] if 'ttl' in record.extra else record.zone.ttl
    ttl = str(ttl)
    record_type = RecordType.__repr__(record.type)
    data = record.data

    if record.type in [RecordType.CNAME, RecordType.DNAME, RecordType.MX,
                       RecordType.PTR, RecordType.SRV]:
        # Make sure trailing dot is present
        if data[len(data) - 1] != '.':
            data += '.'

    if record.type in [RecordType.TXT, RecordType.SPF] and ' ' in data:
        # Escape the quotes
        data = data.replace('"', '\\"')

        # Quote the string
        data = '"%s"' % (data)

    if record.type in [RecordType.MX, RecordType.SRV]:
        priority = record.extra['priority']
        parts = [name, ttl, 'IN', record_type, priority, data]
    else:
        parts = [name, ttl, 'IN', record_type, data]

    line = '\t'.join(parts)
    return line


def libcloud_zone_to_bind_zone_file(zone):
    if zone.type != 'master':
        raise ValueError('You can only generate BIND file for master zones')

    lines = []

    # For consistent output, records are sorted based on the id
    records = zone.list_records()
    records = sorted(records, key=get_record_id)

    date = datetime.datetime.now().strftime('%Y-%M-%d %H:%m:%S')
    values = {'version': libcloud_version, 'date': date}

    lines.append('; Generated by Libcloud v%(version)s on %(date)s' % values)
    lines.append('$ORIGIN %(domain)s.' % {'domain': zone.domain})
    lines.append('$TTL %(domain_ttl)s\n' % {'domain_ttl': zone.ttl})

    for record in records:
        line = get_record_line(record=record)
        lines.append(line)

    output = '\n'.join(lines)
    return output

@@ -1,12 +1,775 @@
+# Licensed to Tomaz Muraus under one or more%0A# contributor license agreements.  See the NOTICE file distributed with%0A# this work for additional information regarding copyright ownership.%0A# Tomaz muraus licenses this file to You under the Apache License, Version 2.0%0A# (the %22License%22); you may not use this file except in compliance with%0A# the License.  You may obtain a copy of the License at%0A#%0A#     http://www.apache.org/licenses/LICENSE-2.0%0A#%0A# Unless required by applicable law or agreed to in writing, software%0A# distributed under the License is distributed on an %22AS IS%22 BASIS,%0A# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A# See the License for the specific language governing permissions and%0A# limitations under the License.%0A%0A
 import datet

Add license header.