# -*- coding: utf-8 -*-
from setuphelpers import *
from setupdevhelpers import *
import os


def update_package():
    install_apt('python3-pip')
    install_apt('git')
    
    mkdirs('build')
    for u in ['master']:

        if isdir("/tmp/doc-samba"):
            remove_tree('/tmp/doc-samba')

        run(fr'git clone --branch %s https://gitea.ad.tranquil.it/doc/doc-samba4.git /tmp/doc-samba' % u)

        olddir = os.getcwd()
        os.chdir('/tmp/doc-samba')

        run("make clean")
        run("rm -Rf ./build/")
        run("pip3 install -U setuptools --break-system-packages")
        run("pip3 install -r requirements.txt --upgrade --break-system-packages")
        run("make gettext")
        run("sphinx-intl update -p build/locale/ -l fr")
        run("make clean")
        run("make htmlen")
        run('make -e SPHINXOPTS="-D language=\'fr\'" htmlfr')
        run('make epub')

        os.chdir(olddir)
        version = "0.0.%s" % run(r"git -C /tmp/doc-samba rev-list --count master").strip()
        control.version = "%s-%s" % (version, control.version.split("-", 1)[-1])
        control.save_control_to_wapt()
        
        copytree2('/tmp/doc-samba/build',makepath('build', u))
