{% if odoo_version == '10.0' %}
#!/usr/bin/env python
{% else %}
#!/usr/bin/env python3
{% endif %}
# -*- coding: utf-8 -*-
import grp
import os
import pwd
import shutil
from subprocess import call
{% if odoo_version == '10.0' %}
from urlparse import urlsplit
{% else %}
from urllib.parse import urlsplit
{% endif %}
import yaml

DEFAULT_PRIVATE_REPO = "/opt/odoo/additional_addons"
REPO_ADDON_FILE = "/opt/odoo/private/repos-addons.yaml"
UID = pwd.getpwnam("odoo").pw_uid
GID = grp.getgrnam("odoo").gr_gid

if os.path.isfile(REPO_ADDON_FILE):
    repos = yaml.safe_load(open(REPO_ADDON_FILE))
    if repos:
        for url in repos:
            repo = urlsplit(url).path
            call(["git", "clone", "-b", repos[url]["branch"], "--depth", "1",
                  url, "/tmp/addon"+repo])
            for module in repos[url]["modules"]:
                shutil.move("/tmp/addon"+repo+"/"+module,
                            DEFAULT_PRIVATE_REPO+"/"+module)

        shutil.rmtree("/tmp/addon")
