#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
from scribe.eoletools import gen_stronger_password
from scribe.ldapconf import MIN_PASSWORD_CLASS, MIN_PASSWORD_LENGTH

parser = argparse.ArgumentParser()
parser.add_argument("-n", "--number", default=1, type=int)
args = parser.parse_args()
if args.number > 1:
    import termios
    import math
    import sys
    if sys.version_info.major >= 3 and sys.version_info.minor >=11:
        width = termios.tcgetwinsize(sys.stdout)[1]
    else:
        import struct
        import fcntl
        try:
            data = fcntl.ioctl(sys.stdout.fileno(),
                               termios.TIOCGWINSZ, u'1234')
            width = struct.unpack('hh', data)[1]
        except:
            width = 80
    cols = width // (MIN_PASSWORD_LENGTH + 4)
    passwords = [gen_stronger_password(MIN_PASSWORD_LENGTH,
                                       MIN_PASSWORD_LENGTH,
                                       MIN_PASSWORD_CLASS).ljust(MIN_PASSWORD_LENGTH + 4)
                 for i in range(args.number)]
    reshaped = [passwords[i * cols: i * cols + cols]
                for i in range(math.ceil(args.number / cols))]

    print("\n".join(["".join(p) for p in reshaped]))

else:
    password = gen_stronger_password(MIN_PASSWORD_LENGTH,
                                     MIN_PASSWORD_LENGTH,
                                     MIN_PASSWORD_CLASS)
    print(password)
