#!/usr/bin/env python
# Maps repository names to rdoinfo distgit names
# package names are all either exact matches or prefixed with either
# "python-" or "openstack-", so we just check that the ends match

import sys

from rdopkg.actionmods import rdoinfo

if len(sys.argv) == 3:
    rdoinfo_location = sys.argv[2]
else:
    rdoinfo_location = "https://github.com/redhat-openstack/rdoinfo.git"

if rdoinfo_location.startswith('http'):
    inforepo = rdoinfo.RdoinfoRepo(".", rdoinfo_location)
    inforepo.init()
else:
    inforepo = rdoinfo.RdoinfoRepo(local_repo_path=rdoinfo_location)

# Attempt a strict mapping with project name
for package in inforepo.get_info()["packages"]:
    if "project" in package and sys.argv[1] == package["project"]:
        if package["project"].startswith("puppet"):
            print("puppet/%s-distgit" % package["project"])
        else:
            print("openstack/%s-distgit" % package["project"])
        exit(0)

# If nothing has been found then attempt a speculative mapping
# looking if sys.argv[1] is found as package["name"] or package["project"]
# suffix
for package in inforepo.get_info()["packages"]:
    if (package["name"].endswith(sys.argv[1]) or
       "project" in package and package["project"].endswith(sys.argv[1])):
        if package["project"].startswith("puppet"):
            print("puppet/%s-distgit" % package["project"])
        else:
            print("openstack/%s-distgit" % package["project"])
        exit(0)

exit(1)
