# License: GPLv3 Copyright: 2009, John Schember <john@nachtimwald.com>

import os


class EreaderError(Exception):
    pass


def image_name(name, taken_names=()):
    name = os.path.basename(name)

    if len(name) > 32:
        cut = len(name) - 32
        names = name[:10]
        namee = name[10 + cut :]
        name = f'{names}{namee}.png'

    i = 0
    base_name, ext = os.path.splitext(name)
    while name in taken_names:
        i += 1
        name = f'{base_name}{i}{ext}'

    return name.ljust(32, '\x00')[:32]
