import pkg_resources


def get_version(deploy_dir, conf):
    """Check if the deploy directory requires the version and deal with it
    properly

    Parameters
    ----------
    deploy_dir : string
        directory where the stuff meed to be deployed
    conf : dict-like
        configurations for the current project

    Returns
    -------
    version : string
        version number or None, if not required
    allow_overwrite : bool
        whether overwriting the deploy_dir, if existing, is allowed
    """
    has_version = (('{version}' in deploy_dir) and
                   ('{{version}}' not in deploy_dir) and
                   ('${version}' not in deploy_dir))
    version = None
    allow_overwrite = False

    if has_version:
        pkg_name = conf['pkg_name']
        pversion = pkg_resources.get_distribution(pkg_name).parsed_version
        if pversion.is_prerelease or pversion.is_postrelease:
            version = conf.get('version_dev', pversion.public)
            allow_overwrite = conf.getboolean('overwrite_dev', True)
        else:
            version = pversion.public
            allow_overwrite = conf.getboolean('overwrite_releases', False)

    return version, allow_overwrite