from optparse import OptionParser, OptionValueError
import io 
import random

'''
This file is used to generate zone file and its queryperf data for dnssec test.
label count is one random number between 1 and 5, only harf part of queryperf data exist in zone file.
'''

def set_cmd_options(parser):
    parser.add_option('-z', '--zone_name', dest = 'zone_name', type = 'string',
                      default = '.', help = 'specify zone name.')

    parser.add_option('-c', '--count', dest = 'count', type = 'int',
                       default = '10000', help = 'specify zone name.')

def one_label():
    ch = random.choice('abcdefghigklmnopqistuvwxyz')
    return ch + str(random.randint(0, 900000000))

def gen_label_name():
    count = random.randint(1, 5)
    name = ''
    i = 0
    while i < count:
        name += one_label() + '.'
        i = i + 1

    return name
    
def create_record_name(zone_name):
    name1 = gen_label_name() + zone_name
    name2 = gen_label_name() + zone_name
    name3 = gen_label_name() + zone_name
    name4 = gen_label_name() + zone_name
    return (name1, name2, name3, name4)


if __name__ == '__main__':
    parser = OptionParser()
    set_cmd_options(parser)
    (options, args) = parser.parse_args()
    zone_name = options.zone_name

    if zone_name[len(zone_name) - 1] != '.':
        zone_name += '.'
    
    file_ = io.open(zone_name + "_zone", 'w')
    qpfile_ = io.open(zone_name + "_qpdata", 'w')

    print('%s 3600 IN SOA a.%s email.%s 1 7200 3600 200000 1000' % (zone_name, zone_name, zone_name), file = file_)
    print('%s 1000 IN NS a.org.zlk.' % (zone_name), file = file_)

    i = 2
    while ( i < options.count):
        name1, name2, name3, name4 = create_record_name(zone_name)
        print('%s %d IN NS %s' % (name1, 1000,  'a.org.zlk.'), file = file_)
        print('%s %d IN NS %s' % (name2, 1000, 'b.org.zlk.'), file = file_)
        print('%s %d IN A %s' % (name3, 3600, '1.1.1.1'), file = file_)
        print('%s %d IN A %s' % (name4, 3600, '1.1.1.1'), file = file_)

        print('%s NS' % (name1), file = qpfile_)
        print('%s NS' % (gen_label_name() + zone_name), file = qpfile_)
        print('%s A' % (name3), file = qpfile_)
        print('%s A' % (gen_label_name() + zone_name), file = qpfile_)
        i = i + 4 

    file_.close()
    qpfile_.close()


