from pathlib import Path


def package2sty(pkgname, lsRdb, allsty):
    """
    Find the main .sty file for a given package

    :param pkgname: the name of a package as registered on CTAN
    :param lsRdb: a dictionnary of the ls-R database

    :return a filename without the .sty extension or None
    """
    styname = pkgname + '.sty'
    if styname in allsty:
        return pkgname
    if pkgname in lsRdb:
        files = lsRdb[pkgname]
        if pkgname + '.sty' in files:
            return pkgname
        else:
            for f in files:
                if f.lower() == pkgname + '.sty':
                    return Path(f).stem
    return None